PHP Submit on Select

I try not to have a submit button, is there a way that the form can submit when the user selects a value?

<form method="post" action="<?php echo $_SERVER['PHP_SELF'] ?>" > <table class="form"> <select name="category" class="formfield" id="category"> <option value="-1"> Category </option> <?php $sql_contry = "SELECT * FROM category"; $rs_c = mysql_query($sql_contry); while ($row_c = mysql_fetch_array($rs_c)) { echo '<option value="'.$row_c['category'].'">'.$row_c['category'].'</option>'; } ?> </select> </table> </form> 
+6
source share
3 answers

Here is an example

 <form> <select name='myfield' onchange='this.form.submit()'> <option selected>Milk</option> <option>Coffee</option> <option>Tea</option> </select> <noscript><input type="submit" value="Submit"></noscript> </form> 

Using the noscript tag allows browsers that do not support JavaScript,

+20
source

Yes - use javascript:

Html:

 <form id="frm"> <select onchange="onSelectChange();"> <option>1</option> <option>1</option> <select> </form> 

JS:

 function onSelectChange(){ document.getElementById('frm').submit(); } 
+2
source

Just change the code as shown below, I have not checked the code, so some error may occur, for example, capital / small letter. I'm not sure if this is submit () or Submit (), etc.

 <script language="javascript"> function submitForm(){ var val = document.myform.category.value; if(val!=-1){ document.myform.submit(); } } </script> <form method="post" name="myform" action="<?php echo $_SERVER['PHP_SELF'] ?>" > <table class="form"> <select name="category" class="formfield" id="category" onchange="submitForm();"> <option value="-1"> Category </option> <?php $sql_contry = "SELECT * FROM category"; $rs_c = mysql_query($sql_contry); while ($row_c = mysql_fetch_array($rs_c)) { echo '<option value="'.$row_c['category'].'">'.$row_c['category'].'</option>'; } ?> </select> </table> </form> 
0
source

Source: https://habr.com/ru/post/943750/


All Articles