0

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.

1
  • Two words: variable interpolation. You don't have it when you use single quotes as delimiters. I suggest using HEREDOC syntax instead; this page will surely be helpful. Commented Jun 15, 2012 at 23:59

1 Answer 1

2

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;
Sign up to request clarification or add additional context in comments.

2 Comments

+1 But even better, change those . dots to , commas, avoiding string concatenation altogether :) There is no need to concatenate strings when the whole thing is being output. You can just output them one after the other.
Pretty sure <input type="hidden" name="reference" value= "'. $id .'"/> needs <?php and ?> or you simply adding a string

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.