2.22. Break Neck's PHP
script
Let's ask Frank over in the server-side group to write us a PHP script to look up customer addresses. Then you can send the customer's phone number to this script, and get the customer's address as a response.
Team Chat: Getting Some PHP Help
Hey, Frank, you're good with PHP, right?
Yeah, sure. What do you need?
I've got a customer's phone number from an order form.You think you could write a script that gives me that customer's address based on their number?
Sure, just send over the phone number as a request parameter. You want me to return their information as part of a new HTML page?
No, I'm making an asynchronous request, so I won't need anything but the raw data...
...then I can use JavaScript to update my HTML on the fly.
|
In the book's examples, look in the chapter02/breakneck directory, and find the lookupCustomer.php script. This is a PHP script that will run without a database server; keep this file on your computer, or FTP it to your web siteyou'll be using it in just a sec.
|
Remember, you don't need to understand all this PHP... this is just for bonus credit.
|
Here's the script that Frank wrote to take a phone number from the order form, and look up a customer's address:
<?php
// Connect to database
$conn = @mysql_connect("mysql.headfirstlabs.com",
"secret", "really-secret");
if (!$conn)
die("Error connecting to MySQL
: " . mysql_error());Here's all the standard database
connection code.
if (!mysql_select_db("headfirst", $conn))
die("Error selecting Head First database: " . mysql_error());
$phone = preg_replace("/[\. \(\)\-]/", "", $_REQUEST['phone']);This bit of code removes
any special phone characters, like "(", ")", and the "-".
$select = 'SELECT *';
$from = ' FROM hraj_breakneck';
$where = ' WHERE phone = \'' . $phone' . '\'';
$queryResult = @mysql_query($select . $from . $where);Using the phone number you
sent as part of the request, the script looks up the customer's address...
if (!$queryResult)
die('Error retrieving customer from the database.');
while ($row = mysql_fetch_array($queryResult)) {
echo $row['name'] . "\n" .
$row['street1'] . "\n" ....and then echoes the address back to the
requesting program.
$row['city'] . ", " .
$row['state'] . " " .
$row['zipCode'];
}
mysql_close($conn);
?>
* The version of lookupCustomer.php in the examples doesn't use a database, and just returns a random address, so you don't need MySQL running to get Break Neck working on your own computer.
|
Team Chat: Sending the Phone Number
OK, I understand how we're getting the customer's phone number...
...and I get that the first part of that request URL is the PHP script we're calling. But how do we send the script the customer's phone number? You said something about request parameters?
Yeah, you can add a name/value pair to your request URL, and my PHP script can read the phone number from that.
Oh, right, we can just tack that parameter onto the URL, can't we?
function getCustomerInfo() {
var phone = document.getElementById("phone").value;
var urlWe're storing the entire request URL in a JavaScript
variable. = "lookupCustomerThis is the script name; you've already got
this part down cold..php?Use a "?" to separate the script from any
parameters.phoneThen give a name to the parameter, like "phone"...=" +
escapeUsing the escape()
function makes sure none of the tricky characters in the phone number
cause problems for the web browser when it sends this request.(phone...and
put the customer's phone number from the order form here.);
request.open("GET", url, true);
request.onreadystatechange = updatePage;
request.send(null);
}
|