PHP and JS "Get" problem
Hello here is my code:
<script type="text/javascript">
function changePage()
{
selectedValue = document.forms['f1'].s1.value;
newLocation = "http://127.0.0.1:8080/PolPlan/branze.php?page="+selectedValue;
window.locatoion = newLocation;
}
</script>
<form name="f1">
<select name="s1" onChange="javascript:changePage()">
<option value="1">strona 1</option>
<option value="2">开发者_如何学JAVAstrona 2</option>
</select>
</form>
<?php
$selectedValue = $_GET['s1'];
echo $selectedValue;
?>
I get error on line: "$selectedValue = $_GET['s1'];" Error message: Notice: Undefined index: s1 Please help me with it.
Try using isset:
if (isset($_GET['s1'])) {
$selectedValue = $_GET['s1'];
echo $selectedValue;
}
Also be sure that the variable in your GET is the same as "s1". It looks like it should be "page".
if (isset($_GET['page'])) {
$selectedValue = $_GET['page'];
echo $selectedValue;
}
PHP is saying that the index 's1' of the array $_GET doesn`t exists. Try this:
<script type="text/javascript">
function changePage()
{
document.forms[0].submit(); // Crossbrowser instruction
}
</script>
<form name="s1" action="http://127.0.0.1/PolPlan/branze.php">
<select name="s1" onChange="javascript:changePage()">
<option value="1">strona 1</option>
<option value="2">strona 2</option>
</select>
</form>
<?php
$selectedValue = isset($_GET['s1']) ? $_GET['s1'] : '';
echo $selectedValue;
?>
Change
$selectedValue = $_GET['s1'];
to
$selectedValue = $_GET['page'];
if you want to use s1 you need to submit the form
<form name="f1" action="/PolPlan/branze.php" method="get">
<select name="s1" onChange="document.forms['f1'].submit()">
<script type="text/javascript">
function changePage()
{
selectedValue = document.forms['f1'].s1.value;
newLocation = "http://127.0.0.1:8080/PolPlan/branze.php?page="+selectedValue;
window.locatoion = newLocation;
}
</script>
<form name="f1">
<select name="s1" onChange="javascript:changePage()">
<option value="1">strona 1</option>
<option value="2">strona 2</option>
</select>
</form>
<?php
$selectedValue = $_GET['page'];
echo $selectedValue;
?>
s1 is undefined because it doesn't exist as an element of $_GET. If you want to know the identifier of that value, it's what's on the left of the =
in the URL, not the name of the form element (in this case – this wouldn't necessarily be the case with, say, POST)
you are sending the value as
$_GET['page']
Tyr this:
<script type="text/javascript">
function changePage()
{
selectedValue = document.forms['f1'].s1.value;
if (selectedValue == 0) {
alert("Please select a valid option");
} else {
newLocation = "http://127.0.0.1:8080/PolPlan/branze.php?page="+selectedValue;
window.location = newLocation;
}
}
</script>
<form name="f1">
<select name="s1" onChange="javascript:changePage()">
<option value="0">Select...</option>
<option value="1">strona 1</option>
<option value="2">strona 2</option>
</select>
</form>
<?php
if (isset($_GET['s1'])) {
$selectedValue = $_GET['s1'];
echo $selectedValue;
}
?>
You can remove the php code.
Hope works for you.
精彩评论