0

I am working on an html page having a subscription section. The users puts his/her email in an input field and submit it. The control is then transferred to a php page which emails the subscription message to the website admin if the message is sent successfully it navigates back to index.html. Now at this point i want to show an alert message that subscription was successful or unsuccessful. How can i do that without making my index.html index.php.

<?php

$to = "@gmail.com";
$from = "edmin.media@gmail.com";
$headers = "From: " . $from . "\r\n";
$subject = "New subscription";
$body = "Dear Admin!\nNew user subscription: " . $_POST['email'];

if( filter_var($_POST['email'], FILTER_VALIDATE_EMAIL) ) {
    if (mail($to, $subject, $body, $headers, "-f " . $from)) {
        @header('location:index.html');
    } else {
        @header('location:index.html');
    }
} else {
    @header('location:index.html');
}

?>
icecub
  • 8,615
  • 6
  • 41
  • 70
  • Use Ajax. This way you can send the form data to your php file and show an alert depending on the response. – icecub Jul 02 '15 at 22:55
  • *it navigates back to index.html any way whether successful or not successful* – Sheikh Awais Jul 02 '15 at 22:57
  • can you give me a small piece of code ? because i don't know anything about ajax. and can i send response to an html page through ajax ? – Sheikh Awais Jul 02 '15 at 22:58
  • There are examples here, https://learn.jquery.com/ajax/, and on many other websites. – chris85 Jul 02 '15 at 22:58
  • That doesn't matter. Ajax will prevent the script from navigating to your php file in the first place. Instead, it'll only send the form data towards it. Depending on the response from your php file, you can display any alert. Like succes or failed. If you could give us your code, I'm sure we can give you an example. – icecub Jul 02 '15 at 22:58
  • I cannot give you an example as this wouldn't fit as an answer to your question. Edit your question with your current code. I'll be able to edit it so it'll use Ajax. – icecub Jul 02 '15 at 23:01
  • Haha I didn't mean to give your code in a comment. Click on edit underneath your question. This allows you to put the code inside it. Please make sure you add the code of your HTML file as well. – icecub Jul 02 '15 at 23:10
  • I've edited your question with that code. But I need your HTML code as well. So please make sure you add it. – icecub Jul 02 '15 at 23:15

1 Answers1

0

You could pass a GET variable in the url e.g. redirect to index.html?subscribed=yes and check for it with JavaScript.

See https://stackoverflow.com/a/901144/5063532

You can also redirect index.html to a php using .htaccess file so that for the user they appear to still be on index.html.

See https://www.addedbytes.com/articles/for-beginners/url-rewriting-for-beginners/

However, I cannot recommend this as server-side processing would be preferred and AJAX probably ideal.

Community
  • 1
  • 1
lolnon
  • 11
  • 2