kpetsche20 Posted October 21, 2008 Share Posted October 21, 2008 Hello I have a form with 1 dropdown menu. What I"m trying to accomplish is for when someone select a number from the drop down the form box value is automatically assigned a value and it works so that when the value is changed certain code is run. Look at the link below it is a picture or what i am needing help with. http://dollarilla.com/php.png Quote Link to comment https://forums.phpfreaks.com/topic/129365-how-do-i-assign-a-form-drop-down-a-variable-onchange/ Share on other sites More sharing options...
Zane Posted October 21, 2008 Share Posted October 21, 2008 onChange="submit()" Quote Link to comment https://forums.phpfreaks.com/topic/129365-how-do-i-assign-a-form-drop-down-a-variable-onchange/#findComment-670651 Share on other sites More sharing options...
kpetsche20 Posted October 21, 2008 Author Share Posted October 21, 2008 didn't work. Quote Link to comment https://forums.phpfreaks.com/topic/129365-how-do-i-assign-a-form-drop-down-a-variable-onchange/#findComment-670658 Share on other sites More sharing options...
Psycho Posted October 21, 2008 Share Posted October 21, 2008 I think you are confusing server-side code (PHP) and client-side code (JavaScript). JavaScript cannot change the value of $_POST unless you submit the form or do an AJAX request. I've reread your request a couple of times and still not sure what you are trying to accomplish. When the user changes the option in the select list the value is already updated. S9o, just run the function that you want to run onchange() of that field. I *think* you may be wanting to add multiple sets of input fields based upon the select value (since the title is number of players to add). Provide more specifics for better help. Quote Link to comment https://forums.phpfreaks.com/topic/129365-how-do-i-assign-a-form-drop-down-a-variable-onchange/#findComment-670899 Share on other sites More sharing options...
cuboidgraphix Posted October 21, 2008 Share Posted October 21, 2008 I don't know if this is what you want, but I tried writing it for you. Try this.. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>Untitled Document</title> </head> <body> <form name="form" method="POST" action="<?=$PHP_SELF?>"> <select name="number" onchange="this.form.submit();" method="post"> <option value="">Select Number</option> <option value="1">1</option> <option value="2">2</option> <option value="3">3</option> <option value="4">4</option> <option value="5">5</option> <option value="6">6</option> </select> </form> <?PHP $num = $_POST['number']; $number = trim($num); if(!isset($num)){ print "Please select from the menu"; } else{ print $number; } ?> </body> </html> Quote Link to comment https://forums.phpfreaks.com/topic/129365-how-do-i-assign-a-form-drop-down-a-variable-onchange/#findComment-671256 Share on other sites More sharing options...
cuboidgraphix Posted October 22, 2008 Share Posted October 22, 2008 Slight modification to my script above.. I had forgotten you wanted to run scripts per number.. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>Untitled Document</title> </head> <body> <form name="form" method="POST" action="<?=$PHP_SELF?>"> <select name="number" onchange="this.form.submit();" method="post"> <option value="">Select Number</option> <option value="1">1</option> <option value="2">2</option> <option value="3">3</option> <option value="4">4</option> <option value="5">5</option> <option value="6">6</option> </select> </form> <?PHP $num = $_POST['number']; $number = trim($num); if(!isset($num)){ print "Please select from the menu"; } elseif($number == 1){ // Right here you will write your script 1. print "Script 1 will run here."; } elseif($number == 2){ // Right here you will write your script 2. print "Script 2 will run here."; } elseif($number == 3){ // Right here you will write your script 3. print "Script 3 will run here."; } elseif($number == 4){ // Right here you will write your script 4. print "Script 4 will run here."; } elseif($number == 5){ // Right here you will write your script 5. print "Script 5 will run here."; } elseif($number == 6){ // Right here you will write your script 6. print "Script 6 will run here."; } else{ print "Error!!!"; } ?> </body> </html> OK that's the entire code... I hope I've helped you. Quote Link to comment https://forums.phpfreaks.com/topic/129365-how-do-i-assign-a-form-drop-down-a-variable-onchange/#findComment-671956 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.