2016-12-15 47 views
0

我有以下形式如何實現代碼檢查到我的提交表單

<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 

Theorically我可以使它與此代碼工作,但我不知道哪裏來實現它

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

EDIT1:Currrently我都這樣了,我得到這個錯誤

<?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):未能成功 打開流: /var/www/wordpress/wp-content/themes/wallstreet/welcome.php沒有這樣的文件或目錄在線4,

+0

'if(is_valid){add_to_file}' –

回答

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); 
} 

您剛纔提到的PHP WarningNo such file exists。你可以提供codes.txt的絕對路徑來檢查它是否正確。

+0

PHP注意:未定義的變量:message_book在/var/www/wordpress/wp-content/themes/wallstreet/welcome.php在第6行, – romanturbo

+0

糟糕。對於那個很抱歉。我現在修改了答案。 – Perumal

+0

我愛你的隊友,現在它的作品! – romanturbo

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(); 
?> 
+0

現在我得到這個錯誤,但是目錄是正確的並且我檢查了權限[Thu Dec 15 22:27:01.063358 2016] [:error] [pid 7210] [client 92.59.55.17:60354] PHP警告:file_get_contents(/wp-content/themes/wallstreet/codes.txt):無法打開流:在/ var/www/wordpress/wp-content/themes/wallstreet中沒有這樣的文件或目錄/ welcome.php 4行 – romanturbo

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數組是如何填充的。