Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    New Coder
    Join Date
    Jul 2012
    Posts
    18
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Question How can i add this java script code to php code??

    This is the php code-

    $blabberDisplayList .= '
    <table width="100%" align="center" cellpadding="4" bgcolor="#CCCCCC">
    <tr>
    <td width="10%" valign="top">' . $blab_pic . '</td>
    <td width="90%" valign="top" style="line-height:1.5em;"><span class="greenColor textsize10">' . $blab_date . ' <a href="profile.php?id=' . $uid . '">' . $username . '</a> said:</span><br />
    ' . $the_blab . '</td>




    This is javascript-
    <a href="javascript:;" onClick="doAction('<?php echo $postid;?>','like');">Like (<span id="<?php echo $postid;?>_likes"><?php echo $data->like;?></span>)</a>
    <a href="javascript:;" onClick="doAction('<?php echo $postid;?>','unlike');">Unlike (<span id="<?php echo $postid;?>_unlikes"><?php echo $data->unlike;?></span>)</a>

    I want to add javascript code to php cod so that i can get like and unlike option on my status.How can i do this?

  • #2
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,979
    Thanks
    4
    Thanked 2,659 Times in 2,628 Posts
    You cannot (if I understand what you are looking to do). Javascript is a client language and PHP is a server language.
    PHP is capable of generating the Javascript code, but Javascript cannot communicate with the PHP generated, nor can PHP call the JS. For dataswapping with JS, you would use AJAX to issue a background call to the server to request more information (PHP is oblivious to this).

    Now, that said with what you have here this is fine in PHP. When it generates those anchors it will replace the doAction parameters with that of what is stored in $postid, and the text with that of $data->like. What you cannot do is change the value of $data->like based on what the doAction performs, nor update that value. For that, you need to use AJAX to issue a new request to the server to tell it to update the provided postid with +1 to like or unlike.

  • #3
    New Coder
    Join Date
    Jul 2012
    Posts
    18
    Thanks
    4
    Thanked 0 Times in 0 Posts
    this is the ajax code i'm using


    <?php

    include('db.php');

    if($_POST['postid'] != '' && $_POST['type'] != ''){

    $alreadyExist = mysql_num_rows(mysql_query(' SELECT id FROM voted WHERE postid="'.(int)$_POST['postid'].'" AND ip="'.$_SERVER['REMOTE_ADDR'].'"'));

    if($alreadyExist==0){
    if($_POST['type']=='like'){
    mysql_query(' UPDATE posts SET `like`=`like`+1 WHERE id="'.(int)$_POST['postid'].'"');
    $num = mysql_fetch_row(mysql_query(' SELECT `like` FROM posts WHERE id="'.(int)$_POST['postid'].'" LIMIT 1'));
    }elseif($_POST['type']=='unlike'){
    mysql_query(' UPDATE posts SET `unlike`=`unlike`+1 WHERE id="'.(int)$_POST['postid'].'"');
    $num = mysql_fetch_row(mysql_query(' SELECT `unlike` FROM posts WHERE id="'.(int)$_POST['postid'].'" LIMIT 1'));
    }
    echo $num[0];
    mysql_query(' INSERT INTO voted (`postid`,`ip`) VALUES ("'.(int)$_POST['postid'].'","'.$_SERVER['REMOTE_ADDR'].'")');

    }else{
    echo 'You already voted this.';
    }

    }

    ?>


    i just want to display buttons below comment box so how can i merge those javascript and php codes.

  • #4
    New Coder
    Join Date
    Jul 2012
    Posts
    18
    Thanks
    4
    Thanked 0 Times in 0 Posts
    whan i put that javascript code below my php it shows error

  • #5
    Senior Coder
    Join Date
    Jun 2008
    Location
    New Jersey
    Posts
    2,530
    Thanks
    45
    Thanked 259 Times in 256 Posts
    This seems like the same issue you're asking in the other thread, is it?

    Also, please please PLEASE start wrapping your code with the correct tags ([code] and [php])


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •