954,561 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

How do I get a drop down jump menu to submit?

I have a drop down menu the is populated with event names from a DB. What I'd like to do is have the form submit after click on one of the event names in the drop down list. At the moment I have a submit button for this.

I don't even know if this is possible. From the searching that I've done so far it doesn't look like it isn't. Let's hope I'm wrong :P

Venom Rush
Posting Whiz
353 posts since Oct 2007
Reputation Points: 31
Solved Threads: 2
 

I used a bit of JavaScript the first time I needed this, I'm not sure if there is a way to do this with PHP.

Put this javascript function in the tags of your file.

<SCRIPT LANGUAGE=javascript>
<!--
function OnChange(dropdown){
        //the item that has been selected
	var myindex  = dropdown.selectedIndex
        //the value of the item that has been selected
	var SelValue = dropdown.options[myindex].value
        //the URL of the page as well as the $_GET variable used to store the value of the 
        //selected item
	var baseURL  = "/filename.php?selectBox="
        //sends the browser to "/filename.php?selectBox=(whatever was selected)
	top.location.href = baseURL +SelValue;
}
//-->
</SCRIPT>

----------------------------------------------------------
FOR THE SELECTION LIST:

<select name="selectBox" id="selectBox" onchange="OnChange(this.form.selectBox);">


Whatever the value of the item selected was it will be in the $_GET['selectBox'] variable. You should change the names to match whatever you're doing.


EXAMPLE:

<html>
<head>
<SCRIPT LANGUAGE=javascript>
<!--
function OnChange(dropdown){
	var myindex  = dropdown.selectedIndex
	var SelValue = dropdown.options[myindex].value
	var baseURL  = "/phptest.php?selectBox="
	top.location.href = baseURL +SelValue;
}
//-->
</SCRIPT>
</head>
<body>
<form name="test" action="phptest.php" method="post">
<select name="selectBox" id="selectBox" onchange="OnChange(this.form.selectBox);">
<option value="" selected> </option>
<option value=1>1</option>
<option value=2>2</option>
</select>
</form>
<?php
echo $_GET['selectBox'];

?>
</body>
</html>
jaymathew
Newbie Poster
4 posts since Aug 2007
Reputation Points: 10
Solved Threads: 1
 

why don't you just use the get method in the form.

<form action="something.php" method="get">


you could just use this.form.submit()

<select name="selectBox" id="selectBox" onchange="this.form.submit()">
<option></option>
<option></option>
<option></option>
</select>
</form>
kkeith29
Nearly a Posting Virtuoso
1,357 posts since Jun 2007
Reputation Points: 235
Solved Threads: 194
 

^yea do that, much better idea.

jaymathew
Newbie Poster
4 posts since Aug 2007
Reputation Points: 10
Solved Threads: 1
 

Thanks, i'll try those out and let you know ;)

Venom Rush
Posting Whiz
353 posts since Oct 2007
Reputation Points: 31
Solved Threads: 2
 

Got side tracked with some other projects. The craziness is now over and I can get back to this problem. KKeith29, would the part where you have "action=something.php" be a php file with the code that retrieves info from the database? That's essentially what the purpose of the page is.

Basically what I'm aiming for is this. The user selects an item from the drop-down list which gets retrieved from the MySQL database and is then displayed in the appropriate input form fields for editing and resubmission.

Venom Rush
Posting Whiz
353 posts since Oct 2007
Reputation Points: 31
Solved Threads: 2
 

Hello,

put: action=""<?php print $_SERVER['PHP_SELF']; ?>" on the form

put this on the select box: onchange="formName.submit();"

then in your php code (on same page) do:

<?php
if (isset($_POST['select_box_name']))
{
//do as u want
}
?>


see here: http://www.thescripts.com/forum/thread165518.html

Good luck!

freequensee
Newbie Poster
1 post since Dec 2007
Reputation Points: 11
Solved Threads: 1
 

Hello,

put: action=""<?php print $_SERVER['PHP_SELF']; ?>" on the form

put this on the select box: onchange="formName.submit();"

then in your php code (on same page) do:

<?php if (isset($_POST['select_box_name'])) { //do as u want } ?>


Got side tracked once again but funny enough I needed this functionality with the current project I'm busy with.

Hi freequensee, thanks for your post. The onchange event I had to have without the form name. As a side note freequensee, u can have your form action simply as:

action="<?= $_SERVER['PHP_SELF'] ?>"
Venom Rush
Posting Whiz
353 posts since Oct 2007
Reputation Points: 31
Solved Threads: 2
 

listen kkeith... he is good! ;)

fatihpiristine
Posting Whiz in Training
283 posts since Sep 2007
Reputation Points: 6
Solved Threads: 19
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You