【问题标题】:Echoed form data and manupulation with GET and POST in PHP在 PHP 中使用 GET 和 POST 回显表单数据和操作
【发布时间】:2018-10-20 05:44:15
【问题描述】:

实际上我需要在同一个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>";
}

【问题讨论】:

  • &lt;input type='submit' value='Submit'&gt;更改为&lt;input type='submit' name='submit'&gt;
  • 更改 - &lt;form action=$_SERVER['PHP_SELF']&gt; - 到 &lt;form action='{$_SERVER['PHP_SELF']}' method="post"&gt;

标签: php html forms echo


【解决方案1】:

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

将 $_SERVER['PHP_SELF'] 更改为 #

<?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;
?>

【讨论】:

    猜你喜欢
    • 2011-05-07
    • 1970-01-01
    • 2011-05-05
    • 2021-08-26
    • 1970-01-01
    • 1970-01-01
    • 2016-10-18
    • 2012-11-29
    • 2012-05-10
    相关资源
    最近更新 更多