没有使用PHP重定向的POST

das*_*aki 7 php post redirect

我有一个简单的邮件列表,我在http://www.notonebit.com/projects/mailing-list/找到

问题是,当我点击提交所有我希望它做的是在当前表单下显示一条消息,说"感谢订阅"没有任何重定向.相反,它引导我进入一个全新的页面.

<form method="POST" action="mlml/process.php"> 
    <input type="text" name="address" id="email" maxlength="30" size="23"> 
    <input type="submit" value="" id="submit"name="submit"  >
</form>     
Run Code Online (Sandbox Code Playgroud)

Gol*_*rol 5

您需要使用AJAX将数据发布到服务器.最好的解决方案是实现常规发布,这样至少可以工作.然后,您可以使用Javascript挂钩.这样,当有人没有Javascript时,发布将起作用(通过刷新).

如果找到一篇关于使用JQuery使用AJAX发布表单的好文章.

此外,您可以选择将数据发布到同一个网址.JQuery库将添加HTTP_X_REQUESTED_WITH标头,您可以在其中检查服务器端脚本中的值.这将允许您发布到相同的URL但返回不同的值(整个页面,或只是一个特定的响应,取决于是否是一个AJAX请求).因此,您实际上可以从表单中获取URL,也不需要在Javascript中对其进行编码.这允许您编写一个更可维护的脚本,甚至可以导致一个通用的表单处理方法,您可以重用所有要使用Ajax发布的表单.


Qua*_*unk 5

使用 jQuery 非常简单:

<form id="mail_subscribe"> 
  <input type="text" name="address" id="email" maxlength="30" size="23">
  <input type="hidden" name="action" value="subscribe" />
  <input type="submit" value="" id="submit"name="submit"  >
</form>

<p style="display: none;" id="notification">Thank You!</p>

<script>
$('#mail_subscribe').submit(function() {
  var post_data = $('#mail_subscribe').serialize();
  $.post('mlml/process.php', post_data, function(data) {
    $('#notification').show();
  });
});

</script>
Run Code Online (Sandbox Code Playgroud)

在你的 process.php 中:

<?php

if(isset($_POST['action'])) {

switch($_POST['action']) {
  case 'subscribe' :
  $email_address = $_POST['address'];

  //do some db stuff...
  //if you echo out something, it will be available in the data-argument of the
  //ajax-post-callback-function and can be displayed on the html-site
  break;
}

}

?>
Run Code Online (Sandbox Code Playgroud)