如何在我的提交表单中实现代码检查

时间:2016-12-15 20:47:34

标签: php html verification

我有以下表格

    <form action="/wp-content/themes/wallstreet/welcome.php" method="post" class="basic-grey">
<h1>Form
<span>Please fill all the texts in the fields.</span>
</h1>
<label>
<span>Your Nickname* :</span>
<input id="name" type="text" name="name" placeholder="It will appear in the text" />
</label>

<label>
<span>Your Email* :</span>
<input id="email" type="email" name="email" placeholder="Valid Email Address" />
</label>

<label>
<span>Message* :</span>
<textarea id="messagebook" name="messagebook" placeholder="The text that will appear" maxlength="80"></textarea>
</label>
<label>
<span>Code* :</span>
<input id="code" type="text" name="code" placeholder="The Code That we sent to your email" maxlength="8" />
</label>
<label>
<span> </span>
<input type="submit" class="button" value="Send" />
</label>
</form>

使用以下php,这个php基本上将消息值发布到txt文件

<?php
$var = $_POST['messagebook'];
file_put_contents("/var/www/wordpress/wp-content/themes/wallstreet/data.txt", $var . "\n", FILE_APPEND);
exit();
?>

但我希望提交按钮仅在我的代码字段与存储在txt文件中的代码匹配时才有效

zACHAS5r
rKUzob3X
omqYjVQZ
BeF375BG
rFKQomMX
y8EVBTGH
Z7icxNoD
wnZ5qBvK
ftbPiCZa
sXJKDETK
wYDVLDPd
AjURjBdZ
LZR4fbtk
gmFY89TV
BAWDxpZ2
bGLLd9Az
qg4C93wN
YJnrDh2c
jwH6hV9h
tm3S4f5j
MU2ikfbu
ZXnUpfmY
hijZPTk4
C2oWha3T
irTg9oUA
jmjLDvL3
jUbiBtJo
gCCAQx6Z

理论上我可以使用此代码,但我不知道在哪里实现它

    function is_valid($code)
{
return in_array($code , explode(' ',file_get_contents('coderoute')));
} 

EDIT1:好像我有这个,我得到这个错误

 <?php
    function is_valid($code)
    {
    return in_array($code , explode(' ',file_get_contents("/wp-content/themes/wallstreet/codes.txt")));
    } 
    $code = $_POST['code'];
    if (is_valid($code)) { 
        $var = $_POST['messagebook'];
        file_put_contents("/var/www/wordpress/wp-content/themes/wallstreet/data.txt", $var . "\n", FILE_APPEND);
    }
    exit();
?>
  

PHP警告:   file_get_contents(/wp-content/themes/wallstreet/codes.txt):未能   open stream:没有这样的文件或目录   第4行的/var/www/wordpress/wp-content/themes/wallstreet/welcome.php,

3 个答案:

答案 0 :(得分:1)

您需要检查的代码位于$_POST['code']

因此将其作为参数传递给is_valid函数:

<?php
    $code = $_POST['code'];
    if (is_valid($code)) { 
        $var = $_POST['messagebook'];
        file_put_contents("/var/www/wordpress/wp-content/themes/wallstreet/data.txt", $var . "\n", FILE_APPEND);
    }
    exit();
?>

答案 1 :(得分:1)

[::-1]

您已提及$code = $_POST['code']; $message_book = $_POST['messagebook']; if(is_valid($code)) { file_put_contents('/var/www/wordpress/wp-content/themes/wallstreet/data.txt', "{$message_book}\n", FILE_APPEND); exit(); } function is_valid($code) { $codes = file('/var/www/wordpress/wp-content/themes/wallstreet/codes.txt', FILE_IGNORE_NEW_LINES); return in_array($code, $codes); } 的{​​{1}}。您可以提供PHP Warning的绝对路径来检查它是否正常工作。

答案 2 :(得分:0)

您可以使用带有代码的JavaScript数组,此数组将填充PHP ,如果输入的代码不在数组中,则提交按钮将不会提交表单。

将下一个代码复制粘贴到PHP文件中,然后在浏览器中打开它:

<html>
  <head>
    <script type="text/javascript">
function check_code () // ◄■ FUNCTION CALLED FROM THE FORM.
{ var arr = [ <?php // ▼ FILL JAVASCRIPT ARRAY WITH CODES FROM FILE ▼
              $arr = explode( PHP_EOL,file_get_contents('coderoute.txt') );
              echo "'" . implode( "','",$arr ) . "'";
              ?> ];
  var code = document.getElementById("code"); // ◄■ FIELD IN FORM.
  if ( arr.indexOf( code.value ) == -1 ) // ◄■ SEARCH CODE IN ARRAY.
       { alert("Code not found.");
         return false; // ◄■ FORM WILL NOT BE SUBMITTED.
       }
  return true; // ◄■ FORM WILL BE SUBMITTED.
}
    </script>
  </head>
  <body>
    <form action="somescript.php" onsubmit="return check_code()"> <!-- ◄■ JS FUNCTION -->
      <input type="text" id="code" name="code"/> <!-- ◄■ CODE FIELD -->
      <br/>
      <input type="submit" value="Submit"/>
    </form>
  </body>
</html>

注意JavaScript数组是如何用PHP填充的,PHP脚本从文件中读取代码并将它们作为JavaScript数组的数组项进行回显。 右键单击页面以查看源代码并观察JavaScript数组的填充方式。