I have the following code which consists of a form whose on submit calls a javascript function.
<div class="form" role="form" onsubmit="createresult()">
<fieldset>
<?php
$subject = $_GET["sub"];
$array = mysqli_query($con, "Select * from questions where subject='$subject'");
$j =1;
while($row = mysqli_fetch_array($array))
{
echo "
<div class=\"form-group\"><h5>$row[question]</h5>";
for($i=1;$i<5;$i++)
{
$opt1 = "option".$i;
$opt = $row[$opt1];
$name = "question" ."$j";
echo "
<label class=\"radio-inline\">
<input type=\"radio\" name=$name required>$opt</label>";
}
echo "</div>";
$j++;
}
?>
<button type="submit" class="btn btn-primary" name="submit">Submit</button>
<button type="reset" class="btn btn-primary">Reset</button>
</fieldset>
</div>
The PHP script is working fine but this form is not calling the JavaScript function createresult()
.
I have the following code which consists of a form whose on submit calls a javascript function.
<div class="form" role="form" onsubmit="createresult()">
<fieldset>
<?php
$subject = $_GET["sub"];
$array = mysqli_query($con, "Select * from questions where subject='$subject'");
$j =1;
while($row = mysqli_fetch_array($array))
{
echo "
<div class=\"form-group\"><h5>$row[question]</h5>";
for($i=1;$i<5;$i++)
{
$opt1 = "option".$i;
$opt = $row[$opt1];
$name = "question" ."$j";
echo "
<label class=\"radio-inline\">
<input type=\"radio\" name=$name required>$opt</label>";
}
echo "</div>";
$j++;
}
?>
<button type="submit" class="btn btn-primary" name="submit">Submit</button>
<button type="reset" class="btn btn-primary">Reset</button>
</fieldset>
</div>
The PHP script is working fine but this form is not calling the JavaScript function createresult()
.
- why not use a <form> tag ? – Hacketo Commented Dec 22, 2014 at 10:41
2 Answers
Reset to default 3https://developer.mozilla/en-US/docs/Web/API/GlobalEventHandlers.onsubmit
The submit event happens when a user clicks a submit button inside a <form>
tag. Right now you use a <div>
tag.
If you would like to use the <div>
tag, instead of using onsubmit
, use onclick
on the submit
<button type="submit" class="btn btn-primary" name="submit" onclick="createresult()">Submit</button>
Change:
<div class="form" role="form" onsubmit="createresult()">
// your code
</div>
To
<form onsubmit="createresult()">
// your code
</form>