使用PHP中的GET和POST回应表单数据和manupulation

时间:2018-05-10 02:39:36

标签: php html html5 forms echo

实际上我需要回显表单并在同一个php页面中获取表单数据或发送到另一个php页面,准备了伪代码,因为我无法在此处发布我的原始代码。

  <?php echo "<html><body>";
  if(isset($_POST['submit']))
{
    $name = $_POST['firstname'];
    echo "User Has submitted the form and entered this name : <b> $name </b>";
}
echo"<form action=$_SERVER['PHP_SELF']>
    First name:<br>
    <input type='text' name='firstname' value='John'><br>
    Last name:<br>
    <input type='text' name='lastname' value='Rambo'><br><br>
    <input type='submit' value='Submit'>
    </form></body></html>";
?>

php页面被调用/加载成功加载。表单也正常工作。但是        form action=$_SERVER['PHP_SELF']效果不佳,因为下面的代码也无效。

 if(isset($_POST['submit']))
{
    $name = $_POST['firstname'];
    echo "User Has submitted the form and entered this name : <b> $name </b>";
}

1 个答案:

答案 0 :(得分:0)

这是经过测试的代码。与你想要的相似。

将$ _SERVER [&#39; PHP_SELF&#39;]更改为#

<?php
error_reporting(E_ERROR );
$first = '';
$last = '';
if (intval($_POST['sub'])){
  $first = $_POST['firstname'];
  $last = $_POST['lastname'];
}

echo <<<EOT
<html><head></head><body>
<form action="#" method="post">
    First name:<br>
    <input type='text' name='firstname' value="$first"><br>
    Last name:<br>
    <input type='text' name='lastname'  value="$last"><br><br>
    <input type='submit' value='Submit'/>
    <input type="hidden" name="sub" value=1/> 
    </form></body></html>
EOT;
?>