簡體   English   中英

如何在php中將變量一頁傳遞給另一頁

[英]How to pass Variable One page to another in php

我創建了動態按鈕,並且單擊按鈕時需要將按鈕ID傳遞到另一頁。 我通過了,但是沒用

請給出任何解決方案

這是我的dash.php代碼:

<?php

session_start();
function dash () {
    include 'config.php';

    $sql = "SELECT RoomNumber FROM roommaster ";

    if ($result = mysqli_query($db, $sql)) {
        $str = '';
        while ($row = mysqli_fetch_array($result)) {
            // generate array from comma delimited list.
            $rooms = explode(',', $row['RoomNumber']);
            //create the dynamic button and set the value.
            foreach ($rooms as $v) {
                $str .= "<a href = 'booking.php'?btn='btn'><input type='button' name='b1' id='btn' value='" . $v . "' /></a>";
            }
        }

        return $str;
    } else {
        echo "ERROR: Could not able to execute $sql. " . mysqli_error($db);
    }
    mysqli_close($db);
}

這是我的booking.php頁面:

<?php
if (isset($_POST['btn'])) {
    $btn = $_POST['btn'];
    echo $btn;
}

$sql = "SELECT RoomNumber FROM roommaster where RoomId=' " . $_GET['btn'] . " '";
if ($result = mysqli_query($db, $sql)) {
    if (mysqli_num_rows($result) > 0) {
        while ($row = mysqli_fetch_array($result)) {
            $RoomNumber = $row['RoomNumber'];
            // $RoomType=$row['RoomType'];
            // $Location=$row['Location'];
            // $ChargesPerDay=$row['ChargesPerDay'];
        }

        // Free result set
        mysqli_free_result($result);
    } else {
        echo "No records matching your query were found.";
    }
} else {
    echo "ERROR: Could not able to execute $sql. " . mysqli_error($db);
}
mysqli_close($db);
?>

HTML:

<html lang="en">
  <head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <!-- The above 3 meta tags *must* come first in the head; any other head content must come *after* these tags -->
    <title>Customer book Form</title>
    <!-- Bootstrap -->
    <link href="css/bootstrap1.min.css" rel="stylesheet">
    <link href="css/book.css" rel="stylesheet">
    <script src='http://cdnjs.cloudflare.com/ajax/libs/jquery/2.1.3/jquery.min.js'></script>
     <link rel="stylesheet" href="https://ajax.googleapis.com/ajax/libs/jqueryui/1.11.4/themes/smoothness/jquery-ui.css">
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
    <script src="https://ajax.googleapis.com/ajax/libs/jqueryui/1.11.4/jquery-ui.min.js"></script>
</style> 
    <!-- jQuery (necessary for Bootstrap's JavaScript plugins) -->
    <script src="js/jquery-1.12.4.js"></script>
    <script src="js/book1.js"></script>
    <!-- Include all compiled plugins (below), or include individual files as needed -->
    <script src="js/bootstrap.min.js"></script>
  </head>
  <body>
  <div class="head" id="link">
      <div class="panel panel-primary" style="margin:20px;">
          <div class="panel-heading">
            <center><h3 class="panel-title"> Customer booking Form</h3></center>
          </div>
          <div class="panel-body">
              <form method="post" action="">
                  <div class="col-md-12">
                      <div class="form-group col-md-12 ">
                          <label for="roomno">Room Number*    </label>
                          <input type="text" class="form-control input-sm" name="RoomNumber"  value="<?=$_GET['btn'];?>"required>
                      </div>
                      <div class="form-group col-md-6">
                          <label for="type">Room Type*</label>
                          <input type="text" class="form-control input-sm" name="RoomType" required >
                      </div>
                      <div class="form-group col-md-6">
                          <label for="location">Location*</label>
                          <input type="text" class="form-control input-sm" name="Location" required>
                      </div>
                      <div class="form-group col-md-12">
                          <label for="charges">Facilities*</label>
                          <input type="text" class="form-control input-sm" name="Facilities" required>
                      </div>

                      <div class="form-group col-md-12">
                          <label for="charges">ChargesPerDay*</label>
                          <input type="text" class="form-control input-sm" name="ChargesPerDay" required>
                      </div>

                      <div class="form-group col-md-4">
                         <label for="customer name">First Name*</label>
                         <input type="text" class="form-control input-sm" name="FirstName" required>
                      </div>
                  </div>
              </form>
          </div>
     </body>
</html>

嘗試使用$ _GET代替$ _POST

if (isset($_GET['btn'])) {
    $btn = $_GET['btn'];
    echo $btn;
}

嘗試使用表單鏈接。 BUTTONID應該是要傳遞的值。 $ str應該是:

<form action='booking.php' method='POST'>
  <input type='hidden' value='BUTTONID' name='bttn'>
  <input type='submit' value='".$v."'>
</form>

然后像您已經獲得的那樣獲取隱藏字段的值...

$if (isset($_POST['bttn'])) {
  $btn = $_POST['bttn'];
  echo $btn;
}

我修改了dash.php和booking.php文件的腳本。 請復制並粘貼此腳本。

dash.php文件

 <?php
    session_start();
   function dash(){

      include 'config.php';

       $sql = "SELECT  RoomNumber  FROM roommaster ";

    if($result = mysqli_query($db, $sql))
    {
        $str = '';
        while($row = mysqli_fetch_array($result))
        {
            // generate array from comma delimited list
            $rooms = explode(',', $row['RoomNumber']);
            //create the dynamic button and set the value
            foreach ( $rooms as $v )
            {
                $str .= "<a href='booking.php'?btn="'.$v.'"><input type='button'   name='b1' id='btn' value='".$v."' /></a>";

            }

        }

        return $str;



    }



      else {

        echo "ERROR: Could not able to execute $sql. " .mysqli_error($db);
          }


mysqli_close($db);

booking.php文件

  $sql = "SELECT  RoomNumber FROM roommaster where RoomId=' " .$_GET['btn']." '";
if($result = mysqli_query($db, $sql)){
    if(mysqli_num_rows($result) > 0){    
        while($row = mysqli_fetch_array($result)){
                $RoomNumber=$row['RoomNumber'];
                  //$RoomType=$row['RoomType'];
                  // $Location=$row['Location'];
                  // $ChargesPerDay=$row['ChargesPerDay'];
        }

        // Free result set
        mysqli_free_result($result);
    } else{
        echo "No records matching your query were found.";
    }
} else{
    echo "ERROR: Could not able to execute $sql. " . mysqli_error($db);
}
mysqli_close($db);

嘗試使用$_REQUEST來獲取GETPOST值。在booking.php中,打印如下數組:

print_r($_REQUEST);

if (isset($_REQUEST['btn'])) {
    $btn = $_REQUEST['btn'];
    echo $btn;
}

要將少量數據從一頁傳遞到另一頁,請使用以下URL中的parameter ,該parameter是要傳遞給下一頁的value parameter

<a href="anotherpage.php?parameter=value">Click Button</a>

然后在anotherpage.php上,使用$_GET獲得URL傳遞的值,如下所示:

$val = isset($_GET['parameter']) ? $_GET['parameter'] : NULL;

然后將$val用於其他處理。 我認為這可以幫助您了解如何簡單地將data從一頁傳遞到另一頁。

暫無
暫無

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

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