php 使用 <input type ="button"> 提交表单
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9694608/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Submitting a form using <input type ="button">
提问by Tim Johnstone
I'm having a little trouble with this and can't see what I'm doing wrong. I have this form:
我在这方面有点麻烦,看不出我做错了什么。我有这个表格:
<div class="user">
<form name="userDetails" id="userDetails" method="post" >
<input type="text" name="firstName" id="firstName" class="firstName" placeholder="first name " />
<input type="text" name="lastName" id="lastName" class="lastName" placeholder="last name " />
<input type="text" name="address" id="address" class="address" placeholder="First line of Address " />
<input type="text" name="postcode" id="postcode" class="postcode" placeholder="Postcode " />
<input type="text" name="email" id="email" class="email" placeholder="E-Mail " />
<input type="text" name="phone" id="phone" class="phone" placeholder="Phone " />
<input type="button" id="submitDetails" class="submitDetails" name="submitDetails" value="Submit Your Details" />
</form>
</div>
So when the user clicks the button it should submit the form then have this PHP to act upon the form (I know that the sql isn't a prepared statement and is vulnerable to injections but this will be done later):
因此,当用户单击按钮时,它应该提交表单,然后让这个 PHP 对表单进行操作(我知道 sql 不是准备好的语句并且容易受到注入,但这将在稍后完成):
<div class="overallSummary">
<?php
$fName = $_POST['firstName'];
$lName = $_POST['lastName'];
$address = $_POST['address'];
$pc = $_POST['postcode'];
$email = $_POST['email'];
$phone = $_POST['phone'];
$userSQL = "INSERT INTO user (forename, surname, address, postcode, email, phone) VALUES ('$fName', '$lName', '$address', '$pc', '$email', '$phone')";
$result = mysql_query($userSQL) or die(mysql_error());
?>
</div>
However when I check my tables, no information is inserted into the database. Bearing in mind this code is all in the same document hence why I have not used action="file.type" within the form declaration. This is because I'm unsure whether ajax is appropriate here.
但是,当我检查我的表时,没有信息插入到数据库中。请记住,这些代码都在同一个文档中,因此我没有在表单声明中使用 action="file.type"。这是因为我不确定 ajax 在这里是否合适。
Any help is much appreciated, thank you.
非常感谢任何帮助,谢谢。
EDIT
编辑
What I could do is use ajax with jQuery to listen for the button click event:
我能做的是使用 ajax 和 jQuery 来监听按钮点击事件:
$(document).ready(function() {
$(".submitDetails").click(function(e) {
e.preventDefault();
var userData = $("#?").val();
var dataToSend = '?=' + eNameData;
$.ajax({
url: "userDetailTest.php",
type: "POST",
data: dataToSend,
cache: false,
success: function(php_output)
{
$(".overallSummary").html(php_output);
}
});
});
});
The idea here is to use this ajax for when the button is clicked and call the ajax function but i'm unsure what to put as the ID in the variable 'userData' and what to add in the 'dataToSend' variable to make it work with my form.
这里的想法是在单击按钮时使用此 ajax 并调用 ajax 函数,但我不确定在变量“userData”中将什么作为 ID 以及在“dataToSend”变量中添加什么以使其工作用我的表格。
回答by Nathan
Actually to submit the form your input
typeneeds to be submit
, not button
.
实际上要提交您的input
类型需要的表单submit
,而不是button
。
Using an button
tag would also work.
使用button
标签也可以。
<input type="submit" id="submitDetails" class="submitDetails" name="submitDetails" value="Submit Your Details" />
Unless you have some javascript code thats triggering the form submit.
除非您有一些触发表单提交的 javascript 代码。
The action attribute is also requiredas per the specification, but even without it, most browsers assume the action URL to be the current page.
根据规范,action 属性也是必需的,但即使没有它,大多数浏览器也会假设 action URL 是当前页面。
Edit: If you want to submit the form data without reloading the page, you haveto use either ajax, or put the entire form under an iframe
. (Please, do it with ajax instead).
编辑:如果您想在不重新加载页面的情况下提交表单数据,则必须使用 ajax,或者将整个表单放在iframe
. (请用 ajax 代替)。
Otherwise clicking on the input[type=button]
won't really do anything.
否则点击input[type=button]
将不会真正做任何事情。
The user data is the actual data from your form, you could capture it using:
用户数据是表单中的实际数据,您可以使用以下方法捕获它:
$(document).ready(function() {
$(".submitDetails").click(function(e) {
e.preventDefault();
// See Teez answer, I wasn't aware of this.
var dataToSend = $("#userDetails").serializeArray();
$.ajax({
url: "userDetailTest.php",
type: "POST",
data: dataToSend,
cache: false,
success: function(php_output)
{
$(".overallSummary").html(php_output);
}
});
});
});
回答by adedoy
Add action on the form and use submit.
在表单上添加操作并使用提交。
<form action="pass_data_here.php" method="post">
<input type="submit" value="Submit"/>
Also to ease the debugging you could just echo first the fields before saving them to the db. That way you can play around with the data without the hazzle of going to the database admin table.
同样为了简化调试,您可以先回显字段,然后再将它们保存到数据库。这样您就可以处理数据,而不必费力去数据库管理表。
Edit: Since you really want a button then this ajax might be useful
编辑:既然你真的想要一个按钮,那么这个 ajax 可能会有用
$('.submitDetails').click(function(e) {
e.preventDefault();
var dataString = "";
//this will get all the data of your form separated by &
$("form input:text").each(function(index, element) {
dataString += $(this).attr("id")+"="+$(this).val()+"&";
});
dataString = '?' + dataString; //added the needed ?
$.ajax({
url: "userDetailTest.php",
type: "POST",
data: dataString,
cache: false,
success: function(php_output)
{
$(".overallSummary").html(php_output);
}
});
});//end submitDetails
回答by adedoy
There are of of ways:
有以下几种方式:
Just use below if don't want to refresh page:
如果不想刷新页面,只需在下面使用:
var dataToSend = $('userDetails').serializeArray();
Or try to use below code for submit form with jquery:
或者尝试使用以下代码通过 jquery 提交表单:
$("userDetails").submit(function(){
// do your stuff
$.post(
"post.php",
// add all stuff for inserting data
);
});
Below links will help you lot:
以下链接将帮助您很多:
http://www.ryancoughlin.com/2008/11/04/use-jquery-to-submit-form/
http://www.ryancoughlin.com/2008/11/04/use-jquery-to-submit-form/
回答by Vincent Ramdhanie
You basically answered your own question. You need to put the action in the form pointing to the script that handles the form. Start with that and if you decide to use AJAX later you can always modify the code a bit to do it.
你基本上回答了你自己的问题。您需要将操作放在指向处理表单的脚本的表单中。从那开始,如果您决定稍后使用 AJAX,您可以随时修改代码来实现它。
回答by Paul Dessert
You still need an action. If the PHP is on the same page you can use something like:
你仍然需要一个动作。如果 PHP 在同一页面上,您可以使用以下内容:
action="<?php echo $_SERVER['PHP_SELF']"
回答by fred2
The three previous answers need to be combined to give one answer as you have actually two things missing from a workable HTML form, action and submit:
需要将前面的三个答案结合起来才能给出一个答案,因为您实际上在一个可行的 HTML 表单、操作和提交中缺少两件事:
<form action="mytarget.php" method="post"/>
....add input fields...
<input type="submit" value="Submit"/>
</form>