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

$ _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?

图片转代码服务由CSDN问答提供 功能建议

我有一个名为`index.php'的登陆页面,其格式如下:

 &lt; form action =“auto_mail.php”method =“post”&gt; 
&lt; input id =“signup”class =“span8”type =“text”placeholder =“您的电子邮件”名称 =“signup”&gt; 
&lt; input type =“submit”&gt; 
&lt;?php 
 if(isset($ _ SESSION ['got_it']))
 {echo“&lt; b&gt; You' 全部注册!&lt; / b&gt;} 
?&gt;&lt; / form&gt; 
   
 
 

在文件 auto_mail.php 我有:

  //代码清理用户输入... 
 $ user_email = $ _POST ['signup']; 
session_start(); 
 $ _SESSION [  'got_it'] ='1'; 
 //代码在有人注册时向我发送电子邮件。
echo&lt;&lt;&lt;&lt; EOD 
&lt; / b&gt; 
&lt; meta http-equiv =“refresh  “content =”0,url = index.php“&gt; 
&lt; / div&gt; 
&lt; / body&gt; 
&lt; / html&gt; 
EOD; 
?&gt; 
  <  / pre> 
 
 

我已经查看了其他一些SO问题(使用 $ _SESSION来传输数据),但这不是我想要的。

我想要的只是让用户看到“你们都注册了” 当他们输入有效的电子邮件时; 电子邮件确认电子邮件在后台发送。 这段代码感觉笨拙和尴尬。 它还会短暂地闪烁 auto_mail.php 页面。

我试图设置&lt; form action =“index.php”...... ,但它不起作用,因为我已经设置 auto_mail.php ,这样你就无法直接访问它。

如何使用 auto_mail.php 中的代码,它检查有效的电子邮件地址并发送确认电子邮件,而不处理 $ _ POST $ _ SESSION ,或者 至少更好地使用它们?

  • 写回答
  • 好问题 提建议
  • 关注问题
  • 收藏
  • 邀请回答

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).

    已采纳该答案
    评论
    解决 无用
    打赏 举报
  • dspvin19712 2013-03-15 23:14

    I think you should consider using one of popular PHP frameworks. I guess you didn't use any in above example. Good framework that also offers MVC structure allows to do operations like this in such a simple way you can't even imagine.

    Breaking it down to MVC structure will even make it extremely simple to handle post sending and displaying dependences and results made by it in one action. Learing good framework at first might look like a waste of time, but believe me - it will pay off very quickly.

    For start I recommend you looking at Kohana Framework or, if you're ambitions one - Symfony Framework.

    评论
    解决 无用
    打赏 举报
  • donglian5309 2013-03-15 23:15

    I dont get completely what you want.

    I think you try to Verify a Mail Address (after?) that form has been sent. But you cannot access the file via http that does the verification.

    Have you thought about including the auto_mail.php?

    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题