0

Couldn't find any solution on this yet, so I'm posting it here.

I have the following code:

<?php
//array: key=> value
$begrippen = array(
    "agrarisch" => "jagers en boeren",
    "cultuur" => "jagers en boeren",
    "jagers-verzamelaars" => "jagers en boeren",
    "landbouwsamenleving" => "jagers en boeren",
    "burgerschap" => "grieken en romeinen",
    "christendom" => "grieken en romeinen",
);
$message1 = 'Goedzo!';
$message2 = 'Fout!';

$random_key = array_rand($begrippen);
$value = $begrippen[$random_key];
echo "Begrip: $random_key <br />";
?>

<form method="POST">
<input type="text" autocomplete="off" name="input1" autofocus>
</form>

<?php
if($_POST['input1'] == $value){
    echo "<SCRIPT> alert('$message1'); </SCRIPT>";
}else{
    echo "<SCRIPT> alert('$message2'); </SCRIPT>";
};
?>

It takes a random key from my array, takes the value and puts that in $value. When I enter input in my textbox, I want it to compare with $value and let it show a message (good or wrong). Yet something goes wrong and I don't know what because sometimes it says it's good, and sometimes it's wrong (while the answer was correct).

4
  • What's the error you're getting ? Commented Jun 16, 2014 at 5:45
  • Where is your submit button ? You need to check whether the form is submitted or not. Commented Jun 16, 2014 at 5:48
  • Please explain your issue more...Where is the submit button... Commented Jun 16, 2014 at 5:49
  • Sorry, forgot the submit button in this code, but with submit button it wwasnt working either. The problem was, when I entered the correct value to random key shown, it still gave me message2(the wrong message) Commented Jun 16, 2014 at 6:09

2 Answers 2

1

Try this,

You can add hidden field in your form and assign the value to be refer.

<form method="POST">
    <input type="text" autocomplete="off" name="input1" autofocus>
    <input type="hidden" name="inputref" value="<?php echo $value;?>">
    <input type="submit" name="submit" value="Submit">
</form>

in PHP: your condition will be

if(isset($_POST['submit'])){

   if($_POST['input1'] == $_POST['inputref']){
   ... Your code
   }
}
Sign up to request clarification or add additional context in comments.

Comments

0

I don't know without form submitting how this will work? So add a button as submit inside your form.

You need to do change your logic When POST request called.

<?php
if($_SERVER['REQUEST_METHOD'] == 'POST' && isset($_POST['check'])){
    //array: key=> value
    $begrippen = array(
       "agrarisch" => "jagers en boeren",
       "cultuur" => "jagers en boeren",
       "jagers-verzamelaars" => "jagers en boeren",
       "landbouwsamenleving" => "jagers en boeren",
       "burgerschap" => "grieken en romeinen",
       "christendom" => "grieken en romeinen",
   );
   $message1 = 'Goedzo!';
   $message2 = 'Fout!';

   $random_key = array_rand($begrippen);
   $value = $begrippen[$random_key];
   echo "Begrip: $random_key <br />";
   if($_POST['input1'] == $value){
      echo "<SCRIPT> alert('$message1'); </SCRIPT>";
   }else{
      echo "<SCRIPT> alert('$message2'); </SCRIPT>";
   }
}
?>
<form method="POST">
   <input type="text" autocomplete="off" name="input1" autofocus />
   <input type="submit" value="Check" name="check" />
</form>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.