p3ace p3ace - 1 year ago 90
PHP Question

Passing php variable as hidden input where html is contained in one echo

I'm new with PHP and still learning.

$myVariable = $_POST['fieldParameter'];

//do something

echo "
<form method='POST' action='submit.php'><br/>
<input type='hidden' name='myVariable' value='<?php echo $myVariable; ?>'/>
<input type='submit' name='btnSubmit' id='submit' value='Submit'>


Notice that the variable
is contained in the main
block. I'm trying to send the value of
to submit.php as hidden field.

Also, i enclosed all the html tags using one
statement with double quotes.

I found related questions here in
but can't find similar to embedding
within a long

I tried to put
value='<?php echo $studentNo; ?>'
with no success.

I want to access it in a submit.php file like this,


$aVariable = $_POST['myVariable'];
echo $aVariable;

How can I pass the value contained in
as hidden field? Is there something wrong with the way I use double and single quotes?

Thank you.

Answer Source

You just need to type $myVariable instead of in your string. Double quotes "" only creates a string literal. It doesn't directly output data like inline HTML. As you can see from the syntax coloring in StackOverflow, the

You can try these variants (simplified):

// code before
echo "<input type='hidden' name='myVariable' value='$myVariable'/>";
// code after

// OR //

// code before
<input type='hidden' name='myVariable' value='<?= $myVariable ?>'/>
// code after

Note that the quotes you use in HTML don't affect PHP, as long as you escape them properly (use \" and \' where appropriate).

See HTML output from web server for more information.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download