如果没有发布数据,则重定向回注册页面


if there is no post data redirect back to the registration page

我有一个表单:

<form id='register' action='http://mydev/test/register' onsubmit="return validateForm()" method='post' accept-charset='UTF-8'>
<fieldset>
<legend><br/>Create An Account</legend><br/>
<input type='hidden' name='submitted' id='submitted' value='1'/>
<!--<label for='username' >Username*: </label>
<input type='text' name='username' id='username' maxlength="50" /><br/><br/></br/>-->
<label for='email' >Email Address*:</label>
<input type='text' name='email' id='email' maxlength="50" /><br/><br/>
<label for='password'>Password:</label>
<input type="password" name="password" id="password" value="" size="32" /><br><br>
<label for='retype_password'>Re-Enter Password:</label>
<input type="password" name="password-check" id="password-check" value="" size="32" /><br><br>
<label for='cpassword' >&zwnj;</label>
<input type="submit" value="Submit" id="submit">
</fieldset>
</form>

我的行动页面会向他们的电子邮件发送激活链接:

$shopper = new Shopper();
$shopper->set_email($_POST['email']);
$shopper->set_username($_POST['email']);
if($shopper->create($_POST['password']))
    {
    $message = "<br>User created<br>" . "An activation code has been sent<br>";
    $token = $shopper->request_activation();
    }
    else
        {
        $message = "Could not create user<br>";
        } 

我的问题是……如果没有发布数据,它们以某种方式出现在register.php页面上。。只需将它们重定向回newregistration.php页面即可。

我知道我应该使用header("location:../../new_registration.php");

我只是有点困惑如何检查丢失的帖子数据?

我应该使用empty吗?

我最终使用了:

    if (empty($_POST['email']['password'])) {
    header('location:http://myjeromesdev/test/new_registration');
}
    else {
$shopper = new Shopper();
$shopper->set_email($_POST['email']);
$shopper->set_username($_POST['email']);
if($shopper->create($_POST['password']))
    {
    $message = "<br>User created<br>" . "An activation code has been sent<br>";
    $token = $shopper->request_activation();
    }
    else
        {
        $message = "Could not create user<br>";
        } 
}
?>
<p>
<?php 
echo $message 
?></p>

是的,您可以使用空,但也应该检查收到的$_POST数据是否是您正在等待的。

 if(!empty($_POST['email']) && !empty($_POST['password']))
 {
      $shopper = new Shopper();
      $shopper->set_email($_POST['email']);
      $shopper->set_username($_POST['email']);
      ...
 }
 else
 {
  //Your redirect
 }

u可以使用isset并在提交时设置名称(不设置名称也可以)u可以使用您输入的一个名称

查看此代码

<input type="submit" name="submit" value="submit"/>

if(isset($_POST['submit'])){
    //code is have 
}else{
    header('URL');
}
if you want if all post is setted u can use AND
isset($_POST['password']) && isset($_POST['email']) and so on

希望这个答案能帮助你