简体   繁体   中英

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!

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

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.

如果这恰好是您的代码,则问题在于尚未设置$ _POST,因为未提交任何表单。

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM