将选择框选项值传递给php

时间:2013-03-16 12:24:55

标签: php javascript html ajax

<body>
<form method="post" action="eex4.php">
  <label for="text_2">Select the Scheme:&nbsp;&nbsp;&nbsp;</label>
  <select name="users" id="users">
    <option value="">Select:</option>
    <option value="1">Sem1</option>
    <option value="2">Sem2</option>
    <option value="3">Sem3</option>
    <option value="4">Sem4</option>
    <option value="5">Sem5</option>
    <option value="6">Sem6</option>
  </select>
  &nbsp;&nbsp;&nbsp;
  <input type="submit" value="Submit" name="btn" onClick="ResultPage()"/>
</form>
</body>

上面的代码输出有selectbox with submit按钮。当我点击提交按钮时,它会调用脚本。

<script>
function ResultPage()
{
if (window.XMLHttpRequest)
  {// code for IE7+, Firefox, Chrome, Opera, Safari
  xmlhttp=new XMLHttpRequest();
  }
else
  {// code for IE6, IE5
  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
xmlhttp.onreadystatechange=function()
  {
  if (xmlhttp.readyState==4 && xmlhttp.status==200)
    {
    document.getElementById("txtHint").innerHTML=xmlhttp.responseText;
    }
  }
 var select = document.getElementById('users');
 var sem = select.options[select.selectedIndex].value;
 var uurl = "eex4.php?q=" + sem;

xmlhttp.open("GET",uurl,true);
xmlhttp.send();
}
</script>

我需要传递selectbox的值,我选择了哪个选项并发送到下面的php文件。

eex4.php

<?php
$schm = $_GET['sem'];
$txt1 = "Semester";
echo $txt1 ." ". $schm;
?> 

我的预期结果是:

“学期[选项的值有选择]”但它只是显示“学期”

任何人都可以找到错误..

6 个答案:

答案 0 :(得分:2)

使用

$schm = $_GET['q'];

因为你在这里传递了q

var uurl = "eex4.php?q=" + sem;

问题在于,当您点击提交按钮时,您的表单会将数据发布到eex4.php,只显示Semester

更改

<input type="submit"

<input type="button"

因此,当您单击此按钮时,内容将加载到页面中而不刷新页面。

答案 1 :(得分:0)

ajax部分取自w3schools。您只需要替换$schm = $_GET['sem']; to $schm = $_GET['q'];

答案 2 :(得分:0)

更改

var uurl = "eex4.php?q=" + sem;

var uurl = "eex4.php?sem=" + sem;

因为你在eex4.php中获得'sem'但是将'q'传递给它

答案 3 :(得分:0)

变化

$schm = $_GET['sem'];

$schm = $_GET['q'];

答案 4 :(得分:0)

您通过q

传递价值
$schm = $_GET['q'];

答案 5 :(得分:0)

   <body>
  <div id="txtHint"></div>
  <form method="post" action="eex4.php">
  <label for="text_2">Select the Scheme:&nbsp;&nbsp;&nbsp;</label>
  <select name="users" id="users">
    <option value="">Select:</option>
    <option value="1">Sem1</option>
    <option value="2">Sem2</option>
    <option value="3">Sem3</option>
    <option value="4">Sem4</option>
    <option value="5">Sem5</option>
    <option value="6">Sem6</option>
  </select>
  &nbsp;&nbsp;&nbsp;
  <input type="submit" value="Submit" name="btn" onClick="return ResultPage()"/>
</form>
</body>



<script>
function ResultPage()
{
if (window.XMLHttpRequest)
  {// code for IE7+, Firefox, Chrome, Opera, Safari
  xmlhttp=new XMLHttpRequest();
  }
else
  {// code for IE6, IE5
  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
xmlhttp.onreadystatechange=function()
  {
  if (xmlhttp.readyState==4 && xmlhttp.status==200)
    {
    document.getElementById("txtHint").innerHTML=xmlhttp.responseText;
    }
  }
 var select = document.getElementById('users');
 var sem = select.options[select.selectedIndex].value;
 var uurl = "eex4.php?q=" + sem;

xmlhttp.open("GET",uurl,true);
xmlhttp.send();
return false;
}
</script>

<强> eex4.php

<?php
$schm = $_GET['sem'];
$txt1 = "Semester";
echo $txt1 ." ". $schm;
?>