传递一个值从jQuery的阿贾克斯到PHP
问题描述:
我想获得一个值返回到PHP,我可以工作,并发送到数据库。我似乎不能够是使用jQuery AJAX的GET类型的事情。下面是我使用捕捉值PHP:
I'm trying to get a value back to PHP which I can work with and send to the database. I don't seem to be able to do that using the GET type of jQuery AJAX. Here's the PHP I'm using to catch the value:
<?php require_once("header.php"); ?>
<?php require_once("class.php"); ?>
<?php
//$month = $_POST['month'];
//$year = $_POST['year'];
$month = 8;
$year = 2013;
$numDays = cal_days_in_month(CAL_GREGORIAN, $month, $year);
$empty = "<table class='box'><td> </td></table>";
$cal = new calendar();
$cal->mkMonth($month);
$cal->emptyDays($empty, $numDays, $month, $year);
$cal->mkdays($numDays, $month, $year);
echo "</div>";
echo "<div id='jresult'>";
if (isset($_GET['id'])) {
echo ($_GET['id']);
}
echo "</div>";
?>
下面是jQuery的...
Here's the jQuery...
$(document).ready(function(){
$(".box").mouseenter(function(){$(this).css("background-color", "red");});
$(".box").mouseout(function(){$(this).css("background-color", "transparent");});
$(".box").click(function() {
date_time = $(this).attr('id');
console.log(date_time);
$.ajax({
type: 'GET',
url: '/book.me/loop.php',
data: "id=" + $(date_time).val(),
success: function(data){
alert('successful');
}
});
});
});
我想传递的的ID .box的
类元素的 DATE_TIME
变量,它的工作原理,但它似乎并不当我打电话 $ _ GET ['身份证']
在PHP是工作。我刚开始处于不确定的状态回来了。
I'm trying to pass in the id of the .box
class element to the date_time
variable which works, but it doesn't seem to be working when I call $_GET['id']
in PHP. I'm just getting an undefined value back.
答
如果您要发送的id属性,而不是价值,你可以用
If you want to send the id attribute, not the value, you can use
$(".box").click(function() {
date_time = $(this).attr('id');
$.ajax({
type: 'GET',
url: '/book.me/loop.php',
data: "id=" + date_time,
success: function(data){
alert('successful');
}
});
});