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 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建
  • ¥15 数据可视化Python
  • ¥15 要给毕业设计添加扫码登录的功能!!有偿
  • ¥15 kafka 分区副本增加会导致消息丢失或者不可用吗?
  • ¥15 微信公众号自制会员卡没有收款渠道啊
  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