2

I want to create a dropdown list in a form and be able to submit/process it without pressing the submit button. I have

$cquery = 'SELECT * FROM tcat ORDER BY id ASC';
$cresult = mysql_query($cquery, $connection);
if(!$cresult){echo 'no result' . mysql_error();}
while($crow = mysql_fetch_array($cresult))
{echo $crow['cat'] . '</option><option>';}
?>

I would like to know if there is a way to make the default that is shown a different value aside the first or the last

1
  • I'm assuming you want to auto-submit when the dropdown changes? You could use javascript & add onchange="this.form.submit()" to your select element. You can't do that with PHP. Commented Apr 1, 2014 at 13:02

3 Answers 3

3

even simpler, this should work too!

<select onchange="submit();">
  <options ... >
</select>
Sign up to request clarification or add additional context in comments.

Comments

2

To submit a form without having the user press a submit button, you can use JavaScript:

document.getElementById("form_id").submit();

To set a default selected option, you can do this:

<option selected="selected">Category Name</option>

Note that PHP will not auto-submit a form for you as it is a server-side language.

Comments

2

You have to use html and javascript and change your script to generate them accordingly:

<select name="aName" onChange="document.getElementById('yourFormId').submit();">
...
</select>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.