Not able to define hidden field

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
vipul73
Forum Commoner
Posts: 27
Joined: Mon May 12, 2003 5:32 am

Not able to define hidden field

Post by vipul73 »

How do I assign the retrieved value to hidden field

Code: Select all

<?php
$email = $_POST["email"]; 
$db = mysql_connect("localhost", "root"); 
mysql_select_db("visitor",$db); 
$sql = "INSERT INTO invoice (email) VALUES ('$email')"; 
$result = mysql_query($sql); 
$last_id = mysql_insert_id();
echo "Thank you! Information entered.\n"; 

//This displays perfectly
echo $last_id;

//This is giving problems
echo '<input name="lastid" type="hidden" id="lastid" value="$last_id">';
?>
User avatar
Stoker
Forum Regular
Posts: 782
Joined: Thu Jan 23, 2003 9:45 pm
Location: SWNY
Contact:

Post by Stoker »

> //This is giving problems

What kind of problems? What is the output you get?
vipul73
Forum Commoner
Posts: 27
Joined: Mon May 12, 2003 5:32 am

Post by vipul73 »

The hidden field value remains $last_id and is not changed
User avatar
nielsene
DevNet Resident
Posts: 1834
Joined: Fri Aug 16, 2002 8:57 am
Location: Watertown, MA

Re: Not able to define hidden field

Post by nielsene »

vipul73 wrote:How do I assign the retrieved value to hidden field

Code: Select all

//This is giving problems
echo '<input name="lastid" type="hidden" id="lastid" value="$last_id">';
?>
Variable replacement is not done inside single quotes. To make this work you need to do one of the following (which one you like is normally a stylistic preference).

Single Quote with concatenation

Code: Select all

echo '<input name="lastid" type="hidden" id="lastid" value="'.$last_id.'">';
or
Double Quote with Escapes

Code: Select all

echo "<input name="lastid" type="hidden" id="lastid" value="$last_id">";
or
HEREDOC

Code: Select all

$input=<<<END_INPUT
<input name="lastid" type="hidden" id="lastid" value="$last_id">
END_INPUT;
echo $input;
vipul73
Forum Commoner
Posts: 27
Joined: Mon May 12, 2003 5:32 am

Post by vipul73 »

Single Quote with concatenation works perfecty

Thanx a lot 8)
Post Reply