0

I have a form that using ajax to post the comment to video_comment.php then show up the comment live on video page.

This is my jquery code:

$("input[id*='post_video_playallcomment_']").livequery('click', function(event) {
    event.preventDefault();    
    var video_msg   = $("#post_message");
    var input_id    = $(this).attr('id');
    var id_split    = input_id.split('_');
    var video_id    = id_split[3];                    
    var comment     = $("textarea[id='video_comment']").val();
    var response_messages = '#video_response';        

    if ( comment == '' ) {
        video_msg.fadeIn();
        return false;
    }

    video_msg.hide();
    user_posting(response_messages, '<?php echo $lang['600']; ?>', 1);
    reset_chars_counter();
    $.post(base_url + '/video_comment.php', { video_id: video_id, comment: comment },
    function(response) {
        if ( response.msg != '' ) {
            $(response_messages).hide();
            user_posting(response_messages, response.msg);                    
            $("textarea[id='video_comment']").val('');
        } else {
            $(response_messages).hide();
            user_response(response_messages, '<?php echo $lang['587']; ?>');             
            $(".no_comments").hide();
            $("textarea[id='video_comment']").val('');
            var bDIV = $("#comments_delimiter");
            var cDIV = document.createElement("div");
            $(cDIV).html(response.code);
            $(bDIV).after(cDIV);
        }
    }, "json");
});                         

On video_comment.php, this is the response.code to show up the comment live on video page.

echo 'text...

echo '<div id="commentplayer"></div>';
echo '<script type="text/javascript">';
echo 'jwplayer("commentplayer").setup({';
echo 'file: "...url",';  
echo 'width: "300",';
echo 'height: "225",';
echo 'provider: "http",';
echo 'startparam: "start",';
echo 'wmode: "transparent",';        
echo 'controls: "true",'; 
echo 'stretching: "uniform",';    
echo 'primary: "flash",';       
echo 'autostart: "false",';
echo 'ga: {}';
echo '});';
echo '</script>';

echo 'text...

I have no problem to get 'text...', but i can't get '<script type="text/javascript">function...</script>' to show up live on video page.

Help please...

27
  • 4
    Shouldn't $(#comment) be $('#comment')? Commented Jan 22, 2013 at 14:29
  • 1
    @Jay Blanchard The link is broken. :( Commented Jan 22, 2013 at 14:33
  • 1
    How do you know the script doesn't run? What exactly is in that <script> block? Commented Jan 22, 2013 at 14:33
  • 2
    You say the response from the server is JSON, but the echo part in your PHP script does not look like to be JSON. The code you posted is very confusing. Commented Jan 22, 2013 at 14:37
  • 1
    This has nothing to do with jQuery, you have to create JSON on the server. echo "<script>...</script>"; certainly does not output JSON, but partial HTML. Which is fine as well, but then don't tell jQuery to expect JSON, and response would be a string containing HTML. Strings don't have a code property (I wonder why you think it should exist). Commented Jan 22, 2013 at 14:44

1 Answer 1

1

instead of placing javascript code in the php return, why dont you place it in the .post response?

$("input[id*='post_video_comment']").click(function() {           
$.post('video_comment.php', function(response) {
  $('#comment').html(response.code);  
    //place javascript code here
}, "json");

});

or placing a onload before your js code

$str = '<div id="commentplayer"></div>
<script type="text/javascript">
window.onload=(function() {
    jwplayer("commentplayer").setup({
    file: "...url",
    width: "300",
    height: "225",
    provider: "http",
    startparam: "start",
    wmode: "transparent",      
    controls: "true", 
    stretching: "uniform",  
    primary: "flash",     
    autostart: "false",
    ga: {}
    });
});
</script>';

echo $str;
Sign up to request clarification or add additional context in comments.

11 Comments

I can't place the javascript code in post response, because the javascript function is display within a text. e.g: text... javascript ...text...
@richard i see, can you share the js inside the php then? do you have a onLoad call?
@ tq, i've updated the javascript in my question. It's a jw player.
I just tried your onload solution, but the problem is, it's just show up "text...<div id="commentplayer"></div> text...", everything inside <script type="text/javascript"></script> missing.
right now it looks like you have response.msg which means that the response is looking for msg, can you use just response?
|

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.