0

我需要将“seatid”添加到 mysql DB...“seatid”变量在另一个函数中检索...换句话说,我需要向 mysql 添加一个在表单中找不到但在另一个函数中找到的变量

 $("#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() {
    }
});
}

php文件:

<?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

4 回答 4

2

有些浏览器不理解这个

data : { 
  seatid  : t_name,
},

尝试

data : {
  seatid  : t_name
},

删除多余的逗号。

像这样使用php

<?php

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

?>

另外在js里写这个,看看你得到了什么。

complete : function(data) {
     alert('complete');
}
于 2013-02-27T17:31:20.340 回答
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>

所有这些代码都工作正常......“btnShowNew”输出用户点击的座位ID......现在我需要在用户点击它时制作一个按钮“保留”btnShowNew表单内的座位id的值将是添加到数据库

于 2013-02-28T20:11:04.883 回答
0

也许您对 jquery 有疑问,希望这可行

$(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;
                            }
                        }
        });
    });
});
于 2013-02-27T19:25:10.427 回答
0

抱歉,您的代码不整洁,无法看到您需要的部分。但是有一些想法,我可以给你,由你来解决你的问题或使代码更清晰(只是你需要的部分)。

您知道表单将数据从一个页面发送到另一个页面(使用 POST 或 GET 方法),因此当您按下提交按钮时,会将所有输入发送到表单的操作。您可以在此页面中查看示例

之后,您必须像这里一样将该数据保存到数据库中

于 2013-03-03T05:25:49.407 回答