Dynamic Form Response

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
jamgood96
Forum Newbie
Posts: 12
Joined: Fri Jan 21, 2011 12:19 am

Dynamic Form Response

Post 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>";

?>
Last edited by jamgood96 on Fri Jan 21, 2011 12:27 pm, edited 1 time in total.
User avatar
social_experiment
DevNet Master
Posts: 2793
Joined: Sun Feb 15, 2009 11:08 am
Location: .za

Re: Dynamic Form Response

Post 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.
“Don’t worry if it doesn’t work right. If everything did, you’d be out of a job.” - Mosher’s Law of Software Engineering
brothaofdes
Forum Newbie
Posts: 21
Joined: Thu Jan 20, 2011 10:05 am

Re: Dynamic Form Response

Post 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.
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Re: Dynamic Form Response

Post by John Cartwright »

You know you can post code as text here, no need to upload a massive image :P
jamgood96
Forum Newbie
Posts: 12
Joined: Fri Jan 21, 2011 12:19 am

Re: Dynamic Form Response

Post 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!!! :-)
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Re: Dynamic Form Response

Post 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..
jamgood96
Forum Newbie
Posts: 12
Joined: Fri Jan 21, 2011 12:19 am

Re: Dynamic Form Response

Post 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!
Post Reply