如何使用会话将文本框数据传递到php中的下一页

时间:2016-03-26 09:46:34

标签: php html session

我想将文本框数据传递到下一页(demo2),我需要将所有数据保存在我的数据库中我尝试使用会话我传递的值是发送到下一页但我想要文本框数据被通过......

以下是我的代码

demo1.php

<?php
session_start();
 if($_POST)
 {
        if($_POST['act'] == "add")
    {
        $firstname = $_POST['firstname'];
        $lastname = $_POST['lastname'];
        $nationality = $_POST['nationality'];   

        $conn = mysql_connect("localhost","root","");
        $db = mysql_select_db("reg",$conn);

    }
}

echo "Session variables are set.";

if(@$_GET)
{
      $_SESSION["firstname"] = "$firstname";
      $_SESSION["lastname"] = " $lastname"; 
      $_SESSION["nationality"] = "$nationality"; 
      echo $firstname;
      echo $lastname;
      echo $nationality;
}
 ?>
    <!DOCTYPE html>
<html lang="en">

<head>

</head>

<body>
    <form  method="POST">

            <?php if(isset($_GET['id'])) { ?>
            <input type="hidden" name="act" value="edit"/>
            <input type="hidden" name="id" value="<?php echo $id; ?>"/>

            <?php } else{ ?>
            <input type="hidden" name="act" value="add"/>
            <?php } ?>

                                                                                <label> FirstName</label>

<input type="text" name="firstname" value="<?php echo @$firstname; ?>"><br>

     <label> LastName</label>

<input type="text" name="lastname" value="<?php echo @$lastname; ?>"><br>

<label> Nationality</label>

<input type="text" name="nationality" value="<?php echo @$nationality; ?>"><br>

 <a href="demo2.php"> <input type="button" id="next" name="next" value="next" >

<?PHP $_SESSION["firstname"] = "Preethi";
                                                          $_SESSION["lastname"] = " Rajan"; 
                                                          $_SESSION["nationality"] = "Indian";  ?>       
 </form>

</body>

</html>

demo2.php

<?php
session_start();
 if($_POST)

    {
        if($_POST['act'] == "add")

    {                                    

        $firstname= $_SESSION['firstname']; 
        $lastname= $_SESSION['lastname'];   
        $nationality= $_SESSION['nationality'];         

        $mobileno = $_POST['mobileno'];

        $email = $_POST['email'];

        $commaddr = $_POST['commaddr'];

        $city = $_POST['city'];

        $pincode = $_POST['pincode'];

        $state = $_POST['state'];

        $permaddr = $_POST['permaddr'];

        $conn = mysql_connect("localhost","root","");
        $db = mysql_select_db("reg",$conn);

    $sql = "INSERT INTO registration (firstname,lastname,nationality,mobileno,email,commaddr,city,pincode,state,permaddr) VALUES ('".$_SESSION["firstname"]."','".$_SESSION["lastname"]."' ,'".$_SESSION["nationality"]."', '".$mobileno."' , '".$email."' , '".$commaddr."','".$city."','".$pincode."','".$state."','".$permaddr."')";

    $rep = mysql_query($sql);
    }
}

    ?>
    <!DOCTYPE html>
<html lang="en">

<head>
</head>
    <form action="demo2.php" method="POST">

<?php if(isset($_GET['id'])) { ?>
            <input type="hidden" name="act" value="edit"/>
            <input type="hidden" name="id" value="<?php echo $id; ?>"/>

            <?php } else{ ?>
            <input type="hidden" name="act" value="add"/>
            <?php } ?>
<label> Mobile Number</label>

<input type="text" name="mobileno" value="<?php echo @$mobileno; ?>"><br>

<label> E Mail ID</label>

<input type="text" name="email" value="<?php echo @$email; ?>"><br>

<label> Communication Address</label>

<input type="text" name="commaddr" value="<?php echo @$commaddr; ?>"><br>

<label> City</label>

<input type="text" name="city" value="<?php echo @$city; ?>"><br>

<label>Pin Code</label>

<input type="text" name="pincode" value="<?php echo @$pincode; ?>"><br>

<label>State</label>

<input type="text" name="state" value="<?php echo @$state; ?>"><br>

<label> Permanent Address</label>

<input type="text" name="permaddr" value="<?php echo @$permaddr; ?>"><br>

<input type="submit" name="submit" value="submit"  style="height:50px; width:150px">

</form>
</body>

</html>

1 个答案:

答案 0 :(得分:2)

将锚标记代码替换为:

<a href="demo2.php?firstname=<?php $_POST['firstname'] ?>&lastname=<?php $_POST['lastname'] ?>&nationality=<?php $_POST['nationality'] ?>"> <input type="button" id="next" name="next" value="next" ></a>

在demo2.php上:

$fname = $_GET['firstname'];
$lname = $_GET['lastname'];
$nationality = $_GET['nationality'];