duanfuxing2212 2013-03-15 23:05
浏览 17
已采纳

$ _SESSION变量用于检查表单是否已提交

I have a landing page called `index.php' with the following form:

<form action="auto_mail.php" method="post">
   <input id="signup" class="span8" type="text" placeholder="Your email" name="signup">
   <input type="submit">
   <?php
     if (isset($_SESSION['got_it']))
     {echo "<b>You're all signed up!</b>}
   ?></form>

In the file auto_mail.php I have:

// code scrubbing user input...
$user_email = $_POST['signup'];
session_start();
$_SESSION['got_it'] = '1';
// code sending me an email when someone signs up.
echo <<<EOD
  </b>
  <meta http-equiv="refresh" content="0, url=index.php">
  </div>
  </body>
  </html>
EOD;
?>

I've looked at some other SO questions (Using $_SESSION to carry data), but it's not what I'm looking for.

All I want, is for a user to see "You're all signed up" when they enter a valid email; with the email confirm email being sent in the background. This code feels clumsy and awkward. It also flashes the auto_mail.php page briefly.

I tried to set <form action="index.php"..., but it doesn't work because I've set up auto_mail.php such that you can't access it directly.

How can use the code in auto_mail.php, which checks for a valid email address and sends confirm emails, without dealing with both $_POST and $_SESSION, or at least using them better?

  • 写回答

3条回答 默认 最新

  • douyi4205 2013-03-15 23:08
    关注

    If you don't want to have any page reloads whatsoever, you'll have to use AJAX to send the form, instead of utilising the form POST.

    If you are using jQuery, or Mootools, they both have built in wrappers to handle ajax calls. Without a helper library, you'll have to look into making an XMLHttpRequest yourself.

    Other than that, traditionally, you would redirect the user to a "form submitted" page, or alternatively, have the form action be sent to the same page (in your case, index.php, and have PHP code to handle form data if it is received).

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 抖音咸鱼付款链接转码支付宝
  • ¥15 ubuntu22.04上安装ursim-3.15.8.106339遇到的问题
  • ¥15 求螺旋焊缝的图像处理
  • ¥15 blast算法(相关搜索:数据库)
  • ¥15 请问有人会紧聚焦相关的matlab知识嘛?
  • ¥15 网络通信安全解决方案
  • ¥50 yalmip+Gurobi
  • ¥20 win10修改放大文本以及缩放与布局后蓝屏无法正常进入桌面
  • ¥15 itunes恢复数据最后一步发生错误
  • ¥15 关于#windows#的问题:2024年5月15日的win11更新后资源管理器没有地址栏了顶部的地址栏和文件搜索都消失了