0

I have an array named "seat" in my javascript file.It is used to store the seat numbers when a user clicks on a seat in a theater layout.In my function,I've used a window alert to show the user his selected seats,and when he clicks OK button,I want to send these booked seats(values in my array) to a php file named "confirm".

This is the window alert

Here is the javascript function.

var init = function (reservedSeat) {
    var seat = [], seatNo, className;
    for (i = 0; i < settings.rows; i++) {
        for (j = 0; j < settings.cols; j++) {
            seatNo = (i + j * settings.rows + 1);
            className = settings.seatCss + ' ' + settings.rowCssPrefix + i.toString() + ' ' + settings.colCssPrefix + j.toString();
            if ($.isArray(reservedSeat) && $.inArray(seatNo, reservedSeat) != -1) {
                className += ' ' + settings.selectedSeatCss;
            }
            seat.push('<li class="' + className + '"' +
                      'style="top:' + (i * settings.seatHeight).toString() + 'px;left:' + (j * settings.seatWidth).toString() + 'px">' +
                      '<a title="' + seatNo + '">' + seatNo + '</a>' +
                      '</li>');
        }
    }
    $('#place').html(seat.join(''));
};

$('.' + settings.seatCss).click(function () {
if ($(this).hasClass(settings.selectedSeatCss)){
 alert('This seat is already reserved!');
}
else{
    $(this).toggleClass(settings.selectingSeatCss);
 }
});

$('#btnsubmit').click(function() {
    var seat = [], item;
    $.each($('#place li.' + settings.selectingSeatCss + ' a'), function (index, value) {
        item = $(this).attr('title');                   
        seat.push(item);                   
    });
    window.alert(seat);
 $_POST('confirm.php', {seat: seat})
})
<form method="POST" action="confirm.php"> 
<div align="center"><input type="Submit" id="btnsubmit" value="Submit" /></div>
</form>

And this is my php code.

$seat = "";

if(isset($_POST['seat']))
{
    $seat = $_POST["seat"];
    print_r($seat);
}

When this is executed I get the window alert,but the values stored in the array does not pass to the php file.Is there something wrong with this code?Please help!I'm stuck here!!!

2 Answers2

2

$_POST isn't a built-in method, and jQuery doesn't provide a method like that either-- you can't just "set" the values into the $_POST array like this.

To post using jQuery, you would do something like the following, including a handler for data returning from the server (if you have any):

$.post("confirm.php", { seat: seat})
 .done(function(data){
          alert('Received data from server: ' + data);
       });
Miles Grimes
  • 432
  • 2
  • 16
0

You need to send the data to your PHP script, this does nothing in your JS code:

$_POST('confirm.php', {seat: seat})

use something like jQuery post method or vanilla JS XMLHttpRequest

Karman Kertesz
  • 334
  • 3
  • 8