将变量发送到我的 PHP 文件 - 由于某种原因它们为空
我正在将变量发送到我的 PHP 文件以将内容添加到数据库中,但由于某种原因,我的数据库要么被填满空白,要么根本不添加到数据库中。
customer.php
<script type="text/javascript">
function addCustomerFunc(add_LN,add_FN,add_PN,add_DOB)
{
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("show_label").innerHTML=xmlhttp.responseText;
}
}
xmlhttp.open("POST","addCustomer.php",true);
xmlhttp.send("add_LN="+add_LN+"&add_FN="+add_FN+"&add_PN="+add_PN+"&add_DOB="+add_DOB);
}
</script>
<p>Add New Customer:</p>
<div align="center">
<table width="337" border="1">
<tr>
<td width="154"><p>Last Name:</p>
<p>First Name:</p>
<p>Phone Number:</p>
<p>Date of Birth:</p>
<p> </p></td>
<td width="167"><p align="center">
<form>
<input type="text" name="add_LN" id="add_LN" />
<br/><br/>
<input type="text" name="add_FN" id="add_FN" />
<br /><br />
<input type="text" name="add_PN" id="add_PN" />
<br /><br />
<input type="text" name="add_DOB" id="add_DOB" />
<br /><br />
<input type="submit" name="add_Customer" id="New_Customer_Form" value="Add Customer" onClick="addCustomerFunc(this.add_LN, this.add_FN, this.add_PN, this.add_DOB); return false;"/>
</form>
<div id="show_label"/>
</p>
</td>
</tr>
</table>
</div>
在 addCustomer.php 中,
<?php
$username="*****";
$password="*****";
$database="*****";
if (isset ($_POST['add_LN']))
$lastName=$_POST['add_LN'];
else
die("Last Name not passed in POST");
if (isset ($_POST['add_FN']))
$firstName=$_POST['add_FN'];
else
die ("First Name not passed in POST");
if (isset ( $_POST['add_PN']))
$phone=$_POST['add_PN'];
else
die("Phone Number not passed in POST");
if (isset ($_POST['add_DOB']))
$dob=$_POST['add_DOB'];
else
die("Date of Birth not passed in Post");
mysql_connect("dbs4.cpsc.ucalgary.ca",$username,$password);
@mysql_select_db($database) or die( "Unable to select database");
$query = "INSERT INTO customer (last_name, first_name, phone_no, date_of_birth, membership) VALUES ('$lastName', '$firstName', '$phone', '$dob', 'T')";
if (mysql_query($query)){
echo "Thanks";
} else
{
echo "Failed to insert customer into database";
}
mysql_close();
?>
我被告知使用 ISSET 函数,但变量似乎永远不会更改为非空值。 我正在使用 PHP 5.3.5。
编辑:我在 show_label 中不断收到“姓氏未在 POST 中传递”的信息,因为它始终将 add_LN 视为空白。
I am sending variables to my PHP file to add things to the database, but for some reason my database either gets filled with blanks or it just does not add to the database at all.
customer.php
<script type="text/javascript">
function addCustomerFunc(add_LN,add_FN,add_PN,add_DOB)
{
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("show_label").innerHTML=xmlhttp.responseText;
}
}
xmlhttp.open("POST","addCustomer.php",true);
xmlhttp.send("add_LN="+add_LN+"&add_FN="+add_FN+"&add_PN="+add_PN+"&add_DOB="+add_DOB);
}
</script>
<p>Add New Customer:</p>
<div align="center">
<table width="337" border="1">
<tr>
<td width="154"><p>Last Name:</p>
<p>First Name:</p>
<p>Phone Number:</p>
<p>Date of Birth:</p>
<p> </p></td>
<td width="167"><p align="center">
<form>
<input type="text" name="add_LN" id="add_LN" />
<br/><br/>
<input type="text" name="add_FN" id="add_FN" />
<br /><br />
<input type="text" name="add_PN" id="add_PN" />
<br /><br />
<input type="text" name="add_DOB" id="add_DOB" />
<br /><br />
<input type="submit" name="add_Customer" id="New_Customer_Form" value="Add Customer" onClick="addCustomerFunc(this.add_LN, this.add_FN, this.add_PN, this.add_DOB); return false;"/>
</form>
<div id="show_label"/>
</p>
</td>
</tr>
</table>
</div>
In addCustomer.php
<?php
$username="*****";
$password="*****";
$database="*****";
if (isset ($_POST['add_LN']))
$lastName=$_POST['add_LN'];
else
die("Last Name not passed in POST");
if (isset ($_POST['add_FN']))
$firstName=$_POST['add_FN'];
else
die ("First Name not passed in POST");
if (isset ( $_POST['add_PN']))
$phone=$_POST['add_PN'];
else
die("Phone Number not passed in POST");
if (isset ($_POST['add_DOB']))
$dob=$_POST['add_DOB'];
else
die("Date of Birth not passed in Post");
mysql_connect("dbs4.cpsc.ucalgary.ca",$username,$password);
@mysql_select_db($database) or die( "Unable to select database");
$query = "INSERT INTO customer (last_name, first_name, phone_no, date_of_birth, membership) VALUES ('$lastName', '$firstName', '$phone', '$dob', 'T')";
if (mysql_query($query)){
echo "Thanks";
} else
{
echo "Failed to insert customer into database";
}
mysql_close();
?>
I've been told to use the ISSET function, but the variables never seem to change to a non null value.
I am using PHP 5.3.5.
Edit: I keep getting "Last Name not passed in POST" in show_label because it keeps seeing add_LN as a blank.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
“onclick”代码中的
this
值不会是Firefox Tamper Data 插件对于追踪问题非常有用像这样,因为它可以让您在浏览器发送 HTTP 请求时直接检查它。
编辑 - 哦,抱歉;另一个主要问题是您只是获取 DOM 元素,但您需要的是“value”属性。例如:
The
this
value in your "onclick" code is not going to be the<form>
, it's going to be the<input>
tag itself. Try changing those tothis.parent.add_LN
etc. Alternatively, since you've given all the inputs "id" values, you could dispense with the argument list and just have the handler function find the inputs with "document.getElementById()" for each.The Firefox Tamper Data plugin is really useful for tracking down problems like this, as it lets you directly examine the HTTP request as the browser sends it.
edit — oh, sorry; another major problem is that you're just grabbing the DOM element, but what you need is the "value" attribute. For example: