I’m kinda newbie on mixing jquery with php and php with jquery.
I have to file script.php and test.php
I manage to post variables from test.php to script.php using jquery, but now I want to read variables from script.php into test.php
my test.php is like this:
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/
libs/jquery/1.3.0/jquery.min.js">
</script>
<script type="text/javascript" >
$(function() {
$(".submit").click(function() {
var like = $("#like").val();
var rid = $("#rid").val();
var dataString = 'like='+ like + '&rid='+ rid;
//alert('like='+ like + 'rid='+ rid);
$.ajax({
type: "POST",
url: "script.php",
data: dataString,
success: function(){
$('.success').fadeIn(200).show();
}
});
return false;
});
});
</script>
<form method="post" name="form">
<input type="hidden" value="3" id="rid" name="rid">
<button type="submit" value="like" class="submit" id="like" name="like">test</button>
<span class="default"> <?=$value?></span>
<span class="success" style="display:none"> You like this</span>
</div></form>
my script.php is:
<?php
if($_POST['like']=="like")
{
require_once"dbconfig.php";
$rid=$_POST['rid'];
mysql_query("INSERT INTO likes set like_value='1', video_id='".$rid."'");
$sql=mysql_query("SELECT * FROM likes where like_value='1' and video_id='".$rid."'")or die(mysql_error());
$result=mysql_num_rows($sql);
}
?>
I want to display the $result instead of ” You like this”. I’m trying to do this way because I don’t want to refresh the whole page (test.php).
Thank you!
In your
script.php, add in the last lineecho $result;, like this:and in your text.php script:
Why add
resultparameter in the success function? This is because result will be the response of yourscript.phpwhich in your case is the$result.