Reputation: 673
I want to make this form:
echo '<form method="post" action="ratinghandler.php" style="width: 250px">
<input type="hidden" name="reference" value= "$id"/>
<select name="rating" style="width: 115px">
<option> Rate </option>
<option>1 Bad</option>
<option>2</option>
<option>3</option>
<option>4</option>
<option>5 Average</option>
<option>6</option>
<option>7</option>
<option>8</option>
<option>9</option>
<option value="10">10 Best</option>
</select><input name="submit1" type="submit" value="Submit Rating" /></form>';
The noteworthy portion is the hidden potion. The output in the handler page of echo $_POST[reference] is simple $id (as opposed to the value stored within $id).
I tried to change that row to read:
<input type="hidden" name="reference" value= "<? echo $id ?>"/>
I assume I am doing something fairly stupid--but I searched and did not see anything directly addressing this so I thought it would be okay to ask.
Upvotes: 0
Views: 594
Reputation: 11744
Use this syntax:
<input type="hidden" name="reference" value= "'. $id .'"/>
When you use single quotes with echo, you cannot interpolate variables. Since there are so many double-quotes in the string, single quotes were used. You can break the string and concatenate your variable using the dot operator. You can also use the comma operator to avoid string concatenation.
If you were using double-quotes, you could leave the $id
in there and it'd be interpolated, but you would then have to escape ALL of your double quotes within the string.
Finally, you can use a HEREDOC to allow double-quotes and interpolation:
echo <<<EOF
<form method="post" action="ratinghandler.php" style="width: 250px">
<input type="hidden" name="reference" value= "$id"/>
<select name="rating" style="width: 115px">
<option> Rate </option>
<option>1 Bad</option>
<option>2</option>
<option>3</option>
<option>4</option>
<option>5 Average</option>
<option>6</option>
<option>7</option>
<option>8</option>
<option>9</option>
<option value="10">10 Best</option>
</select><input name="submit1" type="submit" value="Submit Rating" />
</form>
EOF;
Upvotes: 2