任何人都可以告诉我如何更改getrowid以获取所选id的值,我需要获取所选id的值以传递给sql。
<!DOCTYPE html>
<html lang="en">
<head>
<link rel="stylesheet" href="../jqwidgets/styles/jqx.base.css" type="text/css" />
<link rel="stylesheet" href="../jqwidgets/styles/jqx.classic.css" type="text/css" />
<script type="text/javascript" src="../jquery-1.7.1.min.js"></script>
<script type="text/javascript" src="../jqwidgets/jqxcore.js"></script>
<script type="text/javascript" src="../jqwidgets/jqxbuttons.js"></script>
<script type="text/javascript" src="../jqwidgets/jqxscrollbar.js"></script>
<script type="text/javascript" src="../jqwidgets/jqxmenu.js"></script>
<script type="text/javascript" src="../jqwidgets/jqxcheckbox.js"></script>
<script type="text/javascript" src="../jqwidgets/jqxlistbox.js"></script>
<script type="text/javascript" src="../jqwidgets/jqxdropdownlist.js"></script>
<script type="text/javascript" src="../jqwidgets/jqxgrid.js"></script>
<script type="text/javascript" src="../jqwidgets/jqxdata.js"></script>
<script type="text/javascript" src="../jqwidgets/jqxgrid.selection.js"></script>
<script type="text/javascript">
$(document).ready(function () {
// prepare the data
var data = {};
var theme = 'classic';
var ide= new Array();
var namee= new Array();
var emaile= new Array();
var subjecte= new Array();
var confirmcodee= new Array();
var usernamee= new Array();
var passworde= new Array();
var generaterow = function () {
var row = {};
var firtnameindex = ide.length-1;
row["id"] = ide[firtnameindex];
row["name"] = namee[firtnameindex];
row["email"] = emaile[firtnameindex];
row["subject"] = subjecte[firtnameindex];
row["confirmcode"] = confirmcodee[firtnameindex];
row["username"] = usernamee[firtnameindex];
row["password"] = passworde[firtnameindex];
return row;
}
var source =
{
datatype: "json",
datafields: [
{ name: 'id' },
{ name: 'name' },
{ name: 'email' },
{ name: 'subject' },
{ name: 'confirmcode' },
{ name: 'username' },
{ name: 'password' }
],
ide: 'id',
url: 'data.php',
addrow: function (rowid, rowdata) {
// synchronize with the server - send insert command
var data = "insert=true&" + $.param(rowdata);
$.ajax({
dataType: 'json',
url: 'data.php',
data: data,
success: function (data, status, xhr) {
// insert command is executed.
}
});
},
deleterow: function (rowdata) {
// synchronize with the server - send delete command
var data = "delete=true&id=" + rowdata;
$.ajax({
dataType: 'json',
url: 'data.php',
data: data,
success: function (data, status, xhr) {
// delete command is executed.
}
});
},
updaterow: function (rowid, rowdata) {
// synchronize with the server - send update command
var data = "update=true&" + $.param(rowdata);
$.ajax({
dataType: 'json',
url: 'data.php',
data: data,
success: function (data, status, xhr) {
// update command is executed.
}
});
}
};
// initialize jqxGrid
$("#jqxgrid").jqxGrid(
{
width: 500,
height: 350,
source: source,
theme: theme,
columns: [
{ text: 'id', datafield: 'id', width: 100 },
{ text: 'name', datafield: 'name', width: 100 },
{ text: 'email', datafield: 'email', width: 100 },
{ text: 'subject', datafield: 'subject', width: 180 },
{ text: 'confirmcode', datafield: 'confirmcode', width: 180 },
{ text: 'username', datafield: 'username', width: 100 },
{ text: 'password', datafield: 'password', width: 140 }
]
});
$("#addrowbutton").jqxButton({ theme: theme });
$("#deleterowbutton").jqxButton({ theme: theme });
$("#updaterowbutton").jqxButton({ theme: theme });
// update row.
$("#updaterowbutton").bind('click', function () {
var datarow = generaterow();
var selectedrowindex = $("#jqxgrid").jqxGrid('getselectedrowindex');
var rowscount = $("#jqxgrid").jqxGrid('getdatainformation').rowscount;
if (selectedrowindex >= 0 && selectedrowindex < rowscount) {
var ide = $("#jqxgrid").jqxGrid('getrowid', selectedrowindex);
$("#jqxgrid").jqxGrid('updaterow', ide, datarow);
}
});
// create new row.
$("#addrowbutton").bind('click', function () {
var idw = prompt("Enter ID","");
ide.push(idw);
var namew = prompt("Enter name","");
namee.push(namew);
var emailw = prompt("Enter email","");
emaile.push(emailw);
var subjectw = prompt("Enter subject","");
subjecte.push(subjectw);
var confirmcodew = prompt("Enter confirm code","");
confirmcodee.push(confirmcodew);
var usernamew = prompt("Enter username","");
usernamee.push(usernamew);
var passwordw = prompt("Enter password for the username","");
passworde.push(passwordw);
var rowscount = $("#jqxgrid").jqxGrid('getdatainformation').rowscount;
var datarow = generaterow();
$("#jqxgrid").jqxGrid('addrow', null, datarow);
});
// delete row.
$("#deleterowbutton").bind('click', function () {
var selectedrowindex = $("#jqxgrid").jqxGrid('getselectedrowindex');
var rowscount = $("#jqxgrid").jqxGrid('getdatainformation').rowscount;
if (selectedrowindex >= 0 && selectedrowindex < rowscount) {
var ide = $("#jqxgrid").jqxGrid('getrowid', selectedrowindex);
$("#jqxgrid").jqxGrid('deleterow', ide);
}
});
}
);
</script>
</head>
<body class='default'>
<div id='jqxWidget' style="font-size: 13px; font-family: Verdana; float: left;">
<div style="float: left;" id="jqxgrid">
</div>
<div style="margin-left: 30px; float: left;">
<div>
<input id="addrowbutton" type="button" value="Add New Row" />
</div>
<div style="margin-top: 10px;">
<input id="deleterowbutton" type="button" value="Delete Selected Row" />
</div>
<div style="margin-top: 10px;">
<input id="updaterowbutton" type="button" value="Update Selected Row" />
</div>
</div>
</div>
</body>
</html>
我需要将getrowid更改为在所选行中获取id的值,如何???
这是我的data.php
<?php
error_reporting(0);
#Include the connect.php file
include('connect.php');
#Connect to the database
//connection String
$connect = mysql_connect($hostname, $username, $password)
or die('Could not connect: ' . mysql_error());
//Select The database
$bool = mysql_select_db($database, $connect);
if ($bool === False){
print "can't find $database";
}
// get data and store in a json array
$query = "SELECT * FROM weekang";
if (isset($_GET['insert']))
{
// INSERT COMMAND
$insert_query = "INSERT INTO `weekang`(`id`, `name`, `email`, `subject`, `confirmcode`, `username`, `password`) VALUES ('".$_GET['id']."','".$_GET['name']."','".$_GET['email']."','".$_GET['subject']."','".$_GET['confirmcode']."','".$_GET['username']."','".$_GET['password']."')";
$result = mysql_query($insert_query) or die("SQL Error 1: " . mysql_error());
echo $result;
}
else if (isset($_GET['update']))
{
// UPDATE COMMAND
$update_query = "UPDATE `weekang` SET `name`='".$_GET['name']."',
`email`='".$_GET['email']."',
`subject`='".$_GET['subject']."',
`confirmcode`='".$_GET['confirmcode']."',
`username`='".$_GET['username']."',
`password`='".$_GET['password']."' WHERE `id`='".$_GET['id']."'";
$result = mysql_query($update_query) or die("SQL Error 1: " . mysql_error());
echo $result;
}
else if (isset($_GET['delete']))
{
// DELETE COMMAND
$delete_query = "DELETE FROM `weekang` WHERE `id`='".$_GET['id']."'";
$result = mysql_query($delete_query) or die("SQL Error 1: " . mysql_error());
echo $result;
}
else
{
// SELECT COMMAND
$result = mysql_query($query) or die("SQL Error 1: " . mysql_error());
while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
$weekang[] = array(
'id' => $row['id'],
'name' => $row['name'],
'email' => $row['email'],
'subject' => $row['subject'],
'confirmcode' => $row['confirmcode'],
'username' => $row['username'],
'password' => $row['password']
);
}
echo json_encode($weekang);
}
?>
答案 0 :(得分:0)
您可以使用'getrowdata'方法。它将返回特定索引处的数据记录。