-1

Is it possible to display the echo content which is HTML on my website after I press on my button (type submit), the POST request is made on the same site. When I press on my button it's refreshing the website and making the POST request but I don't want that it's automatically refreshing the site, I want to display the alert on the current site if possible.

<button id="submit" type="submit" class="btn btn-darkred rounded mr-xl-5" style="width:300px">LOGIN</button>

This is the PHP code

<?php

    if ($_SERVER["REQUEST_METHOD"] == "POST") {

        $secretKey = 'captchaSecret';
        $captcha = $_POST['g-recaptcha-response'];

        if(!$captcha){
        echo '<p class="alert alert-warning">Please check the the captcha form.</p>';
        exit;
        }
        $ip = $_SERVER['REMOTE_ADDR'];
        $response=file_get_contents("https://www.google.com/recaptcha/api/siteverify?secret=".$secretKey."&response=".$captcha."&remoteip=".$ip);
        $responseKeys = json_decode($response,true);

        if(intval($responseKeys["success"]) !== 1) {
        echo '<p class="alert alert-warning">Please check the the captcha form.</p>';
        } else {
            echo 'successful';
        }

    }
?>

This is what I want to be displayed on the current site & not a blank page with the alert:

echo '<p class="alert alert-warning">Please check the the captcha form.</p>';
David Buck
  • 3,752
  • 35
  • 31
  • 35
ιcσяεx
  • 19
  • 1
  • 4
  • what does your `
    – Mech Mar 18 '20 at 01:04
  • `
    ` registration is the same site
    – ιcσяεx Mar 18 '20 at 01:06
  • you can just change `action="registration"` to `action=""` in that case. – Mech Mar 18 '20 at 01:08
  • I actually meant that its refreshing the page and showing me the content of echo which is my alert, I actually want that my alert is displayed on the same page and not on a blank one. – ιcσяεx Mar 18 '20 at 01:09
  • 2
    You'll need to use AJAX (through JavaScript) if you want to update the page without refreshing it. – Obsidian Age Mar 18 '20 at 01:11
  • I understand now. Yes, Obsidian Age is correct. To do what you are looking for, it needs to be while the page is still active which is going to be through the use of JS. You will probably want to research something along the lines of JS form validation. – Mech Mar 18 '20 at 01:12
  • If the Captcha is not valid, you can display the form again with the appropriate messages. Alternatively, you can use [AJAX](https://stackoverflow.com/questions/1510011/how-does-ajax-work) to submit the form asynchronously and display the response from your PHP script, all without refreshing the page. Also see [Submit form without page reloading](https://stackoverflow.com/questions/2866063/submit-form-without-page-reloading). – showdev Mar 18 '20 at 01:12

1 Answers1

0

Please make some changes to your PHP code as I mentioned below.

<?php
  $alertMsg = "";
  if ($_SERVER["REQUEST_METHOD"] == "POST") {
    $secretKey = 'captchaSecret';
    $captcha = $_POST['g-recaptcha-response'];
    if(!$captcha){
     $alertMsg = '<p class="alert alert-warning">Please check the the captcha form.</p>';
    } 
    else {
     $ip = $_SERVER['REMOTE_ADDR'];
     $response=file_get_contents("https://www.google.com/recaptcha/api/siteverify?secret=".$secretKey."&response=".$captcha."&remoteip=".$ip);
     $responseKeys = json_decode($response,true);
     if(intval($responseKeys["success"]) !== 1) {
       $alertMsg = '<p class="alert alert-warning">Please check the the captcha form.</p>';
     } else {
      $alertMsg = '<p class="alert alert-success">successful</p>';
     }
    }
  }
?>

Add line - <?php echo $alertMsg; ?>

Wherever you want to show that message.

Alok Mali
  • 2,821
  • 2
  • 16
  • 32