0

,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

  • 2
    If you want an answer that suits your needs, post a reduced example of your code. You might want to look at [Ajax](http://stackoverflow.com/questions/2866063/submit-form-without-page-reloading). – blex Aug 11 '15 at 20:36
  • `` – ctwheels Aug 11 '15 at 20:42
  • 2
    This would be a great place for you to implement an ajax call into your code so that the page wouldn't have to be reloaded for the validation to take place... – War10ck Aug 11 '15 at 20:44

1 Answers1

0

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;
}
squill25
  • 1,198
  • 6
  • 20
  • 1
    If you did this approach, you'd want to add the `` statement within the `if` statement. Then inside an `else` statement add ``. Otherwise you're probably going to get an `undefined` function call error in the JavaScript console of your browser along with potential undesirable effects due to the fact that the body onload function will fire every time regardless of whether you've declared the `onLoad` function. – War10ck Aug 11 '15 at 20:41
  • @War10ck Absolutely right. I'll update the answer. – squill25 Aug 11 '15 at 20:46