开发者

Sending Variables to my PHP File - They are null for some reason

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>&nbsp;</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开发者_C百科']))
    $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.


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 to this.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:

var lastName = document.getElementById("add_LN").value;
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