没有使用PHP重定向的POST

时间:2011-07-24 09:29:17

标签: 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>     

8 个答案:

答案 0 :(得分:5)

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

如果找到a good article on posting forms with AJAX using JQuery

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

答案 1 :(得分:4)

使用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>

并在您的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;
}

}

?>

答案 2 :(得分:3)

由于您的action属性,它会重定向到其他网页。

尝试:

    <form method="POST" action="<?php echo $_SERVER['PHP_SELF'] ?>"> 
        <input type="text" name="address" id="email" maxlength="30" size="23" /> 
        <input type="submit" value="" id="submit" name="submit" />
    </form> 

<?php if (isset($_POST['submit'])) : ?>
   <p>Thank you for subscribing!</p>
<?php endif; ?>

用户点击提交按钮后,该页面将显示“感谢您”消息。

另外,由于我不知道您的代码所在页面的名称,因此我插入了一个超全局变量,该变量将插入当前正在执行的脚本的文件名,相对于文档根 。因此,此页面将自行提交。

答案 3 :(得分:1)

你必须使用AJAX。但这需要JavaScript在用户Brwoser上活跃。 在我看来,这是没有重定向的唯一方法。

答案 4 :(得分:0)

假设您无法控制您要发布到的页面,那么这个想法可能对您有用:

为操作创建您自己的“代理php目标”,然后回复您想要的消息。然后,可以使用http_post_data转发发布到php文件的数据(使用预编码数据执行POST请求)。您可能需要稍微解析一下。

答案 5 :(得分:0)

发送表单请求而不重定向在php中是不可能的,但有一种方法可以解决它。

<form method="post" action="http://yoururl.com/recv.php" target="_self"> <input type="text" name="somedata" id="somedata" /> <input type="submit" name="submit" value="Submit!" /> </form>

然后为php页面发送它让它做某事但不要回显结果,而只是使用

重定向

header( 'Location: http://yourotherurl.com/formpage' );

如果您希望它发回成功消息,只需执行

$success = "true"; header( 'Location: http://yourotherurl.com/formpage?success='.$success);

并在formpage上添加

  

$ success = $ _GET [&#39;成功&#39;];

     

if($ success ==&#34; true&#34;){echo&#39;您的成功消息&#39 ;; } else {echo   &#39;您的失败信息&#39;;

答案 6 :(得分:0)

中文版

似乎没有人解决此问题 没有javascript或ajax

您还可以执行以下操作。

使用这些函数保存一个php文件,然后将它们发送到您页面的索引

实施例

的index.php

<?php
if(isset($_POST['enable'])) { 
echo "Enable";
} else {
}

if(isset($_POST['enable'])) { 
echo "Enable";
} else {
}
?>

Tools.php(它可以是任何名称,请注意它保存在文件夹中的lame工具中)

for

答案 7 :(得分:-2)

使用

form onsubmit="takeActions();return false;"

function takeAction(){ var value1 = document.getElementById('name').innerHTML; // make an AJAX call and send all the values to it // Once , you are done with AJAX, time to say Thanks :) document.getElementById('reqDiv').innerHTML = "Thank You for subscribing"; }

相关问题