0

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.

ilanK
  • 3
  • 2
  • When you're done with your validation use `header()` to redirect to a different file, in your case `email.php` – brombeer May 14 '22 at 12:24

1 Answers1

0

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;
}
Henryc17
  • 851
  • 4
  • 16
  • Thanks for finding the time for answering. I saw on the Web that redirecting is done by the Header tag, but it is not possible if the Header tag was already loaded to the page, which is what happen in my code. So how can I redirect to the email.php file? thanks again. – ilanK May 15 '22 at 04:22
  • @ilanK, if possible, you can use [output buffering](https://www.php.net/manual/en/book.outcontrol.php) to do this with header. Or, you may want to do it with [session](https://www.php.net/manual/en/reserved.variables.session.php) global variable. – Henryc17 May 15 '22 at 08:50
  • Hello Henryc, thanks for answering. I use "start_session()" to keep all session variables, but the problem is still how to reload a new php file. how the output buffering might helps me? it will flush the old header before sending the header of the "email.php" file? thanks. – ilanK May 16 '22 at 17:24
  • @ilanK, I’ve updated the ans, see if it helps ;) – Henryc17 May 17 '22 at 05:35
  • Thanks a lot Henryc, I am trying to understand the code you listed above, it is quite complicated as I am a novice user of php. I will try to do it and update you on the results. Thanks a lot again. – ilanK May 21 '22 at 10:23