3

I have an HTML form that is echoed from my php script. When I try to pass a hidden variable through the script, the code does not work as intended.

$threadNumber=$row["Tid"];
echo "<center><b>Message# ", $messageNumber , ": </b><br>";
echo $row["Mtitle"] , " in Thread# " , $row["Tid"] , "<br>";
echo "The Message was Written on " , $row["Mdate"] , '<br>';
echo $row["Mbody"];
echo "<br><br>";
echo '<form action="messageReply.php" method="post">';
echo '<textarea name="reply" rows=5 cols=30 placeholder="Reply to the Message?"></textarea>';
echo '<input type="hidden" name="Mtitle" value="<?php echo $row["Mtitle"] ?>">';
echo '<input type="submit" value="Send Message">';
echo '</form>';

The result looks like this:

enter image description here

and when I try to read $_POST["Mtitle"] in the messageReply.php script, I get an error saying such an index does not exist.

2
  • You cant use PHP in php.. echo '<input type="hidden" name="Mtitle" value="' . echo $row["Mtitle"] . '">'; - you did it correct in previous code, why suddenly the change of heart? Commented Dec 23, 2015 at 10:51
  • @Crt: chose the best answer and mark as accepted if you feel your issue has been resolved because it will be help full for others who are facing same issue. Commented Dec 23, 2015 at 12:17

2 Answers 2

1

Try this:

echo '<input type="hidden" name="Mtitle" value='.$row['Mtitle'].'>';

Your Complete Code (Modified):

$threadNumber=$row["Tid"];
echo "<center><b>Message# ", $messageNumber , ": </b><br>";
echo $row["Mtitle"] , " in Thread# " , $row["Tid"] , "<br>";
echo "The Message was Written on " , $row["Mdate"] , '<br>';
echo $row["Mbody"];
echo "<br><br>";
echo '<form action="messageReply.php" method="post">';
echo '<textarea name="reply" rows=5 cols=30 placeholder="Reply to the Message?"></textarea>';
echo '<input type="hidden" name="Mtitle" value='.$row['Mtitle'].'>';
echo '<input type="submit" value="Send Message">';
echo '</form>';
Sign up to request clarification or add additional context in comments.

3 Comments

this is good, if you are new on SO, accept the best answer, this will help for other who face this issue. @Crt
sure, no problem. Sorry, was busy. Can you, or anyone else explain, why this worked.
@crt: for this I suggest to read single quote and double quote how it works... u will get basic idea brother
1

Note: You are using php in php. Please change it below code:

<?php
echo '<input type="hidden" name="Mtitle" value="'.$row["Mtitle"].'">';
?>

Comments

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.