Jump to content

How do I assign a form drop down a variable onCHANGE


kpetsche20

Recommended Posts

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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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>

Link to comment
Share on other sites

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.

 

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.