如何在加载时自动点击“不提交”按钮,具体取决于PHP的结果[关闭]

问题描述:

,I'm working on a website as a developer using PHP ,

what I have now is a pop up review form , which appears when the user press a button,

the problem is I'm validating it using PHP and when you press on the submit the validation occurs server side as you know and when it loads the page again you will find the form is gone , and you have to press that button again to see the result of the validation like: "please enter a valid email..",

is there any way I could press that button automatically if isset($_POST) or something similar ?

I can provide the website link if it is allowed , just tell me if you need it.

thanks in advance

,我正在使用PHP作为开发人员在网站上工作, p>

我现在拥有的是一个弹出式评论表单,当用户按下按钮时出现, p>

问题是我正在使用PHP验证它,当你按下提交时 验证发生在服务器端,如你所知,当它再次加载页面时,你会发现表单已经消失,你必须再次按下该按钮才能看到验证的结果 like:“请输入有效的电子邮件..”, p>

有什么办法可以自动按下该按钮,如果是isset($ _ POST)或类似的东西? p>

我可以提供网站链接,如果它 是允许的,只要告诉我你是否需要它。 p>

提前谢谢 p> div>

You could have something like

<?php if (isset($_POST)...){ ?>

    <script>
        // here you could call the Javascript function which is called when the button is clicked
       function onLoad() {
           yourFunction();
       }
    </script>

<?php } ?>

<body onload="onLoad()">
...

As War10ck pointed out in the comments, if you're going to use this approach, you should also wrap the body onload="onLoad()" tag within the if statement so that if the pop up should not show, and because of that the onLoad function is not included in the HTML, you won't get an undefined function error in Javascript.

Another solution would be to not hide the form in the first place i.e something like this:

<?php
    $class = "hidden";
    if (isset($_POST)...) { 
        $class = ""; // make the form not hidden if $_POST...
    }
?>
<div id="form" class="<?php echo $class; ?>"> ...

where

.hidden {
    display:none;
}