如何从用户注册页面跨页面传递变量?

时间:2015-08-06 10:04:32

标签: php

我想从我的用户注册页面(signin_up.php)通过我的确认页面(confirm_validation.php)传递变量'region_name',最后到我的区域首页(regional_head_page.php),我可以在那里查询从我的数据库引用变量(region_name)。这是我各自页面的代码:

signin_up.php

        <form id="form1" name="form1" class="form-signin"   method='post' action="confirm_validation.php">

          <div class="">
            <label for="inputFirstName" class="sr-only">First Name</label>
            <input name="fname"  type="name" id="inputfname"  class="form-control" placeholder="First Name" required autofocus>
         </div>
           <div class="">
            <label for="inputMiddleName" class="sr-only">Middle Name</label>
            <input name="mname" type="name" id="inputmname" class="form-control" placeholder="Middle Name">
          </div>
          <div class="">
            <label for="inputLastName" class="sr-only">Last Name</label>
            <input name="lname" type="name" id="inputname" class="form-control" placeholder="Last Name" required>
          </div>
          <div class="">
            <label for="inputDistrict" class="sr-only">District</label>
            <input name="district_name" type="name" id="inputdistrict" class="form-control" placeholder="District" required>
          </div>
 <div class="">
            <label for="inputEmail" class="sr-only">Email</label>
            <input name="eemail" type="email" id="inputEmail" class="form-control" placeholder="Email" required>
          </div>
   <div class="">
    <select name="access_level"  class="form-control" placeholder="Position"  id="access_level" required>
      <option>Please Select your Position</option>
      <option value="regional_head">Regional Head</option>
      <option value="general_user">General User</option>
    </select>
   </div> 
       <div class="">
           <select name="region_name"  class="form-control" placeholder="Region"  id="region_name" required>
               <option>Please Select your Region</option>
               <option>Greater Accra Region</option>
               <option>Eastern Region</option>
               <option>Western Region</option>
               <option>Central Region</option>
               <option>Ashanti Region</option>
               <option>Volta Region</option>
               <option>Brong Ahafo Region</option>
               <option>Northern Region</option>
               <option>Upper West Region</option>
               <option>Upper East Region</option>
   </select>
  </div>
          <div class="">
            <label for="pwd" class="sr-only">Password</label>
            <input name="district_pass" type="password" id="pwd" class="form-control" placeholder="Password" >
            </div>
    <div class="">
            <button  name="signup" class="btn btn-primary btn-lg" type="submit" >Submit</button>
          </div

confirm_validation.php

 ob_start();
 session_start();
 if(isset($_POST['signup'])){

 $con=mysqli_connect("localhost","root","myspace4199","car_sales");
 // Check connection
 if (mysqli_connect_errno()) {echo "Failed to connect to MySQL: " . mysqli_connect_error();}

 $user_signup = "user_signup";

 $fname = mysqli_real_escape_string($con, $_POST['fname']);
 $mname = mysqli_real_escape_string($con, $_POST['mname']);
 $lname = mysqli_real_escape_string($con, $_POST['lname']);
 $district_name = mysqli_real_escape_string($con, $_POST['district_name']);
 $eemail = mysqli_real_escape_string($con, $_POST['eemail']);
 $access_level= mysqli_real_escape_string($con, $_POST['access_level']);
 $region_name = mysqli_real_escape_string($con, $_POST['region_name']);
 $district_pass = mysqli_real_escape_string($con, $_POST['district_pass']);

        //If user is a regional head
        if($access_level == 'regional_head'){
            $sql="INSERT INTO user_signup (eemail,mobile_number,fname,mname,lname,access_level,district_name,district_pass) VALUES ('$eemail','$mobile_number','$fname','$mname','$lname','$access_level','$district_name','$district_pass')";
            $_SESSION['eemail'] = $eemail; 
            header('Location: regional_head_page.php');
        }
        else if ($access_level == 'car_company'){
                            $sql="INSERT INTO user_signup (eemail,mobile_number,fname,mname,lname,access_level,district_name,district_pass) VALUES ('$eemail','$mobile_number','$fname','$mname','$lname','$access_level','$district_name','$district_pass')";
                            $_SESSION['eemail'] = $eemail;
                            header('Location: car_company_page.php');
        }

  if (!mysqli_query($con,$sql)) {
  die('Error: ' . mysqli_error($con));
  }

 echo "";

 mysqli_close($con);
 }
 ?>

