Using PHP, how do you show text entered from a form to the bottom of the same PHP page?

Go To StackoverFlow.com

0

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'];?>">
2012-04-04 00:25
by Ruger
Ruger 2012-04-04 00:25
action="" will default to the current page - Ben 2012-04-04 00:31


0

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.

2012-04-04 00:29
by Taymon


0

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.

2012-04-04 00:31
by hohner
Beware of https://en.wikipedia.org/wiki/Cross-site_scriptin - Basti 2012-04-04 00:34
You're right, if it's only a quick solution for non-sensitive data it should be adequate thoug - hohner 2012-04-04 00:36
I tried the above but keep getting "Object not found - Ruger 2012-04-04 00:37
Can you output the full error code? You do know you have to replace $_POST['value_you_want'] with the name of your input element, right - hohner 2012-04-04 00:38
I did and I keep getting this: Object not found!

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

This is nothing to do with the view file. Your controller or main PHP file isn't directing or loading something. Can you post more of your code in your question, because there's absolutely no way anybody can solve the problem with one <form> tag.. - hohner 2012-04-04 00:45
He maybe uses a <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
The last comment worked. Thanks to all for their inpu - Ruger 2012-04-04 00:54


0

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>
2012-04-04 00:31
by Basti
I tried the above but keep getting "Object not found - Ruger 2012-04-04 00:36
This seems not to be related to PHP. You are probably refering to HTTP 404. Anyway, this has nothing to do with my example. ;- - Basti 2012-04-04 00:42
Ads