通过Javascript的变量通过AJAX到PHP [英] Pass Javascript variable to PHP via ajax
问题描述
我试图通过从我的javascript code变量转移到服务器端的PHP code。我知道这必须通过Ajax调用它,我相信我已经正确完成,但是访问过我从我的阿贾克斯到我的PHP变量是当我遇到麻烦,因为我是新来的PHP。这是我的code我迄今:
I am trying to pass a variable from my javascript code over to the server side PHP code. I know this must be done via an ajax call which i believe i have done correctly, however accessing the variable i pass from my ajax into my php is when i run into trouble as i am new to php. Here is my code i have thus far:
$(document).ready(function() {
$(".clickable").click(function() {
var userID = $(this).attr('id');
//alert($(this).attr('id'));
$.ajax({
type: "POST",
url: 'logtime.php',
data: "userID=" + userID,
success: function(data)
{
alert("success!");
}
});
});
});
<?php //logtime.php
$uid = isset($_POST['userID']);
//rest of code that uses $uid
?>
我想通过我的JavaScript变量用户ID到PHP($用户ID),但我已经错了地方公路沿线。感谢您的帮助!
I'm trying to pass my javascript variable "userID" to php ($userID), however i've gone wrong somewhere along the road. Thanks for the help!
推荐答案
传递这样的数据,以Ajax调用( HTTP: //api.jquery.com/jQuery.ajax/ ):
Pass the data like this to the ajax call (http://api.jquery.com/jQuery.ajax/):
data: { userID : userID }
和你的PHP做到这一点:
And in your PHP do this:
if(isset($_POST['userID']))
{
$uid = $_POST['userID'];
// Do whatever you want with the $uid
}
isset()函数
函数的目的是检查wheter给定的变量存在,不是为了它的价值。
isset()
function's purpose is to check wheter the given variable exists, not to get its value.
这篇关于通过Javascript的变量通过AJAX到PHP的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!