0

I have an html form like this:

<form method="get" action="save.php">
<input type="email" id="email" name="email"/>
<input type="submit" name="submit" value="Submit" />
</form>

and in save.php i have something like this:

<?php
session_start();
$email = $_REQUEST['email'];
$email_content = "Thank you for your subscription";
mail($email,"Thank you",$email_content);
header("Location:thankyou.php");
?>

Now in save.php file i need to send this e-mail but also to echo a script that runs a js function. For example

<?php
 echo "<script>";
 echo "<script src='my_path_to_file/file.js'></script>";
 echo "var subscriberEmail = '" . $email . "';";
 echo "mySubscribe(subscriberEmail);";
 echo "</script>";
 ?>

Now, if i place the echoing of the script before mail(), then i don't go to thankyou.php, mail() is not executed, i don't go to thankyou.php but script function works. If i place echoing of script after mail, then mail is sent, i go to thankyou.php but script function is not executed at all.

Any ideas to make both happen?

Thank you in advance

asked Mar 29, 2017 at 8:53
2
  • (1) The HTML <script> elements you're trying to echo are invalid. (2) You either echo output to the page or you redirect to a different page. Not both. You have to choose which one you want to do. Do you want to echo this script or do you want to redirect to the "thank you" page? (Maybe just put this script on the "thank you" page?) Commented Mar 29, 2017 at 8:57
  • why don't you use ajax to send the form content (as JSON) to the php file, and then do whatever script you want on the done function? Commented Mar 29, 2017 at 9:04

2 Answers 2

2

It's becouse echo command send content to browser, and header redirect will never works.

You could try to use comething like that:

<?php
echo "<script>";
echo "<script src='my_path_to_file/file.js'></script>";
echo "var subscriberEmail = '" . $email . "';";
echo "mySubscribe(subscriberEmail);";
echo "document.location.href='thankyou.php';";
echo "</script>";
?>

It means, move redirect command from php code to javascript.

answered Mar 29, 2017 at 8:56
Sign up to request clarification or add additional context in comments.

Comments

0

Possibly some error in mySubscribe(subscriberEmail); function. Thats is why if you put script before email and header("Location:thankyou.php"); it not sending mail and redirects you. And also if you put script before, $email variable is not set yet

answered Mar 29, 2017 at 9:04

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.