Reputation: 87
i have the following form
<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>
which uses the following php, this php basically posts the message value into a txt file
<?php
$var = $_POST['messagebook'];
file_put_contents("/var/www/wordpress/wp-content/themes/wallstreet/data.txt", $var . "\n", FILE_APPEND);
exit();
?>
but i want the submit button only work if my code field matches with the codes that are stored in a txt file like this
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 i could make it work with this code, but i dont know where to implement it
function is_valid($code)
{
return in_array($code , explode(' ',file_get_contents('coderoute')));
}
EDIT1: Currrently i have this, and i get this error
<?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 Warning: file_get_contents(/wp-content/themes/wallstreet/codes.txt): failed to open stream: No such file or directory in /var/www/wordpress/wp-content/themes/wallstreet/welcome.php on line 4,
Upvotes: 0
Views: 75
Reputation: 1702
$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);
}
You've mentioned PHP Warning
of No such file exists
. You could provide absolute path of codes.txt
to check if it works right.
Upvotes: 1
Reputation: 10381
You can use a JavaScript array with the codes, this array will be filled with PHP, if the entered code is not in the array, the submit button will not submit the form.
Copy-paste next code in a PHP file and open it in your browser :
<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>
Notice how the JavaScript array is filled with PHP, the PHP script reads the codes from the file and echo them as the array items of the JavaScript array. Right click the page to see the source code and watch how the JavaScript array was filled.
Upvotes: 0
Reputation: 54831
Code you need to check is in $_POST['code']
.
So pass it as argument to is_valid
function:
<?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();
?>
Upvotes: 1