我有以下代码,其中包含一个在提交时调用javascript函数的表单。
<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>
PHP脚本运行正常,但此表单未调用JavaScript函数createresult()
。
答案 0 :(得分:3)
https://developer.mozilla.org/en-US/docs/Web/API/GlobalEventHandlers.onsubmit
当用户点击<form>
标记内的提交按钮时,就会发生提交事件。现在您使用<div>
标记。
如果您想使用<div>
代码,而不是使用onsubmit
,请在提交时使用onclick
<button type="submit" class="btn btn-primary" name="submit" onclick="createresult()">Submit</button>
答案 1 :(得分:2)
变化:
<div class="form" role="form" onsubmit="createresult()">
// your code
</div>
要
<form onsubmit="createresult()">
// your code
</form>