Page 1 of 1
php dynamic select boxes
Posted: Sun Oct 21, 2007 2:19 pm
by FireElement
feyd | Please use Code: Select all
and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read: [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]
Alright... here goes.
Basically I was wondering if anyone new how to do something on result of somet been selected in a select box but not using javascript but php. As I want to call the sql.
hmmm an example might be better.
[syntax="html"]
so basically depending on whether either one is selected I want the other box to show but I want it to do it in real time.
<select name='sport'>
<option value="null" selected="selected">-select-</option>
<option value="1">football</option>
<option value="2">rugby</option>
</select>
If rugby selected display this box
<select name='rugby'>
<option value="null" selected="selected">-select-</option>
<option value="1">league</option>
<option value="2">Union</option>
</select>
if Football selected selected this box
<select name='football'>
<option value="null" selected="selected">-select-</option>
<option value="1">American</option>
<option value="2">English(soccor)</option>
</select>
So basically I was wondering if any one new how to do this! Thanks!!
It needs to be in php though because am going to use sql to input the options. and based on there selection put the options in a new box. If I new how to just change the text box based on the selection using php I could program the whole code with out a problem just need to know how to do this thanks!!!
feyd | Please use[/syntax]Code: Select all
and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read: [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]
Posted: Sun Oct 21, 2007 4:13 pm
by neophyte
The code might look something like this:
Code: Select all
if(!empty($_POST))
{
if(array_key_exists('field_name', $_POST))
{
if($_POST['field_name'] == 'your_value'))
{
//Do something -- redirect -- return message -- validate
}
}
}
Hope that helps.
Posted: Sun Oct 21, 2007 4:23 pm
by gregsometimes
You can't do this with PHP in, how you say it, "real time". PHP does not have functionality similar to JavaScript's "onClick" or "onSelect". Is there a good reason why you don't want to use JavaScript? What you're looking for is an AJAX implementation.
Posted: Sun Oct 21, 2007 4:41 pm
by FireElement
feyd | Please use Code: Select all
and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read: [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]
[quote="gregsometimes"]You can't do this with PHP in, how you say it, "real time". PHP does not have functionality similar to JavaScript's "onClick" or "onSelect". Is there a good reason why you don't want to use JavaScript? What you're looking for is an AJAX implementation.[/quote]
Yeah I kinda figured that php is server based and there for the only way to do what I would want to do would be to refresh page this is not very user friendly and may end up with lose od data.
There is not a good reason why I cant use javascript. I just was not sure how to call sql using javascript. Basically I have the following code. On select of select box item I want to affect the next select box and call the relivant info using an if statment to put the right stuff in to the box.
basically I have sql database as follows:
[b]id : category[/b]
1 : rugby
2 : football
then
[b]id : category : category under[/b]
1 : League : rugby
2 : Union : rugby
3 : American : football
4 : English: football
when they select football from the select list i want the next select box to come up and input american and english.
This is so later I can add a form where they can add categories and sub catebories and the form is updated instanly with out me having to mess about!
Below is my php code for the first box if I can some how work out a way by refreshing page and posting what has been selected I could probable work with that. But what worries me in doing this is losing data and if user entered half the form they will be <span style='color:blue' title='I'm naughty, are you naughty?'>smurf</span> if they have to do it again and may just leave. Any sugestions?
Code: Select all
<select name="tip">
<option value="null" selected="selected">-select-</option>
<?php
//select from table 3
$sql3="SELECT * FROM $tbl_name_3 ORDER BY category";
$result3=mysql_query($sql3);
// Start looping table row 1
while($rows=mysql_fetch_array($result3)){
$category=$rows['category'];
echo "<option value="$category">$category</option>";
}// Exit
?>
</select>
feyd | Please use Code: Select all
and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read: [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]
Posted: Sun Oct 21, 2007 5:05 pm
by gregsometimes
javascript and sql are usually not used in the same sentence, unless to indicate that there is just no straight communication interface between the two.
This problem you are having is exactly why we now have ajax. If you've studied ajax for at least 1 hour you'd know that ajax is... Asynchronous Javascript And XML.
You will not be able to directly communicate with a MySQL database through javascript alone without having to refresh a page, or do a tricky redirect, or.. have an iframe (width=0px and height=0px) controlled by javascript which would refresh your php-script page. With AJAX, you should be able to do anything you could do with an iframe. I once wrote an iframe-js-php implementation for reading and parsing a myspace profile page, getting about me, interests and other fields separated into text areas, without refreshing the main page. But remember that with iframes you get less security. That's just a more dangerous approach.
You might find this page useful:
http://www.tizag.com/ajaxTutorial/ajax- ... tabase.php
Posted: Sun Oct 21, 2007 5:29 pm
by FireElement
gregsometimes wrote:javascript and sql are usually not used in the same sentence, unless to indicate that there is just no straight communication interface between the two.
This problem you are having is exactly why we now have ajax. If you've studied ajax for at least 1 hour you'd know that ajax is... Asynchronous Javascript And XML.
You will not be able to directly communicate with a MySQL database through javascript alone without having to refresh a page, or do a tricky redirect, or.. have an iframe (width=0px and height=0px) controlled by javascript which would refresh your php-script page. With AJAX, you should be able to do anything you could do with an iframe. I once wrote an iframe-js-php implementation for reading and parsing a myspace profile page, getting about me, interests and other fields separated into text areas, without refreshing the main page. But remember that with iframes you get less security. That's just a more dangerous approach.
You might find this page useful:
http://www.tizag.com/ajaxTutorial/ajax- ... tabase.php
sounds like I may be

.... hmmm
This may have got more complicated then I though.... The last thing I want to do is use iframes. one there not always compatable with all browsers and two probable not very secure at all... I may look in to onchange refresh page with javascript and then try to post the selection back to the same page some how, along with all the data entered by the user and post it back in to the relivant fields. Just need to work out away to auto post the data on select... hmmm think this sounds best option... any sugestions?
Posted: Sun Oct 21, 2007 5:32 pm
by gregsometimes
you can also do this with Flash Movie <-> JavaScript communication. But that's also no easy task.