G
G
Grisha0222021-06-10 03:46:56
PHP
Grisha022, 2021-06-10 03:46:56

How to show a popup window after submitting a form?

I have a form that sends a username and a comment. This is all done with phpmailer. In my case, after submitting the form, I am "thrown" to the main page of the site. This piece of code looks like this:

if (!$mail->send()) {
  echo 'Error';
   echo 'Mailer Error: ' . $mail->ErrorInfo;
} else {
  header('location: ../index.html');
}

I want a popup to appear after the form is submitted, not a redirect to the main page.
Popup like this:
function myFunction()
{
alert("Спасибо! Ваше сообщение отправлено");
}

Answer the question

In order to leave comments, you need to log in

2 answer(s)
T
Talyan, 2021-06-10
@Grisha022

You are confusing sending a Header and opening a popup with JS.
If you want it faster - do it

<?php
header("Location: /index.php?formsubmit");

And in index.php add
<?php
if(isset($_GET['formsubmit'])) echo "<script>alert('Форма отправлена!');</script>";

Or in the form itself do
<input type="submit" onclick="alert('Форма отправлена');return true;" value="Отправить">

D
Denis, 2021-06-10
@aksined_by

The question, although it conveys the essence, does not speak about the conditions of your code.
By clicking on send mail, do you go to the page with the script, or call ajax, which sends sending emails without reloading the page?
If a redirect occurs by clicking on sending mail, then the task cannot be done.
If you make an ajax request, then if successful, you must return some response, which then needs to be parsed and a window with the result is already generated there, for example, using jquery

$.post(
    'someUrl',
    {
        mailAddress: 'someAddress',
        mailTitle: 'someTitle',
        mailText: 'someText'
    },
    function (response) {
        if (response === 1){
            alert("Спасибо! Ваше сообщение отправлено");
        } else {
            alert("Во время отправки произошла ошибка. Ошибка: " + response);
        }
    }
);

An example is not a solution, just an example.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question