[英]Grabbing correct variables for AJAX Post - from HTML/PHP page
In my HTML
, I loop thru a PHP
/ MySQL
query result and do something that makes HTML
like this as the end result: 在我的HTML
,我通过PHP
/ MySQL
查询结果循环,并做一些使这样的HTML
作为最终结果:
loop 1
<p>- user review-</p>
<a href=#" id="upvote">Up Vote</a>
loop 2
<p>- user review-</p>
<a href=#" id="upvote">Up Vote</a>
etc.
Now, when the user clicks "up vote", I simply want an AJAX POST
to send to a PHP
script that looks like this: 现在,当用户点击“up vote”时,我只想要一个AJAX POST
发送到一个如下所示的PHP
脚本:
// jQuery / AJAX - separate file linked to `HTML`
$('#upvote').onclick(function() {
var user_id = ??????;
var review_id = ?????;
$.ajax({
url : "search_query.php",
type : "POST",
dataType: "json",
data : {
userId : user_id,
reviewId : review_id
},
success : function(data) {
// do stuff
}
});
// php
<?php
$review_id = $database->escape_value(trim($_POST['reviewId']));
$user_id= $database->escape_value(trim($_POST['userId']));
// Upvote Method
$result = Data::upVoteReview($review_id, $user_id);
// Not discussed in question
$output["result"] = $result;
print(json_encode($output));
?>
My question is: I need to grab two variables from HTML
and put in the AJAX POST
; 我的问题是:我需要从HTML
获取两个变量并放入AJAX POST
; as you see above, they are reviewId
and userId
. 如上所示,它们是reviewId
和userId
。 (Note the user_id
is the logged in user and is actually stored as a global PHP
SESSION
variable.) (注意user_id
是登录用户,实际上存储为全局PHP
SESSION
变量。)
The difficult part is the HTML is dynamic, and each loop will have a different review_id. 困难的部分是HTML是动态的,每个循环都有不同的review_id。 The user_id will be the same but I am not sure how to get that from a SESSION
var to the JavaScript. user_id将是相同的,但我不知道如何从SESSION
var到JavaScript。 Note: In each "loop" on the HTML
page I do have access to the review_id
php variable that I need - just not sure where to place it so I can pick it up in jQuery
/ AJAX
. 注意:在HTML
页面的每个“循环”中,我都可以访问我需要的review_id
php变量 - 只是不确定将它放在哪里,所以我可以在jQuery
/ AJAX
。
Use classes instead of IDs as propper markup there should be only 1 ID of a given name per page. 使用类而不是ID作为propper标记,每页应该只有一个给定名称的ID。
HTML HTML
loop 1
<p>- user review-</p>
<a href="#" class="upvote" data-review="1">Up Vote</a>
loop 2
<p>- user review-</p>
<a href="#" class="upvote" data-review="2">Up Vote</a>
etc.
JS JS
// jQuery / AJAX - separate file linked to `HTML`
$('.upvote').on('click', function(e) {
e.preventDefault();
var review_id = this.data('review');
$.ajax({
url : "search_query.php",
type : "POST",
dataType: "json",
data : {
reviewId : review_id
},
success : function(data) {
// do stuff
}
});
PHP PHP
<?php
$review_id = $database->escape_value(trim($_POST['reviewId']));
// Upvote Method
$result = Data::upVoteReview($review_id, $_SESSION['user_id']);
$output["result"] = $result;
print(json_encode($output));
?>
There seem to be quite a few problems in your code, the first couple of ones I've noticed are the following: 您的代码中似乎存在相当多的问题,我注意到的前几个问题如下:
The first one: 第一个:
loop 1
<p>- user review-</p>
<a href=#" id="upvote">Up Vote</a> <------------------------------Here
loop 2
<p>- user review-</p>
<a href=#" id="upvote">Up Vote</a> <-------------------------------Here
etc.
You are missing the opening quotes, ie it should be href="#"
not href=#"
你错过了开头的引号,即它应该是href="#"
而不是href=#"
Second is: 第二是:
$('#upvote').onclick(function() {
should probably be as jQuery does not have onclick
function, it definitely has on
function. 也许应该是在jQuery没有onclick
功能,它肯定有on
功能。
$('#upvote').on('click', function() {
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.