• 0

Javascript Alert


Question

I have this code

<?php

$to = "admin@domain.co.uk";

$subject = "Submit Link";

$email = $_REQUEST['Title'] ;

$message = $_REQUEST['URL'] ;

$headers = "From: $email";

$sent = mail($to, $subject, $message, $headers) ;

if($sent)

{print " Message sent successfuly"; }

else

{print "We encountered an error sending your mail"; }

?>

now when i submit the form the message sent successfuly would be put onto a new page, how can i make it so that it comes up as an alert box?

Link to comment
Share on other sites

3 answers to this question

Recommended Posts

  • 0

Assuming you want the alert of that same page you have now, just do this...

&lt;?php 
$to = "admin@domain.co.uk"; 
$subject = "Submit Link"; 
$email = $_REQUEST['Title']; 
$message = $_REQUEST['URL']; 
$headers = "From: $email"; 
$sent = mail($to, $subject, $message, $headers); 
if($sent) 
{print "&lt;script&gt;alert('Message sent successfully!');&lt;/script&gt;"; }
else 
{print "&lt;script&gt;alert('We encountered an error sending your mail');&lt;/script&gt;"; }
?&gt;

Link to comment
Share on other sites

  • 0

well the alert comes up now, but for some reason it still goes to a blank page. I need i to stay on the current page.

Link to comment
Share on other sites

  • 0

Well when you submit a form, you are redirecting the user to that script. There is no way around that. If that new script outputs nothing to the page then yes, the user will see a blank page.

If you want them to never actually leave the form page and only see a Javascript alert when they submit your form, you have to use what is called an AJAX call instead of an actual form submission. This would mean Javascript reads your form fields, sends them to a different script in the background, and then outputs an alert to the page upon receiving a positive response from that other script.

Have a look online at some AJAX tutorials online. You should be able to find some AJAX function pretty much ready-made.

Link to comment
Share on other sites

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.