如何存储价值直到它不提交

时间:2017-05-26 16:01:54

标签: php mysql forms post

我有3页:

  1. Register.php
  2. Success.php
  3. 的login.php
  4. 现在当我的用户在success.php上注册它的值时会验证,如果不正确,它会在注册页面上返回,如果它正确,那么它就会进入登录页面。我希望当值返回到注册页面时值不正确,那么在提交之前填充的值应保持原样。

5 个答案:

答案 0 :(得分:1)

您可以通过返回用户发送的值来执行此操作。使用$ _GET http://php.net/manual/en/reserved.variables.get.php

https://www.w3schools.com/php/php_forms.asp

只需使用用户发送的参数并将其传回网址即可。

如果失败,请返回网址:

register.php?uname=value1&email=value2

然后在register.php中使用

获取参数
$username = $_GET['uname'];

然后再次将其作为值在表单中回显:

<input id="uname" value="<?php if(isset($_GET['uname']){
                   echo $username;
                   }
                   else{echo "enter username";} ?>" />

这里有一个非常简单的例子,但只需按照w3school进行操作即可完成所需的一切。

答案 1 :(得分:0)

看看JavaScript的localStorage。 (或sessionStorage

通过它,您将能够在页面之间存储信息。然后在最后一页上将POST数据同时传递给所有信息。

示例:

var existing = localStorage.getItem('user_name');
if (existing == null) {
    // The user has not set their name, lets assume it's John Doe
    existing = 'John Doe';
    localStorage.setItem('user_name', existing);
} 
alert('Hello ' + existing + '!');

或者,在纯PHP方面,你也可以使用PHP sessions。这样,所有信息都保留在服务器上而不是用户的浏览器中。

session_start();
if (!isset($_SESSION['count'])) {
    $_SESSION['count'] = 0;
} else {
    $_SESSION['count']++;
}

答案 2 :(得分:0)

当您使用PHP时,最好的选择是使用SESSIONS。

你可以创建类似的东西:

session_destroy();

这些值可以在3页中用于处理。 你的第一页是register.php,所以在页面的开头只添加一个php函数session_start();,这样当用户打开页面时,任何实时会话都将被销毁,新的会话将与该特定用户一起启动。也不要忘记在每个页面上使用函数$('.wrapper').css('display','block'); 来启动会话。我希望这能解决你的要求。

答案 3 :(得分:0)

最简单的方法,IMO,使用PHP将使用session variable来保存register.php

中所有POST数据的值

可以调用一个简单的函数来从会话变量中检索值,从而显示在HTML表单字段中。这是一个如何实现预期结果的示例。

<?php

    /* success.php */
    session_start();


    function getvalue( $var='register', $field=false ){
        if( isset( $_SESSION[ $var ] ) ){
            return array_key_exists( $field, $_SESSION[ $var ] ) ? $_SESSION[ $var ][ $field ] : '';
        }
        return '';
    }


    if( $_SERVER['REQUEST_METHOD']=='POST' ){
        /* 
           a boolean to indicate if everything is OK with the submitted
           data - to be updated later according to your rules
        */
        $ok=false;



        /* set the session variable */
        $_SESSION['register']=$_POST;



        /* process POST data - set value of $ok to true if everything is OK! */
        /*
            this is where you determine the rules for success or failure
        */


        /* Determine where the use goes next */
        if( $ok ){
            exit( header('Location: login.php') );
        } else {
            exit( header('Location: register.php') );
        }


    }
?>



<?php
    /* register.php */
    session_start();
?>
<html>
    <head>
        <title>register.php</title>
    </head>
    <body>
        <!--
            the form fields should initially be blank, but if the user is redirected 
            back to the page the fields should show the values stored in the 
            session variable.

        -->
        <form name='register' method='post' action='success.php'>
            <!-- various form fields - example -->
            <input type='text' name='email' value='<?php echo getvalue('register','email'); ?>' />
            <input type='text' name='username' value='<?php echo getvalue('register','username'); ?>' />
            <!-- more fields -->

            <input type='submit' />
        </form>
    </body>
</html>

答案 4 :(得分:0)

<?php
session_start();
$_SESSION['namefeild_name'] = $_POST['namefeild_name'];
$_SESSION['mob'] = $_POST['mob'];
$_SESSION['email'] = $_POST['email'];
$_SESSION['city'] = $_POST['city'];



if(isset($_POST['reg']))
{
$con=mysql_connect("localhost","root","");
if(!$con)       {   die('Could Not Connect: '.mysql_error());   }
mysql_select_db("database", $con);

if(!isset($_POST['namefeild_name']) || 

        !isset($_POST['mob']) ||

        !isset($_POST['pass']) ||

        !isset($_POST['pas']) ||

        !isset($_POST['email']) ||

        !isset($_POST['city'])) 
    {

        die("<script type='text/javascript'>alert('We are sorry, but there appears to be a problem with the form you submitted.')</script>");       

    }
    $uid = $_POST['namefeild_name']; // required

    $name = $_POST['namefeild_name']; // required

    $uname=$_POST['mob'];   //required

    $pass=$_POST['pass'];   //required

    $pas=$_POST['pas'];     //required

    $email = $_POST['email']; // required

    $city = $_POST['city'];     // not required

    $type = 'BAL';  // not required



    $id=md5($uname);

 $error_message = "";

    $string_exp = "/\b([A-Za-z]{1,30}[- ]{0,1}|[A-Za-z]{1,30}[- \']{1}  
    [A-Za-z]{1,30}[- ]{0,1}|[A-Za-z]{1,2}[ -\']{1}[A-Za-z]{1,30}){2,5}/";

  if(!preg_match($string_exp,$name)) {

    $error_message .= "<script type='text/javascript'>alert('Name does not appear to be valid.')</script>";
    header('location:register.php?errorname');
  }

    $string_exp = "/^[7-9][0-9]{9}$/";

  if(!preg_match($string_exp,$uname)) {

    $error_message .= "<script type='text/javascript'>alert('Mobile Number does not appear to be valid.')</script>";
    header('location:register.php?errormob');
  } 

    $string_exp = "/\b([A-Za-z]{1,30}[- ]{0,1}){1}/";

  if(!preg_match($string_exp,$pas)) {

    $error_message .= "<script type='text/javascript'>alert('The password you entered does not appear to be valid.<br />Contain Atleast one Uppercase Letter<br />Contain atleast one lower case letter<br />contain atleast one number')</script>";
    header('location:register.php?errorpass');
  }


    $string_exp = "/^[A-Za-z .'-]+$/";

  if(!preg_match($string_exp,$city)) {

    $error_message .= "<script type='text/javascript'>alert('City does not appear to be valid.')</script>";
    header('location:register.php?errorcity');
  }

  if(strlen($city) < 2) {

    $error_message .= "<script type='text/javascript'>alert('City does not appear to be valid.')</script>";
    header('location:register.php?errorcity');

  }

  if(strlen($error_message) > 0) {

    die($error_message);

  }



else
{
    $query=mysql_query("select * from customer where uname='".$uname."'") or die(mysql_error());
    $res=mysql_fetch_row($query);
    if($res)
    {
        header("location: register.php?useralreadyexits");
        session_destroy();                     
    }
    elseif(!$res)
    {
        $datetime=Date("Y/m/d H:i:s");
        $result=mysql_query("INSERT INTO customer VALUES('$id','$uname','$name','$pas','$email','$city','$datetime');");
        $result2=mysql_query("INSERT INTO payment VALUES('$id','$uname','25','$datetime','$type');");
        if($result && $result2)     
        {   
            header("location: login.php?success");
            session_destroy();
        }
        else            
        {   
            $_SESSION['name']=$uname;
            header('location:register.php?notregistered');
        }   
    }
}
mysql_close($con);
}
?>