Php 如何在我的提交表单中实现代码检查
我有以下表格Php 如何在我的提交表单中实现代码检查,php,html,verification,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* :</sp
<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>
从理论上讲,我可以使用这段代码,但我不知道在哪里实现它
function is_valid($code)
{
return in_array($code , explode(' ',file_get_contents('coderoute')));
}
编辑1:现在我有这个,我得到这个错误
<?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警告:
文件获取内容(/wp content/themes/wallstreet/codes.txt):未能
open stream:中没有此类文件或目录
/第4行的var/www/wordpress/wp content/themes/wallstreet/welcome.php
您需要检查的代码位于
$\u POST['Code']
中
因此,将其作为参数传递给是否有效
函数:
<?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();
?>
您需要检查的代码位于
$\u POST['code']
中
因此,将其作为参数传递给是否有效
函数:
<?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();
?>
您可以将JavaScript数组与代码一起使用,此数组将填充PHP,如果输入的代码不在数组中,submit按钮将不会提交表单 复制粘贴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>
函数检查\u代码()//◄■ 从窗体调用的函数。
{var arr=[您可以将JavaScript数组与代码一起使用,此数组将填充PHP,如果输入的代码不在数组中,submit按钮将不会提交表单
复制粘贴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>
函数检查\u代码()//◄■ 从窗体调用的函数。
{var arr=[
您已经提到了的PHP警告
,其中不存在这样的文件
。您可以提供codes.txt的绝对路径
,以检查它是否正常工作
你提到了PHP警告没有这样的文件存在。你可以提供codes.txt的绝对路径来检查它是否工作正常。如果(是有效的){add\u to\u file}
如果(是有效的){add\u to\u file}
现在我得到了这个错误,但是目录是正确的,我已经检查了权限[Thu Dec 15 22:27:01.063358 2016][:error][pid 7210][client 92.59.55.17:60354]PHP警告:文件获取内容(/wp content/themes/wallstreet/codes.txt):无法打开流:第4行的/var/www/wordpress/wp content/themes/wallstreet/welcome.php中没有这样的文件或目录现在我收到了这个错误,但是目录是正确的,我检查了权限[Thu Dec 15 22:27:01.063358 2016][:error][pid 7210][client 92.59.55.17:60354]php警告:文件获取内容(/wp content/themes/wallstreet/codes.txt):无法打开流:第4行的/var/www/wordpress/wp content/themes/wallstreet/welcome.php中没有这样的文件或目录HP通知:未定义变量:第6行的/var/www/wordpress/wp content/themes/wallstreet/welcome.php中的message\u book,Oops。很抱歉。我现在已经修改了答案。php通知:未定义变量:message\u book in/var/www/wordpress/wp content/themes/wallstreet/welcome.php,第6行,Oops。很抱歉。我现在修改了答案。
$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);
}