簡體   English   中英

如何在php中使用會話將文本框數據傳遞到下一頁

[英]How to pass the text box data using a session to the next page in php

我想將文本框數據傳遞到下一頁(demo2),我需要將所有數據一起保存在我的數據庫中我嘗試使用會話,我傳遞的值正在發送到下一頁,但我希望文本框數據被傳遞...

下面是我的代碼

演示1.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>

演示2.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>

將錨標記代碼替換為:

<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'];

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM