TeeKay
TeeKay

Reputation: 1065

Get data in another page through post

I have been trying to get the value of a variable in another webpage. On click of the button Confirm, the page should send the value of id to the next page. I am getting the following error -

Parse error: syntax error, unexpected ''" name = "user_id" >Confirm</' (T_CONSTANT_ENCAPSED_STRING), expecting ',' or ';' in C:\xampp\htdocs\home.php on line 57

This is where I am trying to send the data.

<body>
 <form action="database3.php" method="post" class="form" enctype="multipart/form-data">

<?php
$r=$_GET['ch'];
echo $r;
 echo '<div class="container">
 <button type="submit" class="btn btn-success" value="'.$r'" name = "user_id" >Confirm</button>
 </div>';

 ?>

This is where I am trying to fetch the data in database3.php -

<?php

 extract($_POST);
  $id=$_POST["user_id"];
  echo $id;

  ?>

What changes do I need to make?

Upvotes: 0

Views: 1053

Answers (2)

Crisoforo Gaspar
Crisoforo Gaspar

Reputation: 3830

The use of extract function since this:

Import variables into the current symbol table from an array. Ref

You should do instead:

<?php
    extract($_POST);
    echo $user_id;
?>

Update

All the variables from the $_POST variables are going to be mapped to variables for example:

$var_array = array("color" => "blue", "size"  => "medium");
extract($var_array);

Creates two new variables:

$color; // blue
$size;  // medium

So in your case $_POST has $_POST['user_id'] it creates the variable $user_id than can be used after on any next following code.

Also there is no need to mix HTML with PHPwhen you can do:

<?php $r = $_GET['ch']; ?>
<div class="container">
  <button type="submit" class="btn btn-success" value="<?php echo $r; ?>" name="user_id" >
    Confirm
  </button>
</div>

Update 2

The problem looks like is on the HTML you are missing a . in order to concatenate accordingly:

<?php
$r=$_GET['ch'];
echo $r;
echo '<div class="container">
<button type="submit" class="btn btn-success" value="'.$r.'" name=      "user_id" >
 Confirm</button>
 </div>';
   ?>

Since concatenations between strings and variables inside of single quotes are as follows:

$variable_name = 'Hi!';
echo 'The value of a variable is: ' . $variable_name . ' So that was';

Alternatively there are better ways to work around this as I mentioned on my example to avoid the mix of HTML and php. Also there are functions like sprintf or printf that makes things more expressive:

$variable_name = 'Hi!';
printf('The value of a variable is: %s So that was', $variable_name');

Upvotes: 1

Arkowsky
Arkowsky

Reputation: 903

Instead of this code:

value="'.$r'" 

should be code like this:

value="'.$r.'" 

You have missed the dot after variable $r.

Upvotes: 1

Related Questions