php 提交后停留在页面上
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10432908/
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
Staying on page after submitting
提问by Kyle Yeo
just a question --
就一个问题 -
How do i actually submit a form and still stay on the same page whilst the script is being executed?
我如何实际提交表单并在执行脚本时仍然停留在同一页面上?
I have a simple form that goes like this:
我有一个简单的表格,如下所示:
<form action="process.php" method="post" id="form">
<label for="name">Name</label>
<input type="text" name="name" id="name" required="required" />
<label for="email">Email</label>
<input type="email" name="email" id="email" required="required" />
<label for="subject">Subject</label>
<input type="text" name="subject" id="subject" required="required" />
<label for="message">Message</label>
<input type="text" name="message" id="message" required="required" />
<input type="submit" id="button" value="Submit"/>
</form>
But everytime i submit the form it just goes to process.php. I don't want that, i want it to stay on the same page, maybe even have a jquery popup saying "thank you" or something.
但是每次我提交表单时,它都会转到 process.php。我不希望那样,我希望它保持在同一页面上,甚至可能有一个 jquery 弹出窗口说“谢谢”之类的。
How do i actually stay on the same page whilst the script is being run?
我如何在脚本运行时实际停留在同一页面上?
采纳答案by John Conde
- Put your form processing logic on the same page as the form
- Use Ajax
- Redirect back to that page when process.php is done processing the form
- 将您的表单处理逻辑与表单放在同一页面上
- 使用 Ajax
- 当 process.php 处理完表单后重定向回该页面
回答by William Calvin
You have to use ajax in this case.
在这种情况下,您必须使用 ajax。
Basically, you need to post all data using ajax. In server side, you will need to get all parameters using $_POST['name'] like you normally do in server scripting.
基本上,您需要使用 ajax 发布所有数据。在服务器端,您需要像通常在服务器脚本中一样使用 $_POST['name'] 获取所有参数。
FORM
形式
<form action="process.php" method="post" id="form">
<label for="name">Name</label>
<input type="text" name="name" id="name" required="required" />
<label for="email">Email</label>
<input type="email" name="email" id="email" required="required" />
<label for="subject">Subject</label>
<input type="text" name="subject" id="subject" required="required" />
<label for="message">Message</label>
<input type="text" name="message" id="message" required="required" />
<input type="button" id="button" value="Submit"/>
</form>
<div id="dialog-message" title="Thank You">
<p>Your message here</p>
</div>
?
JQUERY
查询
$(document).ready(function(){
$('#dialog-message').dialog({
modal: true,
autoOpen: false,
buttons: {
Ok: function() {
$(this).dialog("close");
}
}
});
$('#button').on('click', function() {
var name = $('#name').val();
var email = $('#email').val();
var subject = $('#subject').val();
var message = $('#message').val();
var dataString = 'name=' + name + '&email=' + email + '&subject=' + subject + '&message=' + message;
$.ajax({
type: "POST",
url: "process.php",
data: dataString,
success: function() {
alert('');
$('#dialog-message').dialog('open');
}
});
});
});?
See the example here
请参阅此处的示例
Please note, dont forget to put jquery reference and also jquery ui reference
请注意,不要忘记放置 jquery 参考和 jquery ui 参考
回答by Deratrius
I think John Conde gave the best answer.
我认为约翰康德给出了最好的答案。
I would either make it so the php file that displays the form also handles the $_POST or if you can't do it that way then do it in Ajax with a code similar to this:
我要么让它显示表单的 php 文件也处理 $_POST ,或者如果你不能这样做,那么在 Ajax 中使用类似于以下的代码:
$("#form").bind("submit", function() {
var nameTxt = $("#name").val();
var emailTxt = $("#email").val();
$.post('process.php',
{
name: nameTxt,
email: emailTxt
},
function(data) {
if (data == 'ko')
alert('Could not submit...');
else {
alert('Thank you for your message.');
}
});
return false;
}
What this does is that it "blocks" the regular submit of the form when you click the submit button, retrieve input values and sends them to the process.php script.
它的作用是在您单击提交按钮时“阻止”表单的常规提交,检索输入值并将它们发送到 process.php 脚本。
It assumes that your "process.php" does an
它假设你的“process.php”做了一个
echo "ko";
if there is an error. You can do some form cleanup after the form has been successfully sent by reseting the inputs:
如果有错误。您可以通过重置输入在表单成功发送后进行一些表单清理:
$("#name").val('');
$("#email").val('');
回答by jajaperson
I targeted an iframe to solve the problem, and it worked perfectly.
我瞄准了一个 iframe 来解决这个问题,它工作得很好。
<form name="contact" role="form" method="post" action="contact.php" target="my_iframe">
Feild 1: <input name="feild1" id="feild1" type="text" placeholder="Lorem ipsum dolor"><br/><br/>
Feild 2: <input name="feild2" id="feild2" type="text" placeholder="Lorem ipsum dolor"><br/><br/>
Feild 3: <textarea name="feild3" id="feild3" rows="5" type="text" placeholder="Lorem ipsum dolor, sit amet."></textarea><br/><br/>
<input type="submit">
</form>
<!-- target iframe to prevent redirection to 'contact.php' -->
<iframe name="my_iframe" width="1" height="1" style="border:none"></iframe>
回答by srini
try something like this
尝试这样的事情
action="<?php print $_SERVER["PHP_SELF"]; ?>" (or) action=""
EDIT:
编辑:
<?php
if(isset($_POST['Submit'])){
//do your validation or something here
header("location:Process.php");
}
回答by duncan
Assuming you're using jquery:
假设您正在使用 jquery:
$(document).ready(function(){
$('#form').submit(function(e){
// do some ajax request to process.php here...
// stop the form doing its default action of submitting directly to process.php
e.preventDefault();
});
});
回答by hakre
Literally:
字面上地:
<form action="process.php" method="post" id="form" target="_blank">
^^^^^^^^^^^^^^^^
This will make the browser stay on the same page, even if the form is submitted.
这将使浏览器保持在同一页面上,即使提交了表单。
But you will be more likely looking for AJAX instead, some Introduction.
但是您更有可能寻找 AJAX,而不是一些 Introduction。
回答by Michael Mior
You can do something like this (with jQuery, untested)
你可以做这样的事情(使用 jQuery,未经测试)
$(function() {
$("#form").submit(e) {
// Prevent regular form submission
e.preventDefault();
// Submit the form via AJAX
var $form = $("#form");
$.post(
$form.attr("action"),
$form.serialize(),
function() { alert("Done!"); }
);
}
}

