[英]submitting a HTML form using JavaScript
i want to have a button to submit a form, but it doesnt seem to work. 我想有一个按钮来提交表格,但是似乎没有用。 when the button is clicked, nothing happens. 单击该按钮时,没有任何反应。
HTML HTML
<form method="post" action="process.php">
<select name="taskOption" id="taskOption2">
<option value="Select">Please select a site</option>
<option value="http://www.Itslearning.com">Itslearning</option>
<option value="http://www.NDLA.no">NDLA</option>
</select>
</form>
<button onclick="FormSubmit()" class="button button1 button1:hover">Take me there</button>
JavaScript JavaScript的
function FormSubmit() {
document.getElementById("taskOption").submit();
}
PHP PHP
<?php
$option = isset($_POST['taskOption2']) ? $_POST['taskOption2'] : false;
if ($option) {
header("Location: $option");
}
else {
echo "Venligst velg en side.";
exit;
}
?>
Your function is wrong. 您的功能有误。
document.getElementById("taskOption").submit();
// this is wrong //这是错误的
taskOption
it is the id of select box not your forms taskOption
是选择框的ID,而不是您的表单
<form name="F1">// give a name to your form
.....
</form>
function FormSubmit() {
document.F1.submit();
}
or 要么
<form id="formid">// give a id to your form
.....
</form>
function FormSubmit() {
document.getElementById('formid').submit();
}
You must write id of Form, like instead of below: 您必须输入Form的ID,例如,而不是下面的ID:
<form method="post" action="process.php" id="taskOption2">
<select name="taskOption" >
<option value="Select">Please select a site</option>
<option value="http://www.Itslearning.com">Itslearning</option>
<option value="http://www.NDLA.no">NDLA</option>
</select>
</form>
Use: 采用:
Javascript: 使用Javascript:
function FormSubmit() {
document.taskform.submit();
}
HTML: HTML:
<form method="post" name="taskform" action="process.php">
<select name="taskOption" id="taskOption2">
<option value="Select">Please select a site</option>
<option value="http://www.Itslearning.com">Itslearning</option>
<option value="http://www.NDLA.no">NDLA</option>
</select>
</form>
<button onclick="FormSubmit()" class="button button1 button1:hover">Take me there</button>
first you gave a name to form here form1 is name then onclick you call the form 首先,您给表格起一个名字,form1是名称,然后onclick,您调用该表格
<form method="post" name="form1" action="process.php">
<select name="taskOption" id="taskOption2">
<option value="Select">Please select a site</option>
<option value="http://www.Itslearning.com">Itslearning</option>
<option value="http://www.NDLA.no">NDLA</option>
</select>
</form>
<button onclick="FormSubmit()" class="button button1 button1:hover">Take me there</button>
function FormSubmit() {
document.form1.submit();
}
Seems to me you just want a submit button in a form: 在我看来,您只需要表单中的“提交”按钮:
In that case, by far the easiest solution is move the <button>
to inside the form tags, and make its type="submit"
. 在那种情况下,到目前为止,最简单的解决方案是将<button>
移动到表单标签内,并使其type="submit"
。 There's no Javascript required at all: 根本不需要Javascript:
<form method="post" action="process.php">
<select name="taskOption" id="taskOption2">
<option value="Select">Please select a site</option>
<option value="http://www.Itslearning.com">Itslearning</option>
<option value="http://www.NDLA.no">NDLA</option>
</select>
<button type="submit" class="button button1 button1:hover">Take me there</button>
</form>
Try this 尝试这个
function submitMyForm()
{
alert("Test");
document.getElementById("myForm").submit();
}
<form id="myform" method="post" action="process.php">
<select name="taskOption" id="taskOption2">
<option value="Select">Please select a site</option>
<option value="http://www.Itslearning.com">Itslearning</option>
<option value="http://www.NDLA.no">NDLA</option>
</select>
</form>
<button onclick="submitMyForm()">The time is?</button>
It will not work because you forgot to add the id in the form and you are calling submit function on a form with id taskOption
. 由于您忘记了在表单中添加ID,而您正在ID为taskOption
的表单上调用Submit函数,因此它将无法正常工作。
<form method="post" action="process.php">
there is no id in the form tag, add the id as: 表单标签中没有ID,请将ID添加为:
<form method="post" action="process.php" id="taskOption">
You have wrong call submit , only form id or element can use submit()
. 您调用提交有误,只有表单ID或元素可以使用submit()
。 So call form element first 所以先调用表单元素
function FormSubmit() {
document.getElementById("taskOption2").parentNode.submit(); //must call id not name (parentNode is getting for parent form element)
}
In your php you have wrong access with select id ,the correct thing is access by element name
在您的PHP中,您使用选择ID访问错误,正确的事情是通过element name
访问
$option = isset($_POST['taskOption']) ? $_POST['taskOption'] : false; //element name is valid call
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.