Page 1 of 1

Dynamic Form Response

Posted: Fri Jan 21, 2011 12:27 am
by jamgood96
I'm incredibly new to PHP and MySQL. I've started playing with creating a database of customers.

What I've got thus far is a very basic page that displays a customers first and last name taken from the database and displayed in a form list to select from. What I want to have happen is that when a customer's name is selected, their information such as address and contact information will display below. I've got an onClick command built into the code that currently displays a pop-up message and I'm thinking that there might be a way to use something similar to trigger their information to be displayed below.

Hopefully I've given a clear enough explanation. Thanks in advance for any help or guidance!

Code: Select all

<?php // formTest.php
require_once '../connections/login.php';
$db_server = mysql_connect($db_hostname, $db_username, $db_password);
if (!$db_server) die("Unable to connect to MySQL: " . mysql_error());
mysql_select_db($db_database, $db_server)
    or die("Unable to select database: " . mysql_error());

$query = "SELECT * FROM customers ORDER BY last_name";

$result = mysql_query($query);
if (!$result) die ("Database access failed: " . mysql_error());

echo "Client<br /><SELECT NAME=customers SIZE=10 STYLE=width:175>";

while($nt=mysql_fetch_array($result))
{
    echo <<<_END
    <OPTION VALUE="$nt[cust_id]" onClick="alert('Hello, $nt[first_name] $nt[last_name]');">$nt[last_name], $nt[first_name]</OPTION>
_END;
}
echo "</SELECT>";

?>

Re: Dynamic Form Response

Posted: Fri Jan 21, 2011 1:32 am
by social_experiment
jamgood96 wrote:I've got an onClick command built into the code that currently displays a pop-up message and I'm thinking that there might be a way to use something similar to trigger their information to be displayed below.
You probably don't want to click a submit button so AJAX is your best option. I don't know a lot of javascript but you will probably create the function then use something like onFocus or onBlur to display the information.

Re: Dynamic Form Response

Posted: Fri Jan 21, 2011 10:03 am
by brothaofdes
Since PHP is server side scripting (nothing runs in the actual users browser), unless you want to reprocess the entire page, you will need to use client side scripting (yes, JAVASCRIPT or something). This is relatively easy, but you will need to do some research in that area.

Re: Dynamic Form Response

Posted: Fri Jan 21, 2011 10:04 am
by John Cartwright
You know you can post code as text here, no need to upload a massive image :P

Re: Dynamic Form Response

Posted: Fri Jan 21, 2011 10:46 am
by jamgood96
brothaofdes wrote:Since PHP is server side scripting (nothing runs in the actual users browser), unless you want to reprocess the entire page, you will need to use client side scripting (yes, JAVASCRIPT or something). This is relatively easy, but you will need to do some research in that area.
I kind of thought that might be the case. I haven't gotten to that chapter yet on Javascript.
John Cartwright wrote:You know you can post code as text here, no need to upload a massive image
But I spent a lot of trime creating that image!!! :-)

Re: Dynamic Form Response

Posted: Fri Jan 21, 2011 10:48 am
by John Cartwright
jamgood96 wrote:
John Cartwright wrote:You know you can post code as text here, no need to upload a massive image
But I spent a lot of trime creating that image!!! :-)
A lot of our users come from low-bandwidth connection, so it would be in both your interest and their interest to use text ;) I personally wouldn't touch anyone's code if they gave it to me as an image.. just saying..

Re: Dynamic Form Response

Posted: Fri Jan 21, 2011 10:56 am
by jamgood96
John Cartwright wrote:
jamgood96 wrote:
John Cartwright wrote:You know you can post code as text here, no need to upload a massive image
But I spent a lot of trime creating that image!!! :-)
A lot of our users come from low-bandwidth connection, so it would be in both your interest and their interest to use text ;) I personally wouldn't touch anyone's code if they gave it to me as an image.. just saying..
Good to know, thanks!