1

In my index.html I have:

<head>
  <link rel="stylesheet" href="css/messi.min.css" />
  <script type="text/javascript" src="http://code.jquery.com/jquery-latest.min.js"></script>
  <script src="js/messi.min.js"></script>
  <script type="text/javascript">
    function failureFunction(){
      new Messi('There was an error sending your message.', {title: 'Failure'});
    };      
    function successFunction(){
      new Messi('Success sending email', {title: 'Success'});
    };
  </script>
</head>
<body>
  <form class="form" id="form1" method="post" action="contactengine.php">
  ...
  </form>
</body>

And then in my contactengine.php I have:

$success = mail($EmailTo, $Subject, $Body, $Headers);

// redirect to success page 
if ($success){
  echo "<script type='text/javascript'>successFunction();</script>"; 
  print "<meta http-equiv=\"refresh\" content=\"0;URL=index.html\">";
}
else{
 echo "<script type='text/javascript'>failureFunction();</script>";
 print "<meta http-equiv=\"refresh\" content=\"0;URL=index.html\">";
}

My echo ... won't call the successFunction() though. I know the $success var is true because if I put an alert there it calls the alert and I know that my successFunction() works because I can call it from my index.html file with no problems. The messi library is located here for reference.

How can I correctly call the successFuntion() from my php file?

2
  • 3
    You can't. To make this work, you can submit your form using ajax and then call the successFunction() inside the success handler of the ajax function Commented May 28, 2014 at 4:50
  • You've defined your functions in index.html, and you try to call them from another page. It doesn't work that way. Commented May 28, 2014 at 4:59

1 Answer 1

3

May be you can move your JS functions inside a javascript file and include that file in the PHP or HTML pages where you need to call those functions.

js/customMessi.js

function failureFunction(){
  new Messi('There was an error sending your message.', {title: 'Failure'});
};      
function successFunction(){
  new Messi('Success sending email', {title: 'Success'});
};

and in your HTML file :

<head>
 <link rel="stylesheet" href="css/messi.min.css" />
<script type="text/javascript" src="http://code.jquery.com/jquery-latest.min.js">   </script>
 <script src="js/messi.min.js"></script>
 <script src="js/customMessi.js"></script>
 </head>
<body>
 <form class="form" id="form1" method="post" action="contactengine.php">
   ...
  </form>
</body>

and in your contactengine.php file as well:

echo "<script type='text/javascript' src='js/customMessi.js'></script>";
...
$success = mail($EmailTo, $Subject, $Body, $Headers);

// redirect to success page 
if ($success){
  echo "<script type='text/javascript'>successFunction();</script>"; 
  print "<meta http-equiv=\"refresh\" content=\"0;URL=index.html\">";
}
else{
 echo "<script type='text/javascript'>failureFunction();</script>";
 print "<meta http-equiv=\"refresh\" content=\"0;URL=index.html\">";
}
Sign up to request clarification or add additional context in comments.

7 Comments

I tried putting the <head>... both inside the <?php ?> and outside and neither worked. I also put my customMessi.js script inside the index.html file and was able to successfully call the successFunction()... So I know customMessi.js works.
Any thoughts? Thanks for your help so far.
I don't work in PHP, just answered according to my thiinking. But i think something like ths should work in PHP. ?> <script type="text/javascript" src="js/customMessi.js"></script> <?php
echo "<script type='text/javascript' src='js/customMessi.js'></script>";
echo "<script type='text/javascript' src='js/customMessi.js'></script>"; echo "<script type='text/javascript'>successFunction();</script>";
|

Your Answer

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.