K BHATT
K BHATT

Reputation: 21

How to pass a variable to another page in php using GET or POST method?

I am making an editing form. There are 2 forms, one form called edit1.php and another form called edit.php. This second one should get the id generated from the edit1.php and use it to generate further queries. The following is the code for the 2 files:

edit1.php:

<table>
  <tr>
    <td align="center"><h4><b>EDIT DATA</b></h4></td>
  </tr>
  <tr>
    <td>
      <table border="3">
      <form action="edit.php" method="get">
      <?php

      $vendors = "SELECT `vendor`.`v_id`,`vendor`.`v_name` FROM `stfood`.`vendor`";
      $result = mysqli_query($con, $vendors);

      while ($row = mysqli_fetch_array($result)){
        echo ("<tr><td>'".$row["v_id"]."'</td>");
        echo ("<td>'".$row["v_name"]."'</td>");
        echo ("<td><a href=\"edit.php?id=".$row['v_id']."\">Edit</a></td></tr>");

      }

      ?>
      </form>
      </table>

edit.php:

<?php
require 'init.php';
require 'functions.php';

?>
<html>
<head>
<title>Form Edit Data</title>
</head>
<body>
<table border=1>
  <tr>
    <td align=center>Form Edit vendor Data</td>
  </tr>
  <tr>
    <td>
      <table>

      <?php
        echo $_REQUEST['v_id'];
      $vendor = "SELECT `vendor`.`v_name`, `vendor`.`v_email`,`vendor`.`v_desc`,`vendor`.`cont_id` FROM `stfood`.`vendor` WHERE `vendor`.`v_id` = '".$v_id."'";
      $vendor_result = mysqli_query($con, $vendor);
      $row = mysqli_fetch_array($vendor_result);
      ?>
      <form method="post" action="edit_data.php">
      <input type="hidden" name="id" value="<?php echo $row["v_id"];?>">
        <tr>        
          <td>Name</td>
          <td>
            <input type="text" name="name" size="20" value="<?php echo $row["v_name"]; ?>">
          </td>
        </tr>
        <tr>
          <td>Email</td>
          <td>
            <input type="text" name="email" size="50" value="<?php echo $row["v_email"];?>">
          </td>
        </tr>
        <tr>
          <td>Vendor Description</td>
          <td>
            <input type="text" name="description" size="100" value="<?php echo $row["v_desc"];?>">
          </td>
        </tr>
        <tr>
          <td align="right">
            <input type="submit"
          name="submit value" value="Edit">
          </td>
        </tr>
      </form>
      </table>
    </td>
  </tr>
</table>
</body>
</html>

When I run the code, the first form displays all the relevant data, and when I click on the edit link the page gets redirected and I can see the v_id passed in the URL, but nothing comes into the edit.php file. When I do a var_dump($row['v_id']); I get NULL.

Why is v_id not set in edit.php?

Upvotes: 0

Views: 1451

Answers (4)

Piyush Raja
Piyush Raja

Reputation: 49

Try using $_GET['id'] instead of $_REQUEST['v_id'] on edit.php

See example #1 here

Also, have you defined the $v_id before using it in the query?

Like this.

Upvotes: 0

Nazim
Nazim

Reputation: 564

In the edit1.php you have not used form elements. The value $row['v_Id'] should be the value of form element input like this inside form

<input type='hidden' name='v_I'd value ='<?php echo $row['v_id'] ?>'>

Upvotes: 0

DirtyBit
DirtyBit

Reputation: 16772

on your edit.php page you have:

<a href=\"edit.php?id=".$row['v_id']."\">Edit</a>

now on your edit1.php you should first have a isset() and then as follow:

if(isset($_GET['id']){

$id = $_GET['id'];

//carry on blah3 ...

Upvotes: 0

zedfoxus
zedfoxus

Reputation: 37059

Since your hyperlink looks like this:

<a href=\"edit.php?id=".$row['v_id']."\">Edit</a>

Your edit.php should use

echo $_GET['id'];
// or something like this
$received_id = $_GET['id'];
// do validation whether you received a good id or not and then move on
...
...
<input type="hidden" name="id" value="<?php echo $received_id;?>">

Upvotes: 1

Related Questions