我需要将“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);
}
&GT;
答案 0 :(得分:2)
有些浏览器不理解这个
data : {
seatid : t_name,
},
尝试
data : {
seatid : t_name
},
删除额外的逗号。
像这样使用php
<?php
$return['error'] = $error;
echo json_encode($return);
?>
同样在js写这个,看看你得到了什么。
complete : function(data) {
alert('complete');
}
答案 1 :(得分: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;
}
}
});
});
});
答案 2 :(得分: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 ...现在我需要在用户点击表格中的seatid值时按下“保留”按钮btnShowNew将被添加到数据库
答案 3 :(得分:0)