I have a rate.php which is for people to rate something, when they click the star, I should be able to get the score, and in index.php, I want to get the $_POST[‘result’],$_POST[‘id’],$_POST[‘ip’], and store them in my database. However, I don’t know how to get it done correctly.
If I click the rate in rate.php, then it successfully posted. Does that mean, the index.php will be executed automatically because the “url” in ajax is “index.php”? If it doesn’t. What’s the right way to do it?
Below is my code for both pages:
1.<rate.php>
:
<script type="text/javascript" src="./scripts/jquery.js"></script>
<script type="text/javascript" src="./scripts/jquery.raty.js"></script>
<script type="text/javascript">
$.fn.raty.defaults.path = './images';
$(document).ready(function() {
$('#star').raty({
click: function(score) {
var result = score;
var id = $("#getid").val();
var ip = $("#ip").val();
$.ajax({
type: "post",
url: "index.php",
data: "result="+result+"&id="+id+"&ip="+ip,
datatype: "html",
beforeSend: function() {
$("#msg").html("<font color='red'>ajax loading,please wait...</font>");
},
success: function(msg) {
$("#msg").html("your rate:"+score+"star");
},
error: function(XMLHttpRequest, textStatus, errorThrown) {
alert(XMLHttpRequest.status);
alert(XMLHttpRequest.readyState);
alert(textStatus);
}
});
}
});
});
//score: function() {
// return $(this).attr('data-score');
</script>
</head>
<body>
<div id="star" data-score="0"></div>
<p><a href = "<?php echo $browse; ?>">go back</a></p>
<p><a href = "controller.php">mainpage</a></p>
<input type="hidden" id="check" name="check" value="">
<input type="hidden" id="getid" name="getid" value="<?php echo $address?>">
<?php $ip = $_SERVER['REMOTE_ADDR']; ?>
<input type="hidden" id="ip" name="ip" value="<?php echo $ip ?>">
<span id="msg"></span>
</body>
2.<index.php>
:
include $_SERVER['DOCUMENT_ROOT'] . '/includes/db2.inc.php';
if ($_POST['result'] !='' and $_POST['id'] !='' and $_POST['ip'] !='' )
{
try
{
$sql = 'INSERT INTO ratetest SET ip = :ip,
id = :id';
$s = $pdo->prepare($sql);
$s->bindValue(':ip', $_POST['ip']);
$s->bindValue(':id', $_POST['id']);
$s->execute();
}
catch (PDOException $e)
{
$error = 'Error inserting rate info';
include '../error.html.php';
exit();
}
}