471,066 Members | 1,335 Online

# Populate table at selection

I have a combo box (select) and when the user selects a value I want to
populate an HTML table from DB depending on the value selected. Whats the
easiest way to achieve it. I've searched google and the examples require
java script. Is there a way to do this with only PHP. I had the impression
you could do anything with PHP. Infact there is a book titled 'How to do
every thing in PHP'!
--
When you argue with a fool, chances are he's doing the same
Jun 2 '08 #1
8 1707
freelance71 wrote:
I have a combo box (select) and when the user selects a value I want to
populate an HTML table from DB depending on the value selected. Whats the
easiest way to achieve it. I've searched google and the examples require
java script. Is there a way to do this with only PHP. I had the impression
you could do anything with PHP. Infact there is a book titled 'How to do
every thing in PHP'!
--
When you argue with a fool, chances are he's doing the same
PHP is server side. A select box is client side. You need a client
side routine - i.e. javascript.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
js*******@attglobal.net
==================

Jun 2 '08 #2

"Jerry Stuckle" <js*******@attglobal.netwrote in message
news:m5******************************@comcast.com. ..
freelance71 wrote:
>I have a combo box (select) and when the user selects a value I want to
populate an HTML table from DB depending on the value selected. Whats the
easiest way to achieve it. I've searched google and the examples require
java script. Is there a way to do this with only PHP. I had the
impression you could do anything with PHP. Infact there is a book titled
'How to do every thing in PHP'!
--
When you argue with a fool, chances are he's doing the same

PHP is server side. A select box is client side. You need a client side
routine - i.e. javascript.

--
Thanks.
Can a PHP script access/get the default value of a Combo box when the page
Jun 2 '08 #3
On Tue, 22 Apr 2008 11:08:06 +0500, in comp.lang.php "freelance71"
<f9******@yahoo.com>
<fu**********@registered.motzarella.orgwrote:
>|
| "Jerry Stuckle" <js*******@attglobal.netwrote in message
| news:m5******************************@comcast.com. ..
| freelance71 wrote:
| >I have a combo box (select) and when the user selects a value I want to
| >populate an HTML table from DB depending on the value selected. Whats the
| >easiest way to achieve it. I've searched google and the examples require
| >java script. Is there a way to do this with only PHP. I had the
| >impression you could do anything with PHP. Infact there is a book titled
| >'How to do every thing in PHP'!
| >--
| >When you argue with a fool, chances are he's doing the same
| >
| PHP is server side. A select box is client side. You need a client side
| routine - i.e. javascript.
| >
| --
|
| Thanks.
| Can a PHP script access/get the default value of a Combo box when the page
If the default value is obtained from a database and php sets the
value then yes.

If the default value is within the HTML page then you will need to
parse the page (within php) to get the value. This needs to be done
before the page is sent to the browser.

Why don't you want to use javascript as what you've described is an
ideal candidate for AJAX.
-- -------------------------------------------------------------
-- -------------------------------------------------------------
Jun 2 '08 #4
freelance71 wrote:
"Jerry Stuckle" <js*******@attglobal.netwrote in message
news:m5******************************@comcast.com. ..
>freelance71 wrote:
>>I have a combo box (select) and when the user selects a value I want to
populate an HTML table from DB depending on the value selected. Whats the
easiest way to achieve it. I've searched google and the examples require
java script. Is there a way to do this with only PHP. I had the
impression you could do anything with PHP. Infact there is a book titled
'How to do every thing in PHP'!
--
When you argue with a fool, chances are he's doing the same
PHP is server side. A select box is client side. You need a client side
routine - i.e. javascript.

--

Thanks.
Can a PHP script access/get the default value of a Combo box when the page
If PHP is setting the value in the combobox, yes.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
js*******@attglobal.net
==================

Jun 2 '08 #5
On Apr 22, 7:23 am, Jerry Stuckle <jstuck...@attglobal.netwrote:
freelance71 wrote:
"Jerry Stuckle" <jstuck...@attglobal.netwrote in message
news:m5******************************@comcast.com. ..
freelance71 wrote:
I have a combo box (select) and when the user selects a value I want to
populate an HTML table from DB depending on the value selected. Whats the
easiest way to achieve it.
You can do it all with PHP if you'd like, although it might get
messy .
You've got 3 ways to go:
1) The Wrong Way.
Bake everything into the file using php. This means creating the
select form with php, and also dynamically creating javascript with
php. Because you already know the only possibly options the user might
select, you just load the different table data though php/mysql and
shove them into javascript variables (within php. i.e. <?php echo
"<script type=\text/javascript\"";.....) You will be heading down the
rabbit hole this way.

2) The Old Way:
Create the form with HTML. Set the form action to a PHP page (It can
even be the same page that displays the form)
The form sends a GET request to the PHP Page, you take the value the
user has chosen with $userchoice=$_GET['userchoice']; Work your magic
with mysql, and create a table with the data and display the data with
a inline php variable.

