Reputation: 2167
When the user submits a form and there is an error, I want whatever they typed to remain in the text box so they can edit it instead of re-typing it. I'm trying to use a hidden field to store this value.
echo '<table><form method="post" action="" name="groupInvite">
<tr><td>Event Title:</td><td> <input type="text" name="eventTitle" value = "'.$_POST['hiddenTitle']. '"></td><td>';
$hiddenTitle = $_POST['eventTitle'];
echo '<input type="hidden" name="hiddenTitle" value = "' .$hiddenTitle. '">';
Upvotes: 0
Views: 12816
Reputation: 2872
This:
<input type="hidden" name="hiddenTitle" value = "'<? $_POST['eventTitle'] >?'">
Should be:
<input type="hidden" name="hiddenTitle" value="<?= $_POST['eventTitle'] ?>" />
The <?=
is the same as <? echo
. You also had the closing tag mixed up, and you've got single quotes inside the double quotes - I'm assuming you didn't need those so I've removed them, but if you meant to have those displayed in the text box you can still add them back.
The previous text element will also need <?=
instead of <?
When you've submitted your form, everything is held in the $_POST array (assuming you're posting the form).
In your form, you have a textbox that the user fills in, and then submits. Only once it;s submitted will the $_POST array be filled, so all you need to do is this:
<table><form method="post" action="" name="groupInvite">
<tr><td>Event Title:</td><td> <input type="text" name="eventTitle" value="<? if(isset($_POST['eventTitle']) && trim($_POST['eventTitle']) != ''){ echo $_POST['eventTitle']; } ?>"></td><td>
We check to see if the $_POST['eventTitle']
exists (If the form was submitted basically), and that it's not empty. If thats the case, we echo out it's contents into the value of the textbox.
Upvotes: 1