2.22. Break Neck's PHP scriptLet'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.
Remember, you don't need to understand all this PHP... this is just for bonus credit.
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); } |