我正在尝试将多个值传递给javascript函数“get2”,然后调用“data2.php”文件,以便根据下拉列表/文本框中提交的值从数据库中检索数据。两者分开工作但不在一起工作,我得到的错误是“Undefined index”。有人可以帮帮我吗?
<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";
}
}
?>
答案 0 :(得分:1)
这是使用POST请求向页面发送两个值的方法:
<script type="text/javascript">
function get2() {
$.post('data2.php', { gender: form2.gender.value,
skills: form2.skills.value },
function(output) {
$('#gender').html(output).show();
$('#skills').html(output).show();
}
);
}
</script>
请注意,现在这会更新性别和技能元素。我不确定你想做什么; data2.php需要/允许使用这两个参数。
您还应该清理用户输入:目前您很容易受到SQL注入攻击。使用允许使用预准备语句(如PDO)的数据库客户端库会更好。您正在使用的旧mysql库已被删除,并且将在某些时候从PHP中删除。