1

I have an array in my js file.And after the user selects his prefered seats,the data creation in the array is done.

selection of seats,and data creation in the array

Then I send it to a php file for displaying.The javascript code is shown below.

$('#btnsubmit').click(function(ev) {
    ev.preventDefault();
    var seat = [], item;
    $.each($('#place li.' + settings.selectingSeatCss + ' a'), function (index, value) {
        item = $(this).attr('title');                   
        seat.push(item);                
    });
    var bookseats = seat;
    $.ajax({
      type: 'POST',
      url: 'confirm.php',
      data: {'bookseats': bookseats}
    }).then(function(data) {
      alert(data);
      window.location='confirm.php';
    },function(err){
      alert('error');
    });

When sending this data,I've made an alert(data) function to see if the values are passed correctly.Below is a screenshot of the alert.alert

The alert clearly shows us that the array has been passed to the php page.But in my php page,no data of the array is displayed.And it shows an error saying "Invalid Arguments".

PHP display

Here is my php code.

$bookseats = "";

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

print_r($bookseats);

$seatar = implode(',', $bookseats);
echo 'Booked Seats : '.$seatar.'<br>'.'<br>';

And when I try to save this array in a database,the following error is shown.Database insertion error

Here is the php code for data insertion to the database.

$bookings_added = $conn->query($sql);
$last_bid = $conn->insert_id;
for($i = 0; $i < count($bookseats); $i++){
    $SQL_project_has_type = "INSERT INTO bookseat (seat, bid)
        VALUES({$bookseats[$i]}, ".$last_bid.")";
    $bs = $conn->query($SQL_project_has_type);

    if($bs === FALSE) exit("mysql error: ".$conn->error);
}

Is there something wrong in my array?Please help!

0

1 Answer 1

1

In HTML I added a hidden input to hold values of chosen seats.

In js code, added a statement that sets the value for the added hidden input.

Also on successful ajax, the form is submitted in POST method along with the new values using jquery submit trigger.

HTML:

<form method="POST" id="bookseatform" action="confirm.php">
<input type="hidden" value="" name="bookseats"/>
<div align="center"><input type="Submit" id="btnsubmit" value="Submit" /></div>
</form>

JS:

 $('#btnsubmit').click(function(ev) {
    ev.preventDefault();
    var seat = [], item;
    $.each($('#place li.' + settings.selectingSeatCss + ' a'), function (index, value) {
        item = $(this).attr('title');                   
        seat.push(item);                
    });
    var bookseats = seat;
    $('input[name="bookseats"]').val(seat);
    $.ajax({
      type: 'POST',
      url: 'confirm.php',
      data: {'bookseats': bookseats}
    }).then(function(data) {
      //alert(data);
      $('#bookseatform').trigger('submit');
    },function(err){
      alert('error');
    });
 });

PHP:

if(isset($_POST['bookseats'])){
    $bookseats = $_POST["bookseats"];   

    if(is_array($bookseats)){
        print_r($bookseats);
        $seatar = implode(',', $bookseats);
    }
    else {
        $seatar = $bookseats;
    }
}

echo 'Booked Seats : '.$seatar.'<br>'.'<br>';
Sign up to request clarification or add additional context in comments.

4 Comments

Yes to confirm.php
If you don't need the ajax part of the js code, this - pastebin.com/T9MRt2tg - is fine alternative too.
:o :o :o :o :o :o
FYI, you can avoid the is_array conditional and force the data type to array like this: $seatar = implode(',', (array)$bookseats); Effectively, if the value is a string, the string becomes the lone indexed element of the new array. ...but from what I see I would be expecting an array.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.