Page 1 of 1

Not able to define hidden field

Posted: Tue May 27, 2003 2:27 pm
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">';
?>

Posted: Tue May 27, 2003 3:44 pm
by Stoker
> //This is giving problems

What kind of problems? What is the output you get?

Posted: Tue May 27, 2003 3:49 pm
by vipul73
The hidden field value remains $last_id and is not changed

Re: Not able to define hidden field

Posted: Tue May 27, 2003 4:31 pm
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;

Posted: Tue May 27, 2003 4:59 pm
by vipul73
Single Quote with concatenation works perfecty

Thanx a lot 8)