3) The Right Way:
Use Ajax:
Create a div specifically for the table
Set one of the SELECT events to a custom javascript handler.
something like <inputtype = "select" id="myselect"
onmouseup="javascript:myHandler(this.form);"
then u make a javascript function
function myHandler(form)
{
myselect = form.select.value;
}
Then you use your favorite Ajax framework to send the value to the
server (via ajax) you will need to write a custom callback handler for
the data that comes back from php.
function callBackAjaxHandler(response)
{
document.getElementById('mytablediv').innerHTML = response;
}
Jun 2 '08 #6
On Tue, 22 Apr 2008 10:11:31 -0700 (PDT), venti wrote:
On Apr 22, 7:23 am, Jerry Stuckle <jstuck...@attglobal.netwrote:
>freelance71 wrote:
"Jerry Stuckle" <jstuck...@attglobal.netwrote in message
news:m5******************************@comcast.com ...
freelance71 wrote:
I have a combo box (select) and when the user selects a value I want to
populate an HTML table from DB depending on the value selected. Whats the
easiest way to achieve it.

You can do it all with PHP if you'd like, although it might get
messy .
You've got 3 ways to go:
1) The Wrong Way.
Bake everything into the file using php. This means creating the
select form with php, and also dynamically creating javascript with
php. Because you already know the only possibly options the user might
select, you just load the different table data though php/mysql and
shove them into javascript variables (within php. i.e. <?php echo
"<script type=\text/javascript\"";.....) You will be heading down the
rabbit hole this way.

2) The Old Way:
Create the form with HTML. Set the form action to a PHP page (It can
even be the same page that displays the form)
The form sends a GET request to the PHP Page, you take the value the
user has chosen with $userchoice=$_GET['userchoice']; Work your magic
with mysql, and create a table with the data and display the data with
a inline php variable.

3) The Right Way:
Use Ajax:
Create a div specifically for the table
Set one of the SELECT events to a custom javascript handler.
something like <inputtype = "select" id="myselect"
onmouseup="javascript:myHandler(this.form);"
then u make a javascript function
function myHandler(form)
{
myselect = form.select.value;
}
Then you use your favorite Ajax framework to send the value to the
server (via ajax) you will need to write a custom callback handler for
the data that comes back from php.
function callBackAjaxHandler(response)
{
document.getElementById('mytablediv').innerHTML = response;
}
#3 looks a lot messier than #2. Never trust the client.

--
Mares eat oats, and does eat oats, and little lambs eat ivy,
A kid will eat ivy too, wouldn't you?
Jun 2 '08 #7
#3 looks a lot messier than #2. Never trust the client.
Well, 3 requires more code up front, but it saves you having full
round trips with a page refresh. The payoff for Ajax is that the
headers remain the same, and only the div containing the table is
changed through the DOM.
As for trusting the client, of course, don't. This means, among other
things, client and (more important) server-side parsing for sql
injection. But ultimately I don't see any real difference in client
based attacks one way or the other.
Jun 2 '08 #8
Peter H. Coffin wrote:
On Tue, 22 Apr 2008 10:11:31 -0700 (PDT), venti wrote:
>On Apr 22, 7:23 am, Jerry Stuckle <jstuck...@attglobal.netwrote:
>>freelance71 wrote:

"Jerry Stuckle" <jstuck...@attglobal.netwrote in message
news:m5******************************@comcast.c om...

freelance71 wrote:
>
>I have a combo box (select) and when the user selects a value I want to
>populate an HTML table from DB depending on the value selected. Whats the
>easiest way to achieve it.
>>
You can do it all with PHP if you'd like, although it might get
messy .
You've got 3 ways to go:
1) The Wrong Way.
Bake everything into the file using php. This means creating the
select form with php, and also dynamically creating javascript with
php. Because you already know the only possibly options the user might
select, you just load the different table data though php/mysql and
shove them into javascript variables (within php. i.e. <?php echo
"<script type=\text/javascript\"";.....) You will be heading down the
rabbit hole this way.

2) The Old Way:
Create the form with HTML. Set the form action to a PHP page (It can
even be the same page that displays the form)
The form sends a GET request to the PHP Page, you take the value the
user has chosen with $userchoice=$_GET['userchoice']; Work your magic
with mysql, and create a table with the data and display the data with
a inline php variable.

3) The Right Way:
Use Ajax:
Create a div specifically for the table
Set one of the SELECT events to a custom javascript handler.
something like <inputtype = "select" id="myselect"
onmouseup="javascript:myHandler(this.form);"
then u make a javascript function
function myHandler(form)
{
myselect = form.select.value;
}
Then you use your favorite Ajax framework to send the value to the
server (via ajax) you will need to write a custom callback handler for
the data that comes back from php.
function callBackAjaxHandler(response)
{
document.getElementById('mytablediv').innerHTML = response;
}

#3 looks a lot messier than #2. Never trust the client.

You can always make the Javascript fire from an event on a hyperlink
that would reload the entire page and accomplish the same thing for
those who do not have JS enabled.

--
*****************************
Chuck Anderson • Boulder, CO
http://www.CycleTourist.com
Nothing he's got he really needs
Twenty first century schizoid man.
***********************************

Jun 2 '08 #9

### This discussion thread is closed

Replies have been disabled for this discussion.