【问题标题】:How would I display PHP echo HTML content on my current site?如何在当前站点上显示 PHP 回显 HTML 内容?
【发布时间】:2020-03-18 21:00:23
【问题描述】:

按下按钮(输入提交)后,是否可以在我的网站上显示 HTML 的回显内容,POST 请求是在同一网站上发出的。 当我按下按钮时,它会刷新网站并发出POST 请求,但我不希望它自动刷新网站,如果可能的话,我想在当前网站上显示警报。

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

这是 PHP 代码

<?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';
        }

    }
?>

这是我希望在当前网站上显示的内容,而不是带有警报的空白页面:

echo '<p class="alert alert-warning">Please check the the captcha form.</p>';

【问题讨论】:

  • 你的&lt;form action=是什么?
  • &lt;form class="mt-5 form-material" style="width:300px" method="POST" action="registration"&gt;注册的是同一个站点
  • 在这种情况下,您只需将action="registration" 更改为action=""
  • 我实际上的意思是刷新页面并向我显示作为我的警报的 echo 的内容,我实际上希望我的警报显示在同一页面上而不是空白页面上。
  • 如果您想在不刷新页面的情况下更新页面,则需要使用 AJAX(通过 JavaScript)。

标签: php html bootstrap-4


【解决方案1】:

请按照我在下面提到的那样对您的 PHP 代码进行一些更改。

<?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>';
     }
    }
  }
?>

添加行 - &lt;?php echo $alertMsg; ?&gt;

无论您想在哪里显示该消息。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-04-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-02-13
    相关资源
    最近更新 更多