1

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>";
3
  • You're mixing " in PHP and Javascript in the script block you're trying to echo (and I doubt you need the $successMessage variable). You need to either escape the " (like so: \") within the Javascript, or use ' instead. Note, if you look at the code highlighting above, you'll notice that the script tag text is alternately red and highlighted in different colors. This is an indicator of a problem. Commented Dec 13, 2011 at 0:12
  • 2
    Have a look at heredoc. Commented Dec 13, 2011 at 0:14
  • output buffering via ob_start() may also be a solution Commented Dec 13, 2011 at 0:22

1 Answer 1

1

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

Sign up to request clarification or add additional context in comments.

1 Comment

Your answers led me to use a new function which processed the code after the success message. successEnd: function () { since I couldn't figure out how to move forward inside of the PHP code. Rather than replace the success message with new code I enabled the code to process after the success message. The $successMessage variable was locked in a php file however, the actual message was initiated by .js file which made it much easier to integrate the code.

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.