简体   繁体   English

如何在PHP中将值从页面传递到另一个页面

[英]How to pass a value from a page to another page in PHP

Im trying to pass a variable value from a page to another page. 我试图将一个变量值从一个页面传递到另一个页面。 Im using to go to the next page and I use Session to get the value, 我过去一直去下一页,而我使用Session来获取价值,

the link variable does working but the price variable doesn't work 链接变量有效,但价格变量无效

Here's the code: index.php 这是代码:index.php

<?php 

  require('connect.php');
  if(!isset($_SESSION)){
    session_start();
  }

 $query  = mysqli_query($connection, "SELECT link, price FROM funnyture WHERE category ='Bed' or category = 'Chairs' or category = 'Tables'") or die("Couldn't find search");
    $count = mysqli_num_rows($query);

    if($count == 0) {
      $output = "There was no row in array";

    } else {

      while($row = mysqli_fetch_array($query)) {
        $link = $row['link'];
        $price = $row['price'];

        echo '

        <form action="Orderform.php" method="post" class="formClass">

          <input name="link" type="image" src="'.$link.'" value="'.$link.'" class="inputClass">
        </form>';
      }
    }   
?>

Orderform.php Orderform.php

<?php include 'navbar2.php' ;?>

    <div class="left">
      <?php
      $_SESSION['link'] = $_POST['link'];
       // $_SESSION['price'] = $_POST['price'];
       echo '<img src="'.$_SESSION['link'] .'">';  
       echo '<p>'.$_GET['price'].'</p>';

      ?>

Help me :) 帮我 :)

Only values that are sent by a form will be in the GET or POST array. 只有由表单发送的值才会在GETPOST数组中。

From what you are showing I conclude that you don't want to show the field in your form, so make it hidden. 根据您显示的内容,我得出结论,您不想在表单中显示该字段,因此将其隐藏。

Add this inside your form tag: 将此添加到您的表单标签中:

<input name="price" type="hidden" value="'.$price.'" class="inputClass">

Also, if you are going to use these values to access the database, be more careful with just taking in variables like this, assume that all user input is wrong and/or dangerous. 另外,如果要使用这些值访问数据库,则仅输入这样的变量时要格外小心,并假定所有用户输入都是错误和/或危险的。 You would want to look at things like SQL injection and mysqli_escape_string 您可能想看一下SQL注入mysqli_escape_string之类的东西

You are fetching the price using get method and you have not passed it in your redirect url . 您正在使用get方法获取价格,但尚未在重定向url中传递价格。 If you want to fetch that variable on other page using get method then pass it in action url as : 如果您想使用get方法在其他页面上获取该变量,则将其作为以下内容传递给操作网址:

<form action="Orderform.php?price=<?php echo $price; ?>" method="post" class="formClass">

and fetch as : 并提取为:

$price =  $_GET['price'];

Or otherwise pass it using a hidden field inside form and fetch using post method. 或者以其他方式在表单内使用隐藏字段传递它,并使用post方法获取。

<input name="price" type="hidden"  value="'.$price.'" class="inputClass">

and fetch like: 并获取像:

$price = $_POST['price'];

Data can be Send in multiple ways 数据可以多种方式发送

  1. GET or POST GET或POST
  2. Using Session 使用会议
  3. Using cookie 使用cookie

in your orderform.php you are forgot to start session. 在您的orderform.php中,您忘记了开始会话。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM