'how to load the php file in the form action only after php submit button was executed

I am not a PHP expert. I have a form that has Captcha validation image. the form has a button "submit", and the code for the submit is within the same page (let's call it "code 1"):

<form method="post" name="emailForm" action="email.php">
   Name:<input type="text" name="name"><br>
   Email Add:<input type="text" name="email"><br>
   Message:<textarea rows="10" cols="40" name="message">/textarea><br>
   <input name="SubmitCF" type="submit" value="Send Email" class="button1"><br>
</form>

As I understand, the php concept is that the button action code in done by the following code fragment, and not by loading the file stated in the form's "action" tag, which is the php file "email.php" (in the form's declaration above) as in the following code fragment (let's call it "code 2"):

<?php
    if (isset($_POST['SubmitCF'])) {
        // Validate Captcha code ...
    }
}
?>

The problem I face is as follows: when I have the "action="email.php" " in the form declaration, then the "email.php" is run but not the validation captcha code in "code 2", therefore the Captcha code is not validated (which is a bug). but when I delete the "action="email.php" from the form's declaration, then "code 2" is executed, and the "email.php" is not loaded and executed, and is it also a bug.

What is the way to validate the Captcha code, and only after the Captcha is validated, make the "email.php" in the form;s "action" declaration load and run?

how to run the "submit" button code within the same page, and only upon confirmation, load and execute the php file which is in the form's action declaration.

Many thanks to the answering members for their help and time.



Solution 1:[1]

You can do something like this:

  1. In code 1 file, remove action="email.php" first, and let the form submit to the same page.
  2. Check if the captcha is answered correctly on the page, if yes, redirect to the page email.php and transfer the current POST values.
  3. Do what is in email.php

I found this question might be helpful, you can check it out.

EDIT: You can do sth like this via output buffering:

ob_start(); //place this before the first output or header, or the first line simply
header(sth…);
header(sth…);
echo “sth outputs”;
header(“Location: http://yourpage”);

And then transfer the data in the link or use session:

//from the reference question
function do_post_request($url, $data, $optional_headers = null)
{
  $params = array('http' => array(
              'method' => 'POST',
              'content' => $data
            ));
  if ($optional_headers !== null) {
    $params['http']['header'] = $optional_headers;
  }
  $ctx = stream_context_create($params);
  $fp = @fopen($url, 'rb', false, $ctx);
  if (!$fp) {
    throw new Exception("Problem with $url, $php_errormsg");
  }
  $response = @stream_get_contents($fp);
  if ($response === false) {
    throw new Exception("Problem reading data from $url, $php_errormsg");
  }
  return $response;
}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1