SOLDIER-OF-FORTUNE
SOLDIER-OF-FORTUNE

Reputation: 1654

onclick submit check php

I have the following PHP script, But it seems to be always checking for the session and therefore echos out "please enter the correct code before the user has even entered a code.

I would like to at a click event before the code actually fires and does the check for the session and post of the form. How can this be done in PHP?

here is my code:

<?php

session_start();
    if (md5($_POST['norobot']) == $_SESSION['randomnr2'])   
    { 
        // here you  place code to be executed if the captcha test passes
            echo "Code correct - email should send now";

}   

else {  
        // here you  place code to be executed if the captcha test fails
            echo "Code incorect please try again";
    }

?> 

my submit buttons looks like this:

<input type="submit" id="submit_btn" class="button" value="Submit" />

Upvotes: 1

Views: 517

Answers (3)

Riz
Riz

Reputation: 10246

<?php
    session_start();
    if (isset($_POST['norobot'])) {
        if (md5($_POST['norobot']) == $_SESSION['randomnr2']) {
            // here you  place code to be executed if the captcha test passes
            echo "Code correct - email should send now";
        }
        else {
            // here you  place code to be executed if the captcha test fails
            echo "Code incorect please try again";
        }
    }
?> 

Upvotes: 2

Bono
Bono

Reputation: 4849

Why not do something like this? Check if the user has submitted anything, and if not don't execute the code.

<?php

session_start();

if(isset($_POST))
    if (md5($_POST['norobot']) == $_SESSION['randomnr2'])   
    { 
        // here you  place code to be executed if the captcha test passes
            echo "Code correct - email should send now";

}   

else {  
        // here you  place code to be executed if the captcha test fails
            echo "Code incorect please try again";
    }
}

?> 

Upvotes: 1

psx
psx

Reputation: 4048

If you want to check if the session variable is set, and has a non-blank value, you can do something like:

if(isset($_SESSION['THECODE']) && $_SESSION['THECODE']!='')
{
  // THECODE is set to something
}

============

UPDATE, as you added code:

<?php

session_start();
if(isset($_POST['norobot']) && isset($_SESSION['randonnr2']))
{
  if (md5($_POST['norobot']) == $_SESSION['randomnr2'])   
  { 
    echo "Code correct - email should send now";
  }
  else
  {  
    echo "Code incorect please try again";
  }
}

?> 

Upvotes: 2

Related Questions