Roger Travis
Roger Travis

Reputation: 8538

PHP won't echo out the $_POST

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!

Upvotes: 0

Views: 1627

Answers (5)

Sarfraz
Sarfraz

Reputation: 382616

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>';
}

Upvotes: 7

Peter Wippermann
Peter Wippermann

Reputation: 4569

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.

Upvotes: 0

Soufiane Hassou
Soufiane Hassou

Reputation: 17750

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

Upvotes: 2

danp
danp

Reputation: 15241

  • 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.

Upvotes: 0

Danilo Bargen
Danilo Bargen

Reputation: 19432

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.

Upvotes: 3

Related Questions