Jump to content

Php FOR or WHILE Question... I think


eagle1771

Recommended Posts

Hello Everyone,

 

In my spare moments I have been trying to learn Php and a little MySQL.

 

I have a script which writes the contents of a form to a database and emails me the results, after checking the CAPTCHA form field input. The problem that I am having is that regardless of the results of the Captcha test, the script continues as if the Captcha test was a success instead of a failure.

 

Do I need a WHILE or a FOR statement? I kind of know what the problem is but not how to write it. Here is the relevant code:

 

<?php
error_reporting(E_ALL);
session_start();

$errors = array(); // set the errors array to empty, by default
$fields = array(); // stores the field values
$success_message = "";
$captcha_success_message = "CAPTCHA was entered correctly - Thank You";
$captcha_failure_message = "CAPTCHA was entered incorrectly - Please try again";

// more code, etc;

// test for valid captcha

if(isset($_POST["captcha"]))
if($_SESSION["captcha"]==$_POST["captcha"])
{ 
    //CAPTCHA is valid write to database and send email;
    echo $captcha_success_message;
}
else 
{ 
    echo $captcha_failure_message;
}


// create variables from form data

if ($_POST) {
foreach($_POST as $fn => $v) {
   $v = trim($v) ;
   $$fn = $v ;
    }
}

   // make connection to server, email results, etc.;

?>

 

Thank you in advance for any help.

 

Ralph Moore

Link to comment
https://forums.phpfreaks.com/topic/104445-php-for-or-while-question-i-think/
Share on other sites

 

I think you need to exit your code when the test fails:

 

<?php
if($_SESSION["captcha"]==$_POST["captcha"])
{ 
    //CAPTCHA is valid write to database and send email;
    echo $captcha_success_message;
}
else 
{ 
    echo $captcha_failure_message;
    exit;
}
?>

moselkady,

 

I tried using the "exit;" statement and it certainly does exit the script. I end up with my error statement displayed on a blank page. What I am trying to accomplish is to allow the visitor to correct their error and then continue with their form submission.

 

That is why I thought I might need something along the lines of an IF statement. Like, IF the error exists loop until the error is corrected, and then continue with the script.

 

Any ideas on how to do this?

 

Thanks.

 

Ralph Moore

Can anyone tell me if the following code makes sense, or is the rest of the script going to run regardless of whether the captcha test is a success or a failure (not a match)?

 

// more code above;

// test for valid captcha;

if(isset($_POST["captcha"]))

//If CAPTCHA is NOT valid - display error message for additional user input;

if ($_SESSION["captcha"]!==$_POST["captcha"]) { 
    echo $captcha_failure_message;
}
elseif ($_SESSION["captcha"]==$_POST["captcha"])
{ 
    //CAPTCHA is valid - write to database and send email;
    echo $captcha_success_message;
}
endif

// script continues;

 

Thanks,

 

Ralph Moore

 

I assume that you have a form and when it is submitted you check a value and display a success or failure message. In the case of failure you also display the form again for re-submission. Here is a sample code "test.php" that you may want to modify to fit your project:

 

<?php
// test.php

// function to display the form
function display_form()
{
?>
<form action="test.php" method="post">
CAPTCHA: <input type="text" name="captcha">
<br>
<input type="submit" value="send">
</form>
<?php
}

// this part executes when captcha submitted
if(isset($_POST["captcha"]))
{
    if($_SESSION["captcha"]==$_POST["captcha"])
    { 
        //CAPTCHA is valid write to database and send email;
        echo $captcha_success_message;
    }
     else 
    { 
        echo $captcha_failure_message;
        display_form();
    }
}
else display_form();

?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.