Sebastian
Sebastian

Reputation: 3628

jQuery AJAX POST gives undefined index

My eventinfo.php is giving the following output:

<br />
<b>Notice</b>:  Undefined index:  club in <b>/homepages/19/d361310357/htdocs/guestvibe/wp-content/themes/yasmin/guestvibe/eventinfo.php</b> on line <b>11</b><br />
[]

HTML (index.php):

<select name="club" class="dropdown" id="club">
<?php getClubs(); ?>
</select>

jQuery (index.php):

<script type="text/javascript">

    $(document).ready(function() {
        $.ajax({
            type: "POST",
            url: "http://www.guestvibe.com/wp-content/themes/yasmin/guestvibe/eventinfo.php",
            data:  $('#club').serialize(),
            success: function(data) {
                $('#rightbox_inside').html('<h2>' + $('#club').val() + '<span style="font-size: 14px"> (' + data[0].day + ')</h2><hr><p><b>Entry:</b> ' + data[0].entry + '</p><p><b>Queue jump:</b> ' + data[0].queuejump + '</p><br><p><i>Guestlist closes at ' + data[0].closing + '</i></p>');
                },
            dataType: "json"
        });
    });

    $('#club').change(function(event) {
        $.ajax({
            type: "POST",
            url: "http://www.guestvibe.com/wp-content/themes/yasmin/guestvibe/eventinfo.php",
            data:  $(this).serialize(),
            success: function(data) {
                $('#rightbox_inside').hide().html('<h2>' + $('#club').val() + '<span style="font-size: 14px"> (' + data[0].day + ')</h2><hr><p><b>Entry:</b> ' + data[0].entry + '</p><p><b>Queue jump:</b> ' + data[0].queuejump + '</p><br><p><i>Guestlist closes at ' + data[0].closing + '</i></p>').fadeIn('500');
                },
            dataType: "json"
        });

    });

</script>

I can run alerts from the jQuery, so it is active.

I've copied this as is from an old version of the website, but I've changed the file structure (through to move to WordPress) so I suspect the variables might not even be reaching eventinfo.php in the first place...

index.php is in wp-content/themes/cambridge and eventinfo.php is in wp-content/themes/yasmin/guestvibe but I've tried to avoid structuring issues by referencing the URL in full.

Any ideas?

EDIT

Sorry, forgot about eventinfo.php. I suspect only really line 3 is relevant but I may be wrong.

include('functions.php');
connect();
$night = $_POST['club'];
$night = mysql_real_escape_string($night);

$query = "SELECT * FROM nights WHERE name = '" .$night. "'";

    $result = mysql_query($query);
    $items = array();

    if($result && mysql_num_rows($result) > 0) { 
        while ($row = mysql_fetch_array($result)) { 
        $items[] = array("entry"=>$row['entry'], "day"=>getLongDateString($row['day']), "queuejump"=>$row['queue jump'], "closing"=>$row['closing']);
        }
    } 

    mysql_close(); 
    // convert into JSON format and print

    echo json_encode($items);
?>

vardump[$_POST] gives:

array(0) {
}

Upvotes: 0

Views: 3640

Answers (1)

rationalboss
rationalboss

Reputation: 5389

In your index.php file, you have this:

<select name="club" class="dropdown" id="club">
<?php getClubs(); ?>
</select>

Change this to (updated):

<form name="myform" id="myform" action="submit.php" method="POST">
<select name="club" class="dropdown">
<?php getClubs(); ?>
</select>
</form>

Then change your code to $('#myform').serialize() instead of $('#club').serialize()

$_POST['club'] is null that's why you are getting the notice. It's null because it's not getting submitted properly. The form must be submitted, not the select element. The form itself must be serialize(), not the select element.

Upvotes: 2

Related Questions