【问题标题】:Post a value from php page to the same page doesn't work将值从 php 页面发布到同一页面不起作用
【发布时间】:2018-02-27 07:42:10
【问题描述】:

我编写了一个 php 代码来保存输入的发布值,它发送到同一页面。事情就在这里,什么都不会发布。 代码如下:

<form name="form1" method="post" action="../page1.php" id="form1">
paid code: 
 <input id="pcode" type="text" />
 <input type="submit" />
 <?php
 if(empty($_POST["pcode"])){
    echo "<br/> This Field Cannot Be Empty";
 }
 else{

$num = $_POST["pcode"];

# To get a shorter version of the hash, just use substr
$hash = substr(md5($num), 0, 10);
echo $hash;
file_put_contents("code.txt",  $hash ."\r\n", FILE_APPEND);
}
?>
</form>

它是 page1.php。我是不是做错了什么?

【问题讨论】:

  • 保持空白...没有什么可写的

标签: php html file post


【解决方案1】:

问题源于缺少分配给文本元素的name - ID 属性未在 POST 请求中发送。此外 - 您可以简单地省略 action 表单,以便让表单自行发布。

<form method='post'>
    paid code: 

    <input name='pcode' type='text' />
    <input type='submit' />

    <?php
        if( empty( $_POST['pcode'] ) ){
            echo '<br/> This Field Cannot Be Empty';
        } else{

            $num = $_POST['pcode'];

            # To get a shorter version of the hash, just use substr
            $hash = substr(md5($num), 0, 10);
            echo $hash;
            file_put_contents('code.txt',  $hash .'\r\n', FILE_APPEND);
        }
    ?>
</form>

【讨论】:

    【解决方案2】:

    更改这些行:

    <form name="form1" method="post" action="../page1.php" id="form1">
    <input id="pcode" type="text" />
    

    到:

    <form name="form1" method="post" action="" id="form1">
    <input id="pcode" name='pcode' type='text' />
    

    它会起作用的。

    【讨论】:

      【解决方案3】:

      你改变这条线,

      <form name="form1" method="post" action="../page1.php" id="form1">
      

      <form name="form1" method="post" action="" id="form1">
      

      它应该可以工作。

      【讨论】:

        【解决方案4】:

        使用action="page1.php"action="" 代替action="../page1.php"

        【讨论】:

          猜你喜欢
          • 2013-06-05
          • 1970-01-01
          • 2017-10-12
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2020-10-02
          • 2011-05-21
          相关资源
          最近更新 更多