Using PHP, how do you show text entered from a form to the bottom of the same PHP page?
I am trying to show user input at the bottom once submit is pressed
<form name="assignment2" method="post" action="<?php echo $_SERVER['PHP_SELF'];?>">
It sounds like you want AJAX. To do that, the form should post an XMLHttpRequest to the server; JavaScript on the page should then dynamically modify the page DOM to display the text you want.
If you're okay with it being static, just have the PHP page display just the form if there's no input data, and the data if there is input data.
You don't need AJAX or jQuery; you can keep this to simple PHP:
<?php
echo '<form name="assignment2" method="post" action="'. $_SERVER['PHP_SELF'] .'">';
// Other form elements
echo '</form>';
if(isset($_POST['submit']))
{
echo '<input type="text" value="'. $_POST['value_you_want'] .'">';
}
?>
As Basti has pointed out, you should always be cautious about the possibility of cross-site scripting so you should only really use this code above with non-sensitive data or data which will go nowhere near a database.
$_POST['value_you_want']
with the name of your input element, right - hohner 2012-04-04 00:38
The requested URL was not found on this server. The link on the referring page seems to be wrong or outdated. Please inform the author of that page about the error.
If you think this is a server error, please contact the webmaster.
Error 404
localhost 04/03/12 20:39:58 Apache/2.2.21 (Win32) modssl/2.2.21 OpenSSL/1.0.0e PHP/5.3.8 modperl/2.0.4 Perl/v5.10. - Ruger 2012-04-04 00:42
<form>
tag.. - hohner 2012-04-04 00:45
<base>
-tag, altering the forms target, so PHP_SELF
won't work. Ben's comment to leave the action
empty should solve this - Basti 2012-04-04 00:49
Since your form is sent via POST request, all of the form's inputs can be accessed using $_POST
. You can simply put them into your inputs as value
.
<form method="post" action="<?php echo $_SERVER['PHP_SELF'];?>">
<input type="text" name="inputname"
value="<?php echo @htmlspecialchars($_POST["inputname"]); ?>" />
</form>
action=""
will default to the current page - Ben 2012-04-04 00:31