0

I have a html form on my page. Im using a php file to send an email from the form, and after a successful email send its redirects back to the html form. After that i want to fade in a bootstrap success-alert. Is there any event that can handle this or anyway to solve this problem?

This is my JS now, but its dont run because if I hit the send button, its call the php file.:

<script type="text/javascript">
            $(document).ready(function() {
                $('#btnSubmit').click()(function(){
                    $('#myAlert').show('fade');
                });
            });
            </script>
alesszia
  • 23
  • 3
  • *"Is it possible the invoke a js in html after php from validation and redirection?"* - the PHP code and the Javascript code runs on different computers, on different time frames. The PHP code completed its execution long before the Javascript starts. – axiac Nov 07 '17 at 15:48
  • @axiac, Yes and I understand this, I want to know is there anything in js that can handle "i was redicrected from..." or something else like this. – alesszia Nov 07 '17 at 15:52
  • You would not do an actual redirection, you would call the alert in the success callback of the AJAX function after the AJAX function calls the PHP to send the email. – Jay Blanchard Nov 07 '17 at 16:16

2 Answers2

1

I'm assuming you need show alert after the form request is processed and page is redirected to original form page, In that case You need to pass some parameter as a flag in query string while redirecting from server side. On form page you need to check for that flag and show alert.

On server side while redirecting

header("Location:form_page.php?status=success");

On form page

$status = $_GET['status'];

Assign value of $status to js variable and show alert.

Vishal
  • 639
  • 7
  • 32
1

Use ajax to achieve that. Catch the submit event with

$("#btnSubmit").submit(function(){//your ajax});

In the success do your $('#myAlert').show('fade');

You have a good example here : How to submit html form without redirection?

Romain B.
  • 656
  • 6
  • 15