Tips for transferring a value between two pages in PHP

Passing a variable value from one page to another is proving to be a bit tricky for me. I am using a link to navigate to the next page and trying to retrieve the value using Session,

The link variable seems to be working fine but the price variable is causing some trouble.

Below is the code snippet:

<?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>';
      }
    }   
?>

Code in 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>';

      ?>

I'd appreciate any help with this issue :)

Answer №1

Values sent by a form will only be found in the GET or POST array.

Based on what you've shared, it seems like you want to hide the field in your form.

Include this code within your form tags:

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

Moreover, if you intend to use these values for database access, exercise caution when handling input variables. Always treat user input as potentially incorrect or risky. It's essential to consider issues like SQL injection and mysqli_escape_string.

Answer №2

To retrieve the price, make sure you are using the correct method of get and that you have included it in your redirect url. If you need to access this variable on another page using the get method, ensure that you pass it in the action URL like this:

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

Then, retrieve the value with:

$price = $_GET['price'];

Alternatively, you can pass it using a hidden field within the form and retrieve it using the post method.

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

Retrieve the value like this:

$price = $_POST['price'];

Answer №3

There are various methods to send data.

  1. GET or POST
  2. Using Session
  3. Using cookies

It appears that you forgot to start a session in your orderform.php file.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Using PHP to assign a value to a variable within JSON parameters

Recently, I successfully created a REST integration for my Salesforce instance by sending fixed JSON data. However, I encountered a challenge when attempting to pass variables to the JSON for a proof of concept. Despite my efforts, I have not been able to ...

Strange results found in Google Currency Converter

I've been attempting to make a curl request to the Google currency converter URL. Although I successfully receive JSON data back, it seems to be in the incorrect encoding. Despite my efforts to adjust the values or convert the encoding, my json_decode ...

No data retrieved from database using PDO, Empty Table

I am struggling to integrate MySQL database information into HTML. Despite researching extensively on this forum for solutions, I have not been successful. The code below aims to retrieve data such as courseNumber, section, instructor, startTime, endTime ...

PHP's version of a DataTable

Is there a PHP version of C#'s datatable? I have thought about using a two-dimensional array, but it feels messy. Are there any other data structures that could be used to store different types of data in a two-dimensional format with columns and rows ...

Extract and display the custom file's output using shell_exec function in PHP

Is there a way to view the output of a shell script in a format similar to PuTTy or gnome-terminal using a php script? I attempted to use shell_exec with the following invocation: $output = shell_exec('echo "return value: ";foo=$(nameOfFileToExecute) ...

Refresh MySQL database using AJAX

In my form, there are both a submit button and a close button. When a user enters information and clicks the submit button, an answer is posted and saved in the database. If the user chooses to click the close button instead, the entry in the database will ...

Guide on embedding PHP/MYSQL array into an independent JavaScript document

I'm looking for guidance on how to insert an array from a PHP MySQL page into a separate JavaScript file. Can someone please help me with indicating where to include the PHP URL and provide the correct format for the PHP array code in order to achieve ...

Inserting a value at the beginning of every array in a multidimensional array can be done using the array

Apologies for any language barriers in my explanation. I need to insert an element at the beginning of each sub-array within a multidimensional array. $docNum = "RT/2013-2014/0266"; $values = Array ( [0] => Array ( [0] => 2014-08-07 ...

51% of memory being consumed by /ds_extras_node_page_view in Drupal 7

I've been dealing with a Drupal 7 website that experiences downtime a few times per month for about 4 to 5 minutes. According to the New Relic report, /ds_extras_node_page_view consumes 52% to 92% of the memory during loading, which is causing the sit ...

What is the best way to extract information from an online discussion thread?

Currently, I am delving into the world of Curl/PHP and finding it quite fascinating. However, I have hit a roadblock that has been hindering my progress for a few days now, and I am in need of some assistance. I have come across some unique data that requ ...

Step-by-step guide on using Ajax to accurately delete selected records with multiple checkboxes

When I utilize a loop to generate rows of data, each row is assigned a unique value for data-host-id as confirmed by the Chrome Developer Tool. However, when clicking on any record, it consistently updates the initial record instead of the intended one. Th ...

Reset Laravel 5's internal throttle/rate limiter: a step-by-step guide

My Laravel application is utilizing the built-in throttle feature in the following way: //File: Kernal protected $middlewareGroups = [ 'api' => ['throttle:10,3'] ]; But now I am looking to reset the count after a specific action is ...

header color scheme and dimensions

Hello, I've managed to get the menu working correctly on my website at www.g-evo.com/header.php. However, I'm now facing an issue with the size of the header. I'd like to shrink the grey area slightly so it aligns better with the logo, while ...

Issue with PHP PDO bindParam not correctly handling array data

A mechanism has been created to enable users to determine the order in which products are displayed within a product group. This mechanism relies on a database table named "grouplines" (MySQL) with columns for group_id, product_id, and manual_order. The fi ...

Check out the stored image from the database with the help of PHP and jQuery

I have successfully uploaded and saved several images simultaneously into a MySQL database using jQuery and PHP. This process stores the image names in the database and uploads the files to a folder on the server. Currently, I am looking to retrieve the im ...

Steps for revealing all the information from a database when clicking on the 'Read More' button

I recently set up a database called Magazine, featuring a table named social_media. This table consists of 5 columns: ID, Headline, Author, Content Short, Content. The Content Short column contains condensed versions of the articles which are displayed on ...

Google Locations API + React JS + Node.js data not returning

I would like to express my gratitude in advance for any assistance provided. I am currently exploring the development of an application using data from the Google Places web services API. I am utilizing AngularJS, PHP, and HTML for this project. Unfortunat ...

How to optimize htaccess for multi-level category and subcategory pagination

I've been working on a custom php/mysql blog project and ran into an issue with setting up the htaccess rule for displaying posts under specific categories/subcategories with pagination. Here's what I have so far: For a URL like "" which shows t ...

How can we shift some of the value to the initial value in an array? (See visual representation)

In the image below, you can see my array. I am looking to transfer the values highlighted in yellow boxes to the first value, as all these values have the same furniture_id marked with a red circle. Is there a way to accomplish this task? https://i.stack. ...

Avoiding the storage of POST data in JSON format

Here is some code I am working with: var jsonString = "some string of json"; $.post('proxy.php', { data : jsonString }, function(response) { var print = response; alert(print); }); And this P ...