我試圖將多個值傳遞給javascript函數「get2」,然後調用「data2.php」文件以從數據庫檢索數據,根據提交的值下拉列表/文本框。兩者都單獨工作,但不在一起,我得到的錯誤是「未定義索引」。任何人都可以幫助我嗎?將多個值從javascript傳遞到php文件
<script type="text/javascript">
function get2() {
$.post('data2.php', { gender: form2.gender.value },
function(output) {
$('#gender').html(output).show();
}
);
$.post('data2.php', { skills: form2.skills.value },
function(output) {
$('#skills').html(output).show();
});
}
</script>
...
...
<form name="form2">
<Select name="gender">
<OPTION>Male</OPTION>
<OPTION>Female</OPTION></SELECT>
What is the patients relationship status?
(hold "Ctrl" key to select multiple options at one time):
<br/><br/>
<BR>
<BR>
<select name="skills">
<OPTION value="Single" selected="selected">Single</OPTION>
<OPTION value="With partner">With partner</OPTION>
<OPTION value="Separated from partner">Separated from partner</OPTION>
<OPTION value="Partner died">Partner died</OPTION>
<OPTION value="DK">DK</OPTION>
</select>
<BR>
<BR>
<INPUT TYPE="button" VALUE="search" onClick="get2();">
</form>
<div id="gender"></div>
<div id="skills"></div>
DATA2.PHP
<?php
mysql_connect("localhost", "root", "");
mysql_select_db("grist");
$gender = $_POST['gender'];
$skills = $_POST['skills'];
if (($gender==NULL) && ($skills==NULL)) {
echo"please enter gender and skills!";
}
else
{
$dob4 = mysql_query("SELECT * FROM patients WHERE gender='$gender' AND relationship_status='$skills'");
//$dob_num_rows = mysql_num_rows($dob);
while($row4 = mysql_fetch_array($dob4)){
$a=$row4['patient_id'];
$b=$row4['gender'];
$c=$row4['dob'];
echo "<b>Patient:</b> $a";
echo "<b>Patient:</b> $b";
echo "<b>Patient:</b> $c";
}
}
?>
您正在向'data2.php'發出兩個帖子,每次只傳遞一個變量。你需要同時通過兩者。 – 2013-03-25 19:04:30
我該怎麼做?謝謝 – shieldcy 2013-03-25 19:06:42
另外,不要使用'mysql_query()'[它已棄用](http://php.net/manual/en/function.mysql-query.php)並且容易受到[SQL注入](http:// en.wikipedia.org/wiki/SQL_injection)。我可以輕鬆地製作併發送一個自定義POST到您的表單處理器,其中包含'$ gender ='或'1'='1' - ''(將返回您的整個患者列表)或$ gender ='; DELETE * FROM耐心; - '你會有一個糟糕的一天。改爲使用Mysqli或PDO(鏈接在上面的頁面中)。 – msanford 2013-03-25 19:10:47