...

View Full Version : How can i add this java script code to php code??



sssunny
07-20-2012, 10:35 PM
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?

Fou-Lu
07-20-2012, 10:47 PM
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.

sssunny
07-21-2012, 07:56 AM
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.

sssunny
07-21-2012, 07:57 AM
whan i put that javascript code below my php it shows error

Keleth
07-21-2012, 02:36 PM
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])



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum