I am trying to check to see if the text the user has entered in my textbox called 'valid' is equal to a variable containing a randomly generated 6 digit number.
$validkey = rand(100000,999999);
My question is; in an 'if' statement how can I check to see if the user has entered the same numbers in the textbox as the $validkey variable? It would be embedded within an if that checks if a button called 'next' has been clicked. So when the button is clicked it will do the check.
My current code is as follows:
<form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">
E-mail: <input type="text" name="email">
<span class="error">* <?php echo $emailErr;?></span>
<br><br>
<input type="submit" name="submit" value="Submit">
<br>
<input type="text" name="valid">
<input type="submit" name="next" value="Next">
</form>
<?php
$validkey = rand(100000,999999);
if (isset($_POST["submit"]))// the user has submitted the form
{
if (preg_match("/([\w\-]+\@[\w\-]+\.[\w\-]+)/",$email))
{
if (isset($_POST["email"])) // Check if the "from" input field is filled out
{
$from = "Simon"; // sender
$subject = "Validation Key";
$message = "Your Validation key: ".$validkey." enter this key into the required field to gain access to the Cyber Security Health Check";
$message = wordwrap($message, 70);
mail($_POST["email"],$subject,$message,"From: $from
");
echo '<span class="success">We have sent a validation key to your email!</span>';
}
}
}
if (isset($_POST["next"]))
{
if ($_POST['valid'] == $validkey)
{
echo '<span class="success">CORRECT!</span>';
}
else
{
echo '<span class="error">INCORRECT!</span>';
}
}
?>
I apologise if I worded this question wrong or I have not provided enough information, I am a bit of a PHP newb.
Thanks for any help in advance.
Use the intval
function to parse the input value, then compare it to your generated value.
Is this the sort of thing?
if isset($_REQUEST['next']) { // next has been pressed
if($_REQUEST['textentry'] === $validkey) { // entered text identical to the valid key
// handle correct entry
}
else {
// handle incorrect entry
}
}
else {
// handle some other button being pressed
}
I don't know if it help or I understood your question correctly
did you try if ( $_POST['valid'] === $validkey){}
Or you could also use jquery Ajax request, No need to submit next button when the user will leave the textbox ajax request will verify, you could display the message based on that.
$(document).ready(function(){
$("input[name='valid']").on('blur', function(){
var text = $("input[name='valid']").val();
var data = 'valid=' + text;
$.ajax({
type:"POST",
url:"checkvalidkey.php",
data: data,
success: function(html){
$("p").html(html)
}
})
});
});
Try this code. This is very basic code. Make necessary changes you need, like adding form and all and use it for your need.
<?php $validkey="hello" ?>
<script type="text/javascript">
function check_value()
{
var code = document.getElementById("code").value;
if(code=='<?php echo $validkey; ?>')
{
alert('Matches');
}
}
</script>
<input type ="text" name="code" id="code" />
<button onclick="check_value();">Click</button>
Hope this is what you were looking for.