if(isset($ _ POST ['Submit']))没有获得状态值

时间:2014-01-03 06:03:53

标签: php mysql ajax

从这个ajax函数我填充了第一个下拉列表first_state.php * 以下是完整代码的一部分 *

function showUser(str)
{
if (str=="")
  {
  document.getElementById("txtHint").innerHTML="";
  return;
  }
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;
    }
  }
xmlhttp.open("GET","get_district.php?q="+str,true);
xmlhttp.send();
}

在这里我调用函数并获取第一个下拉列表的值

<form class="form2" name="form_pin" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<select name="state"  onchange="showUser(this.value)">
<option value="">Select State</option>
while ($row = mysql_fetch_array($result)){                          
echo "<option value='".$row['State']."'>".$row['State']."</option>";}
</form>

在调用以下功能时我没有获得地区的价值

if(isset($_POST['Submit']))
{
$state = $_POST["state"];
$district = $_POST["district"];
echo $state; echo $district;        
}

将varable传递给get_district.php时填充的第二个下拉列表

<?php
include_once "connect_db.inc";
$q = $_GET['q'];
//echo $q;
$con = mysqli_connect($dbhost, $dbusername, $dbuserpassword);
if (!$con)
  {
  die('Could not connect: ' . mysqli_error($con));
  }

mysqli_select_db($con,$default_dbname);
$sql="SELECT DISTRICT FROM state_district WHERE STATE = '".$q."'";
//$sql="SELECT * FROM state_district";

$result = mysqli_query($con,$sql);
?>
<select name="district">
<?PHP
while($row = mysqli_fetch_array($result))
  {
     echo "<option value='".$row['DISTRICT']."'>".$row['DISTRICT']."</option>";
  }
?>
</select>
<?PHP
mysqli_close($con);
?> 

如何以一种形式获取_post ['district']和_post ['state'],上面的代码工作正常,但我我一无所知。问题是我有两个php文件**

2 个答案:

答案 0 :(得分:0)

**You are not requesting the below code** 
------------------

<select name="district">
<?PHP
while($row = mysqli_fetch_array($result))
  {
     echo "<option value='".$row['DISTRICT']."'>".$row['DISTRICT']."</option>";
  }
?>
</select>
-----------------------------

**to do so add ID with <select id="txtHint"></select> option in first_step.php**     
--------------------------
<form class="form2" name="form_pin" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<select name="state"  onchange="showUser(this.value)">
<option value="">Select State</option>
while ($row = mysql_fetch_array($result)){                          
echo "<option value='".$row['State']."'>".$row['State']."</option>";}
**// add below**
<select id="txtHint"></select>
</form>

答案 1 :(得分:0)

在函数文件中用$ _get替换$ _post