dongzouhe9734 2011-12-13 00:09
浏览 31
已采纳

从PHP变量调用Javascript

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>";
  • 写回答

1条回答 默认 最新

  • doudao0660 2011-12-13 00:18
    关注

    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

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 java业务性能问题求解(sql,业务设计相关)
  • ¥15 52810 尾椎c三个a 写蓝牙地址
  • ¥15 elmos524.33 eeprom的读写问题
  • ¥15 使用Java milo连接Kepserver服务端报错?
  • ¥15 用ADS设计一款的射频功率放大器
  • ¥15 怎么求交点连线的理论解?
  • ¥20 软件开发方法学习来了
  • ¥15 微信小程序商城如何实现多商户收款 平台分润抽成
  • ¥15 HC32L176调试了一个通过TIMER5+DMA驱动WS2812B
  • ¥15 cocos的js代码调用wx.createUseInfoButton问题!