regional_head_page.php

 <?php
 $con=mysqli_connect("localhost","root","myspace4199","car_sales");
 // Check connection
 if (mysqli_connect_errno()) {echo "Failed to connect to MySQL: " . mysqli_connect_error();}

 $region_name = mysqli_real_escape_string($con, $_POST['region_name']);

 $sql="SELECT * FROM general_reservation WHERE region_name = '$region_name'";

 $records=mysqli_query($con,$sql);


 ?>

3 个答案:

答案 0 :(得分:1)

您已使用session_start();。所以在会话中传递数据。

 ob_start();
 session_start();
 if(isset($_POST['signup'])){

 $con=mysqli_connect("localhost","root","myspace4199","car_sales");
 // Check connection
 if (mysqli_connect_errno()) {echo "Failed to connect to MySQL: " . mysqli_connect_error();}

 $user_signup = "user_signup";

 $fname = mysqli_real_escape_string($con, $_POST['fname']);
 $mname = mysqli_real_escape_string($con, $_POST['mname']);
 $lname = mysqli_real_escape_string($con, $_POST['lname']);
 $district_name = mysqli_real_escape_string($con, $_POST['district_name']);
 $eemail = mysqli_real_escape_string($con, $_POST['eemail']);
 $access_level= mysqli_real_escape_string($con, $_POST['access_level']);
 $region_name = mysqli_real_escape_string($con, $_POST['region_name']);
 $district_pass = mysqli_real_escape_string($con, $_POST['district_pass']);


 $_SESSION['region_name'] = $region_name;



 //If user is a regional head
 if($access_level == 'regional_head'){
       $sql="INSERT INTO user_signup (eemail,mobile_number,fname,mname,lname,access_level,district_name,district_pass) VALUES ('$eemail','$mobile_number','$fname','$mname','$lname','$access_level','$district_name','$district_pass')";
       $_SESSION['eemail'] = $eemail; 
            header('Location: regional_head_page.php');
        }
        else if ($access_level == 'car_company'){
                            $sql="INSERT INTO user_signup (eemail,mobile_number,fname,mname,lname,access_level,district_name,district_pass) VALUES ('$eemail','$mobile_number','$fname','$mname','$lname','$access_level','$district_name','$district_pass')";
                            $_SESSION['eemail'] = $eemail;
                            header('Location: car_company_page.php');
        }

  if (!mysqli_query($con,$sql)) {
  die('Error: ' . mysqli_error($con));
  }

 echo "";

 mysqli_close($con);
 }
 ?>

在regional_head_page.php中

<?php
 session_start();

 $con=mysqli_connect("localhost","root","myspace4199","car_sales");
 // Check connection
 if (mysqli_connect_errno()) {echo "Failed to connect to MySQL: " . mysqli_connect_error();}

 if ( isset($_SESSION['region_name']) ) {
     $region_name = $_SESSION['region_name'];
 } else {
     // got a problem, deal with it here
 }

 $sql="SELECT * FROM general_reservation WHERE region_name = '$region_name'";

 $records=mysqli_query($con,$sql);
?>

答案 1 :(得分:0)

在我看到您的代码之后,首先是您为什么不在<?php $currentwidth中使用<script>代码。 现在你也可以这样做: 在文件</script>中: 改变regional_head_page.phpconfirm_validation.php

答案 2 :(得分:0)

尝试使用会话,或者您可以在查询字符串中传递该变量值。

查询字符串:标题('位置:regional_head_page.php?region_name ='。$ region_name);

OR

Session = $ _SESSION ['region_name'];