admin管理员组文章数量:1312662
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>";
?>
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');
}
});
});
});
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.
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>";
?>
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');
}
});
});
});
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.
- # missing in selector of date_time – Susheel Commented Aug 9, 2013 at 6:23
4 Answers
Reset to default 3If 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');
}
});
});
You can reference the clicked .box
by using $(this)
:
$(".box").click(function() {
$.ajax({
type: 'GET',
url: '/book.me/loop.php',
data: {
id: $(this).val()
},
success: function(data){
alert('successful');
}
});
});
you should set the data like this:
$(".box").click(function() {
$.ajax({
type: 'GET',
url: '/book.me/loop.php',
data: "id=" + this.id,
dataType: 'html',
data: { id: $(this).val() },
success: function(data){
alert('successful');
}
});
});
Try this:
$('#' + date_time).val()
本文标签: javascriptPassing a value to PHP from jQuery AjaxStack Overflow
版权声明:本文标题:javascript - Passing a value to PHP from jQuery Ajax - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741913733a2404597.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论