tags:

views:

106

answers:

6

Hi guys, got a small problem, this code

<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
<?php
...
echo '<input name="textfield" type="text" id="textfield" value="Roger" />';
echo 'Hello, '.$_POST['textfield'].'<br>';
...
?></p>
</form>

should echo out "Hello, Roger", as roger is the default value, yet it gives out only "Hello, " and nothing else. Any suggestions?

edit: yes, there's a form.

Thanks!

+1  A: 

Try print_r($_POST) or var_dump($_POST) to see if any POST data gets submitted.

Edit: Did you specify POST as the submit method in your form tag? Do you submit the form? Please show the entire <form>-Tag.

danilo
+1, var_dump()!
KM
A: 
  • check the css (is it hidden..?=
  • check the source for the page (does the input field appear?)
  • don't forget to wrap the input in a form if you want to submit it back to the same page.
danp
+6  A: 

You are echoing the text box and at the same time hoping to gets its value, which is not possible.

echo '<input name="textfield" type="text" id="textfield" value="Roger" />';
echo 'Hello, '.$_POST['textfield'].'<br>';

You need to first submit the form with method set to post and only then you can get its value.

Example:

<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
...
<input name="textfield" type="text" id="textfield" value="Roger" />
...
<input name="submit" type="submit" id="submit" value="submit" />
</form>

PHP

if (isset($_POST['submit']))
{
    echo 'Hello, '.$_POST['textfield'].'<br>';
}
Sarfraz
Thanks man! Now it works. yet is there a way for php to echo out the default value ( or a new value once it is typed in ) without having to press the 'submit' button?
@user296516: You can get value only when form is submitted. If you meant without submitting form, then you can use the javascript for that, but it won't be avaliable to PHP.
Sarfraz
Yes, say `if(!isset($_POST['submit'])) { echo 'Hello, Roger.'; }`
Josh K
+2  A: 

If this is, exactly, your code then the problem is that the $_POST is not set yet since no form is submitted.

Soufiane Hassou
A: 

My guess it, that the server doesn't allocate $_GET and/or $_POST. You could check that in the php configuration.

Have a look if you can access the data via $_REQUEST, which should unify get and post data.

Peter Wippermann
But don't use `$_REQUEST` in production, only for debugging. It's bad practice to use it in live systems.
danilo
A: 

The guy is write

Akay
If you have a comment, then make a comment (except you can't since you haven't got enough points - earn some more first), don't answer questions with comments. We have no idea who "The guy" is. I think you mean "right".
David Dorward