0

I am using the mail() in php and wondered if it was possible to have event listeners included. Below includes code with a button that is to have the paragraph slideup when clicked. Yet, nothing happens.

<html>
    <head>
        <script type="text/javascript"src="js/jquery.js"></script>
        <script type="text/javascript" src="js/jquery-ui.js"></script>
        <script type='text/javascript'>
            $(function() {
                $(':button').click(function(){
                    $("p").slideToggle();   
                });
            });
        </script>
    </head>
    <body>
        <?php
            $to = "Example@yahoo.com"; 
            $subject = "Maybe so??"; 
            $body = "<p>Yeppir </p>
            <button>Click Me </button>"; 
            $headers  = 'MIME-Version: 1.0' . "\r\n";
            $headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n"; 

            $headers .= 'From: skywalker@msn.com';
            if (mail($to, $subject, $body, $headers)) { 
                echo("<p>Message sent!</p>"); 
            } else { 
                echo("<p>Message delivery failed...</p>"); 
            } 
        ?> 
    </body>
</html>
arulmr
  • 8,620
  • 9
  • 54
  • 69
Octavius
  • 583
  • 5
  • 19
  • You do realise that PHP runs server side while JQuery runs client side, so on totally different machines at different locations? – jbx Nov 11 '12 at 04:52
  • if a user can run the javascript at other computer via website ...supp you send mail with JS and it run on other computer that is called **XSS** – NullPoiиteя Nov 11 '12 at 04:55

3 Answers3

1

The short answer is you can't. Mail clients strip out JS

Just stick with plain HTML and CSS that works over most of mail clients.

NullPoiиteя
  • 56,591
  • 22
  • 125
  • 143
1

You are trying to send an email message and your javascript is on your website. When they open the message, they will see the message, but don't get any of that javascript because you didn't include it in the message in the first place.

What you can do is fix your code to send jQuery in the message along with the body. In theory that would work. In actuality this wouldn't work either as about 99% of email services (100% of all the most commonly used ones) strip javascript tags as they can be malicious. So, rather than spend time on getting toggles in emails, spend time trying to get the user to your site.

Tim Withers
  • 12,072
  • 5
  • 43
  • 67
  • **rather than spend time on getting toggles in emails, spend time trying to get the user to your site.** hit the nail. – itachi Nov 11 '12 at 05:05
0

jQuery is client-side language and PHP is a server side, the possible way to achieve this is by requesting the PHP page which has the mail function with jQuery (using Ajax) and then change the html to show/hide the results (error or success) upon the PHP response.

read more about this topic in this post jQuery/PHP mail send the easy way?

Community
  • 1
  • 1
Mohd Moe
  • 577
  • 3
  • 9