Day 14: Submit form without submit button

Sometime it’s require to submit the form on server without submit button, like on dropdown change. So this can be done with the help of JavaScript.

I am taking “tbstudent” table, that we have created on “day12”.

Program 41: Submit form through JavaScript

<?php
$con = mysql_connect(“localhost”,”root”,””);
mysql_select_db(“mydb”,$con);
if(isset($_REQUEST[‘drpEmp’])) {
$selectedId = $_REQUEST[‘drpEmp’];
$query = “select * from tbemp where id = “.$selectedId;
$selectedData = mysql_query($query);
$selectedEmp = mysql_fetch_array($selectedData);
}
$stdQuery = “select * from tbemp”;
$stdData = mysql_query($stdQuery);
?>
<html>
<body>
<form method=”post”>
Select Employee :
<select name=”drpEmp” onChange=”this.form.submit()”>
<option value=””>–Select–</option>
<?php
$sel = “”;
while($recEmp = mysql_fetch_array($stdData)) {
if($recEmp[‘id’] == $selectedId) {
$sel = “selected”;
} else {
$sel = “”;
}
?>
<option <?php echo $sel; ?> value=”<?php echo $recEmp[‘id’]; ?>”><?php echo $recEmp[‘name’]; ?></option>
<?php } ?>
</select>
<?php if(!empty($selectedEmp)) { ?>
<p> <strong>Name :</strong> <?php echo $selectedEmp[‘name’]; ?> </p>
<p> <strong>Salary :</strong> <?php echo $selectedEmp[‘salary’]; ?> </p>
<p> <strong>City :</strong> <?php echo $selectedEmp[‘city’]; ?> </p>
<?php } ?>
</form>
</body>
</html>

Leave a Reply