I’m new to jQuery (or all things javascript, really) but know my way around php and basic programming – sorry if my question sounds utterly stupid, trying my best to learn.
I’m using a seat selection jQuery script outlined on TechBrij
This is the part of the code that selects the seat (from a layout of airplane/bus seats)
$('.' + settings.seatCss).click(function () {
if ($(this).hasClass(settings.selectedSeatCss)){
alert('This seat is already reserved');
} else{
$(this).toggleClass(settings.selectingSeatCss);
}
});
$('#btnShowNew').click(function () {
var str = [], item;
$.each($('#place li.' + settings.selectingSeatCss + ' a'), function (index, value) {
item = $(this).attr('title');
str.push(item);
});
alert(str.join(','));
})
The selected Seat(s) are shown in an alert window on a click of a button & I want exactly that value.
I now want to pass the selected seat number into $_GET (or just into a php variable which I can then use as a $_GET value and just continue with it in PHP). If I understand it right I somehow need to grab the variable “str”? How would I do that?
Also, is there a way to limit the script to only allow ONE selected seat at a time? Right now it will only give an alert if I click on an already booked seat, I can select as many seats as I like, toggle them selected/unselected and such. Just setting it up that click on a seat set to “selectingSeatCss” will result in an alert would also stop me from unselecting it I guess.
The second part of your question probably deserves its own question, but as for the main question:
If you are trying to pass the JavaScript variabble into a PHP script, you need to use ajax:
You probably want to read more about jQuery’s
$.ajaxhere:http://api.jquery.com/jQuery.ajax/
Although, it really might make more sense to just do something with the
seatsvalue using JavaScript on the page, depending on what you are trying to do. If you want to save it to a database, you will probably want to send it to your PHP script.UPDATE
To just put the value in a form, and submit the form, create a regular HTML form (you can put
display: nonein the css to hide it) and then:In this example, the form might look like this:
Then the value will be available to the recieving script in
$_POST['seats']