david Morigeau
david Morigeau

Reputation: 15

Call Javascript from a PHP variable

I have a form that calls a success message with this code:

// Form processed successfully, return the success message
$result = array(
    'type' => 'success',
    'data' =>      
    $form->replacePlaceholderValues($successMessage)
);

the variable $successMessage is called if the form is successfully sent.

$successMessage = '<div class="success-message">Your message has been sent, thank you.</div>';

I want to process this Javascript popup rather than the success message using this code

<script>
$(document).ready(function(){
    $().socialTrafficPop({
        timeout: 999,
        title: "One Great Site!",
        message: '<div class="success-message">Your message has been sent, thank you.</div><em>Share Send Email Free</em>!',
        google_url: "http://tyler.tc/",
        fb_url: "somesite.com",
        closeable: true,
        advancedClose: false,
        opacity: '0.45',
        twitter_method: "tweet",
        tweet_url: 'somesite.com',
        tweet_text: 'Just tried out this awesome plugin Social Traffic Pop - Its Amazing!'
    });
});
</script>

Can I call it from the variable $successMessage? Or is there a better and more appropriate way to call this script? How would I do either?

Also, I put the necessary scripts inside of the header.php file which gets called by the index.php file which projects the homepage. Is there somewhere else I should be putting the necessary scripts that the popup code needs to function rather than header.php?

I have tried endlessly around the code below and it doesn't seem to work. The $successMessage works without the JavaScript fine, but when I try to add the JavaScript the form will not process anymore. Here is one of the many things I have tried. Thank you for any help.

<?php
$successMessage = echo "
    <script>
    $(document).ready(function(){          
        $().socialTrafficPop({
            timeout: 999,
            title: "One Great Site",
            message: '<div class="success-message">Your message has been sent, thank you.</div><em>Share Send Email Free</em>!',
            google_url: "http://tyler.tc/",
            fb_url: "someurl.com",
            closeable: true,
            advancedClose: false,
            opacity: '0.45',
            twitter_method: "tweet",
            tweet_url: 'someurl.com',
            tweet_text: 'Just tried out this awesome!'
        });
    });
    </script>";

Upvotes: 1

Views: 764

Answers (1)

Jared Farrish
Jared Farrish

Reputation: 49188

Your quote escaping is not right; you either need to replace the " within the Javascript with ', escape the \" if you have to, or as Felix Kling notes, use a heredoc.

<?php

echo "
    <script>
    $(document).ready(function(){          
        $().socialTrafficPop({
            timeout: 999,
            title: 'One Great Site',
            message: '<div class=\"success-message\">Your message has been sent, thank you.</div><em>Share Send Email Free</em>!',
            google_url: 'http://tyler.tc/',
            fb_url: 'someurl.com',
            closeable: true,
            advancedClose: false,
            opacity: '0.45',
            twitter_method: 'tweet',
            tweet_url: 'someurl.com',
            tweet_text: 'Just tried out this awesome!'
        });
    });
    </script>
";

?>

http://codepad.org/lsqLVXs4

Upvotes: 1

Related Questions