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>&nbsp</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>&nbsp</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.

Share Improve this question edited Aug 9, 2013 at 6:50 Mylon James Marcell asked Aug 9, 2013 at 5:53 Mylon James MarcellMylon James Marcell 232 silver badges7 bronze badges 1
  • # missing in selector of date_time – Susheel Commented Aug 9, 2013 at 6:23
Add a ment  | 

4 Answers 4

Reset to default 3

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');
        }
    });
});

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