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

Recommended Answers

All 8 Replies

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 <head> 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 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>

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>

^yea do that, much better idea.

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

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.

Hello,

put: action=""<?php print $_SERVER; ?>" 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))
{
//do as u want
}
?>

see here

Good luck!

commented: A good post. Only had to change one thing from one of his/her suggestions +1

Hello,

put: action=""<?php print $_SERVER; ?>" 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))
{
//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'] ?>"
Member Avatar for fatihpiristine

listen kkeith... he is good! ;)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.