0

I need to add the "seatid" to mysql DB... the "seatid" variable is retrieved in another funtion...in another words i need to add to mysql a variable that is not find in a form but find in another function

 $("#reserveid").click(function()
{
    addSeat();

});

function addSeat()
{
var t_name    = $("#btnShowNew ['seatid']").val(); //btnShowNew is the id of the form that output the seatid variable
var errors  = '';

$.ajax({
    type    : "POST",
    url     : "INSERT.php",
    data    : { seatid  : t_name,
       },
    cache   : false, timeout: 10000,

    success  : function() {
        alert("WORKED!");
    },
    error    : function() {
        alert("DIDN'T WORK!");
    },
    complete : function() {
    }
});
}

the php file:

<?php

$host="localhost"; // Host name 
$username="root"; // Mysql username 
$password=""; // Mysql password 
$db_name="test"; // Database name 
$tbl_name="seat"; // Table name 

// Connect to server and select databse.
mysql_connect("$host", "$username", "$password")or die("cannot connect"); 
mysql_select_db("$db_name")or die("cannot select DB");

$seatid = mysql_real_escape_string($_POST['seatid']);
$error = '';

$query = "INSERT INTO seat ( seatNo ) VALUES ('{$seatid}')";

if (!mysql_query($query, $conn))
{
$error = mysql_error();
$return['error'] = $error;
echo json_encode($return);
mysql_close($conn);
}
else
{
$success = "Seat Successfully Added!";
$return['mysql'] = $success;
echo json_encode($return);
mysql_close($conn);
} 

?>

4
  • And so what is wrong with this function ? What error do you have ? Commented Feb 27, 2013 at 17:29
  • Have you check if you are entering into the php file? leave in the php only an echo (forget about all the if else and connections) Commented Feb 27, 2013 at 17:33
  • when i click on the button nothing happens :/ Commented Feb 27, 2013 at 17:34
  • 1
    what is ['seatid'] in your jQuery selector? Commented Feb 27, 2013 at 17:42

4 Answers 4

2

Some browsers do not understand this

data : { 
  seatid  : t_name,
},

try with

data : {
  seatid  : t_name
},

Delete the extra comma.

Use the php just like this

<?php

$return['error'] = $error;
echo json_encode($return);

?>

Also in the js write this, to see what you get.

complete : function(data) {
     alert('complete');
}
Sign up to request clarification or add additional context in comments.

5 Comments

Have you check if you are entering into the php file?
what do you mean?? can you explain plz
after var t_name = $("#btnShowNew ['seatid']").val(); //btnShowNew is the id of the form that output the seatid variable add alert(t_name); check if you are take a value
Check the comment I left in your question. I am editing my answer.
dear @kilkadg, i have deleted my php file and copy pasted ur code, but still nothing happen well, the seatid variable is outputted when the user click on a "show selected seats" i have created... now i want to do a reserve button when user click on it, the seatid will be delivered to the DB...maybe you have a better solution thnks
0

Maybe you have a problem with the jquery, Hope this works

$(document).ready(function() {
            $("#reserveid").click(function(){

                alert('CLICK'); //here you have to be able to see the alert if not maybe the jquery is not working properly
                var t_name    = $("#btnShowNew ['seatid']").val(); //Not sure if you are taking a value
                alert(t_name); //here you have to be able to see the value that you are catching
                $.ajax({
            url:'insert.php',
            type:'POST',
            dataType:'json', //since you are echo json_encode($return)
            data:{
                'seatid':t_name //use ' just in case
            },
            success:function (result) {
                            if(result.error){
                                alert(result.error); // .error because it is the key of the array you are giving back
                                return;
                            }
                        }
        });
    });
});

2 Comments

thanks for the effort, i rly appreciate, but this is not working...i dnt know why...i rly need help since im doing my first project for the university...if you want i can copy paste for you the entire code of my page..i dnt know if it will help
Please do it, I will try to help.
0
 <html>
 <head>
<title>Seat Reservation </title>
<script src="js/jquery.js" type="text/javascript"></script>
</head>
<body>
<form id="form111" >
<script type="text/javascript"
  src="js/jquery.js">
</script>
   <div id="screen"><p class= "pos_fixed">S C R E E N</p></div>

   <div id="holder"> 
    <ul  id="place">
    </ul>    
</div>
 <div style="width:600px;text-align:center;overflow:auto"> 
<ul id="seatDescription">
<li style="background:url('images/available_seat_img.gif') no-repeat scroll 0 0     transparent;">Available Seat</li>
<li style="background:url('images/booked_seat_img.gif') no-repeat scroll 0 0 transparent;">Booked Seat</li>
<li style="background:url('images/selected_seat_img.gif') no-repeat scroll 0 0 transparent;">Selected Seat</li>
</ul>        </div>
<div style="width:580px;text-align:left;margin:5px">
    <input type="button" id="btnShowNew" value="Show Selected Seats" name="btnShowNew" /><input type="button" id="btnShow" value="Show All" /> </form> 
<table width="300" border="0" >
<tr>
<form name="form1" method="post" action="login.html">
<td>
<table width="100%" border="0" >
<tr>
<td width="294"><input type="submit" name="Submit" value="Logout"></td>
</tr>
</form>



 <form method="post" id="reserveid">
 <td>
 <table width="100%" border="0" >
 <tr>
 <td width="294"><input type="button" value="reserve""></td>
 </tr>
 </form>



 <form id="cvvform" method="post" action="checkcvv.php">
 <td>
 <table width="100%" border="0" >
 <tr>
 <td width="294"><input type="submit" name="cvvchecking" value="cvvchecking"          id="cvvchecking"></td>
 </tr>
 <tr>
 <td>CVV<input type="text" maxlength="4" id="cvvchecking"  name="cvvchecking" /></td>
 <td>USERNAME<input type="text" id="username"  name="username" /></td>
 </tr>
 </form>



    <script type="text/javascript">
    $(function () {
        var settings = {
            rows: 5,
            cols: 15,
            rowCssPrefix: 'row-',
            colCssPrefix: 'col-',
            seatWidth: 35,
            seatHeight: 35,
            seatCss: 'seat',
            selectedSeatCss: 'selectedSeat',
            selectingSeatCss: 'selectingSeat'
        };

        var init = function (reservedSeat) {
            var str = [], 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;
                    }
                    str.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(str.join(''));
        };

        //case I: Show from starting 
        init();

        //Case II: If already booked
        var bookedSeats = [];
        init(bookedSeats);


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

        $('#btnShow').click(function () {
            var str = [];
            $.each($('#place li.' + settings.selectedSeatCss + ' a, #place li.'+ settings.selectingSeatCss + ' a'), function (index, value) {
                str.push($(this).attr('title'));
            });
            alert(str.join(','));
        })

        $('#btnShowNew').click(function () {
            var str = [], seatid;
            $.each($('#place li.' + settings.selectingSeatCss + ' a'), function (index, value) {
                seatid = $(this).attr('title');                   
               str.push(seatid);                   
            });
            alert(str.join(' , '));
        })

})

</script>

all this code is working fine...the "btnShowNew" output the seats id clicked by the user...now i need to make a button "reserve" when a user click on it the value of seatid inside the form btnShowNew will be added to the database

Comments

0

Sorry your code it is not tidy can not see just the part you need. But there are some ideas, I can give you, it is up to you to solve your problem or make a code much clear (Just the part you need).

You know the forms send data from one page to another (with POST or GET method) so when you press a submit button send all the inputs to the action of the form. You can see the example in this page

After that you have to save that data into the data base like in here

Comments

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.