这是我的问题:我想使用PHP表单进行数据库搜索。我有我的表单,即使有一些空字段,我也希望它能工作(就像你填写的字段越多,得到的答案就越具体)。我使用Jquery首先从我的表单中获取值,然后通过$.post将其发送到我的实际php文件,该文件与数据库连接并进行搜索。我只是给空字段一些特定的值,这些值在我的php文件中被识别为"空"字段,这样我就可以进行正确的sql查询。问题是我发现$_post变量是空的,即使Jquesry脚本中的变量设置正确。我在其他情况下使用这种方法,效果很好。我不知道这是否有问题,但我也在使用Jquery将表单加载到主div中。alert(data)函数完成了它的工作:在提示窗口中,我可以看到我的搜索结果,但重新加载profile.php什么都没有。这是我的代码:
形式:
<script type="text/javascript" src="scripts/admin.js"></script>
<form id="form_admin">
<table cellpadding="0" cellspacing="0" width="180">
<p style="line-height: 2cm; ">
<tr>
<td width="50" class="label1">Imię:</td>
<td>
<input id="imie" type="text" value="">
</td>
</tr>
<tr>
<td width="50" class="label1">Nazwisko:</td>
<td>
<input id="nazwisko" type="text" value="">
</td>
</tr>
<tr>
<td width="50" class="label1">Numer telefonu:</td>
<td>
<input id="telefon" type="text" value="">
</td>
</tr>
<tr>
<td align="center" colspan="2">
<input type="button" id="register" value="Pokaż!">
<br>
</td>
</tr>
</p>
</table>
</form>
admin.js
$(document).ready(function () {
$("#register").click(function () {
if ($("#imie").val() != "") {
var imie = $("#imie").val();
} else {
var imie = 'nic';
}
if ($("#nazwisko").val() != "") {
var nazwisko = $("#nazwisko").val();
} else {
var nazwisko = 'nic';
}
if ($("#telefon").val() != "") {
var telefon = $("#telefon").val();
} else {
var telefon = 'nic';
}
$.post("profile.php", {
name: imie,
surname: nazwisko,
telephone: telefon
}, function (data) {
alert(data);
});
$("#main").load('profile.php');
});
});
profile.php
<? php
include('config.php');#logging into database
if (isset($_POST['name']) && isset($_POST['surname']) && isset($_POST['telephone'])) {
$IMIE = $_POST['name'];
$NAZWISKO = $_POST['surname'];
$TELEFON = $_POST['telephone'];#then my sql queries go...
while ($user = mysql_fetch_assoc($select)) {
echo 'ORDER_ID zamówienia: '.$user['ORDERINFO_ID'];
echo 'DATA zamówienia: '.$user['DATE_PLACED'];
}
while ($user = mysql_fetch_assoc($select2)) {
echo 'SUBORDER_ID: '.$user['SUBITEM_ID'];
echo 'Ilość: '.$user['QUANTITY'];
echo 'Koszt zamówienia: '.$user['COST'];
}
} else {
echo "$_POST variables aren't set";
}
?>
编辑:我已经安装了Firebug,并在提交数据后检查了POST profile.php,它包含了预期的所有变量。
尝试将$.post函数更改为:
$.post("profile.php", {
data: "&name=" + imie + "&surname=" + nazwisko + "&telephone=" + telefon
}, function (data) {
alert(data);
});
您的输入没有任何名称
例如:
<input id="imie" type="text" value="" name="imie">