将值从 php 页面发布到同一页面不起作用



i编写一个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 ."rn", FILE_APPEND);
}
?>
</form>

是page1.php。我做错了吗?

问题源于缺乏分配给文本元素的name - 邮政请求中未发送ID属性。另外 - 您可以简单地省略表单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 .'rn', FILE_APPEND);
        }
    ?>
</form>

更改以下行:

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

to:

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

它将起作用。

您更改此行,

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

to

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

它应该起作用。

而不是action="../page1.php",使用action="page1.php"action=""

最新更新