问题描述
我需要一些JS / AJAX / jQuery脚本将数据保存到数据库中动态当我检查的复选框。此刻或复选框中的下一个加载到记录和更改取决于如果检查或not.but我必须重新加载页面我选择一个后,将其保存到数据库中的数据库变量。我能做的一切,但知道如何实现AJAX这个,所以我不必提交查询每次刷新页面。任何帮助是极大的AP preciated。
<表格名称=form1aa方法=邮报行动=process.php FN = LT ;?回声$行['第一'];?>&放;类=< PHP的echo $行['类'];?>&放大器;最后=< PHP的echo $行['最后'];?>
&功放;模型=< PHP的echo $行['模型']; ?>&安培; CAS =< PHP的echo $行['案件']; ?>&安培; UPID =< PHP的echo $ ID; ?>&安培;组= 1ID =form1a>
<选择名称=输入的onchange =fill_damage(document.form1aa.type.selectedIndex);>
<期权价值=硬件>五金LT; /选项>
<期权价值=软件>软件和LT; /选项>
< /选择>
<选择名称=损害>
< /选择>
<输入类型=文字名称=意见占位符=评论栏>
<输入类型=文字名称=成本占位符=成本>
<输入类型=提交值=保存NAME =保存>
< /形式GT;
< PHP
//作业状态
如果(使用isset($ _ POST ['复选框'])){$复选框= $ _ POST ['复选框'];
如果(使用isset($ _ POST ['激活'])$激活= $ _ POST [激活]:$停用= $ _ POST [禁用])
$ ID =('内爆(,,$复选框)。'); ;
$ SQL =UPDATE修理设置状态='。(?使用isset($激活)已完成:正在执行)WHERE ID = $编号;。
$结果= mysql_query($的SQL)或死亡(mysql_error());
}
//结束作业状态
//付款状态
如果(使用isset($ _ POST ['薪水'])){$薪水= $ _ POST ['薪水'];
如果(使用isset($ _ POST ['支付'])$支付= $ _ POST [有偿?]:$无薪= $ _ POST [无偿])
$ ID =('内爆(,,$片酬)。'); ;
$ SQL =UPDATE修理SET支付='(使用isset($支付)?'有偿':'无薪')。WHERE ID = $编号;
$结果= mysql_query($的SQL)或死亡(mysql_error());
}
//结束付款状态
//返回状态
如果(使用isset($ _ POST ['retcheck'])){$ retcheck = $ _ POST ['retcheck'];
如果(使用isset($ _ POST ['退役'])$ RET = $ _ POST [沤]:$未返回= $ _ POST [未返回])
$ ID =('内爆(,,$ retcheck)。'); ;
$ SQL =UPDATE修理SET RET ='(使用isset($ RET)?'重新调谐:在办公室)。WHERE ID = $编号;
$结果= mysql_query($的SQL)或死亡(mysql_error());
}
//结束返回状态
$ SQL =SELECT * FROM $ tbl_name;
如果(使用isset($ _ POST ['所有'])){
$ SQL =SELECT * FROM $ tbl_name;
}
如果(使用isset($ _ POST ['TPC'])){
$ SQL =SELECT * FROM $ tbl_name WHERE类LIKE'1%';
}
如果(使用isset($ _ POST ['金'])){
$ SQL =SELECT * FROM $ tbl_name WHERE类LIKE'D%';
}
如果(使用isset($ _ POST ['BSC'])){
$ SQL =SELECT * FROM $ tbl_name WHERE类LIKE'B%';
}
$结果= mysql_query($ SQL);
?>
<表格名称=frmactive方法=邮报行动=>
<输入名称=激活式=提交ID =激活值=完成作业/>
<输入名称=有偿TYPE =提交ID =付款值=付款状态/>
<输入名称=RET类型=提交ID =RET值=返回2学生/>
< BR />
<一个ID =displayText的href =JavaScript的:切换();>显示额外< / A>
< DIV ID =toggleText的风格=显示:无>
< BR />
<输入名称=未返回类型=提交ID =未返回值=在办公室/>
<输入名称=无偿类型=提交ID =无偿值=不付款/>
<输入名称=禁用TYPE =提交ID =取消值=进行中/>< / DIV>
<表格的宽度=1000的边界=0的cellpadding =3CELLSPACING =1>
< THEAD>
百分位宽度=67ALIGN =中心><强>开始日期和LT; / STRONG>< /第i个
百分位宽度=50ALIGN =中心><强>案例< / STRONG>< /第i个
百分位宽度=34ALIGN =中心><强>类型< / STRONG>< /第i个
百分位宽度=120ALIGN =中心><强>损伤和LT; / STRONG>< /第i个
百分位宽度=31ALIGN =中心><强>注释< / STRONG>< /第i个
百分位宽度=31ALIGN =中心><强>成本< / STRONG>< /第i个
百分位宽度=90ALIGN =中心><强>付款状态< / STRONG>< /第i个
百分位宽度=100ALIGN =中心><强> ??? 2学生和LT; / STRONG>< /第i个
百分位宽度=100ALIGN =中心><强>招聘岗位< / STRONG>< /第i个
< / THEAD>
< PHP
而($行= mysql_fetch_array($结果)){
?>
&其中; TR>
< TD>&LT ;?回声$行['开始']; ?>< / TD>
< TD>&LT ;?回声$行['案件']; ?>< / TD>
< TD>&LT ;?回声$行['类型']; ?>< / TD>
< TD宽度=70>&LT ;?回声$行['损害']; ?>< / TD>
< TD宽度=70>&LT ;?回声$行['评论']; ?>< / TD>
< TD>&LT ;?回声$;回声$行['成本']; ?>< / TD>
<! - 付款显示器(开始) - >
< PHP
如果($行['支付'] ==付费)
{
?>
< TD><输入名称=月光[]类型=复选框ID =月光[]值=&LT ;?回声$行['身份证'];?>>
&LT ;?回声$行['支付'];?>
< / TD>
&LT ;?
}
如果($行['支付'] ==未付款)
{
?>
< TD宽度=21><输入名称=月光[]类型=复选框ID =月光[]值=&LT ;?回声$行['身份证'];?> >
&LT ;?回声$行['支付']; ?>
< / TD>
&LT ;?
}
如果($行['退役'] ==){
?>
< TD宽度=50>无数据< / TD>
&LT ;?
}
?>
做到这一点使用jQuery,一个简单的例子是:
HTML:
<输入类型=复选框名称=选项1值=奶>
<输入类型=复选框名称=选项2值=糖>
<输入类型=复选框名称=2选项值=巧克力>
JS:
$(输入[类型=复选框])。在('点击',函数(){
VAR检查= $(本).attr('检查');
如果(检查){
VAR值= $(本).VAL();
.post的$('为file.php',{值:值},功能(数据){
//数据= 0 - 表示有一个错误
//数据= 1 - 表示一切正常
如果(数据== 1){
//做某件事,或者什么也不做:-)
警报(数据保存在数据库!');
}
});
}
});
PHP:为file.php
< PHP
如果($ _ POST和放大器;&安培;使用isset($ _ POST ['值'])){
//数据库连接
$链路的mysql_connect =('本地主机','mysql_user','mysql_password');
如果(!$链接){
//发生错误
印刷(0);
}
mysql_select_db('MYDB');
//消毒值
$值= mysql_real_escape_string($ _ POST ['值']);
//开始查询
$ SQL =INSERT INTO表(值)VALUES('$值');
//检查查询被执行死刑
如果(的mysql_query($的SQL,$链接)){
//一切正常,插入数据
打印(1);
} 其他 {
//发生错误
印刷(0);
}
}
?>
I need some js/ajax/jquery script saving data to database dynamically when I check the check-box.the checkboxes at the moment or loaded in next to records and change the variable in the database depending if its checked or not.but i have to reload the page after i select one to save it to the database. i can do everything else but understand how to implement the ajax to this so i don't have to submit the query and refresh the page every time. any help is greatly appreciated.
<form name="form1aa" method="post" action="process.php?fn=<? echo $rows['first']; ?>&class=<?php echo $rows['class']; ?>&last=<?php echo $rows['last']; ?>
&model=<?php echo $rows['model']; ?>&cas=<?php echo $rows['cases']; ?>&upid=<?php echo $id; ?>&group=1" id="form1a" >
<select name="type" onchange=" fill_damage (document.form1aa.type.selectedIndex); ">
<option value="Hardware">Hardware</option>
<option value="Software">Software</option>
</select>
<select name="damage">
</select>
<input type=text name="comment" placeholder="Comments Box">
<input type=text name="cost" placeholder="Cost">
<input type="submit" value="Save" name="Save">
</form>
<?php
//Job Status
if(isset($_POST['checkbox'])){$checkbox = $_POST['checkbox'];
if(isset($_POST['activate'])?$activate = $_POST["activate"]:$deactivate = $_POST["deactivate"])
$id = "('" . implode( "','", $checkbox ) . "');" ;
$sql="UPDATE repairs SET status = '".(isset($activate)?'Completed':'In Progress')."' WHERE id=$id" ;
$result = mysql_query($sql) or die(mysql_error());
}
//End Job Status
//Payment Status
if(isset($_POST['paycheck'])){$paycheck = $_POST['paycheck'];
if(isset($_POST['paid'])?$paid = $_POST["paid"]:$unpaid = $_POST["unpaid"])
$id = "('" . implode( "','", $paycheck ) . "');" ;
$sql="UPDATE repairs SET paid = '".(isset($paid)?'Paid':'Unpaid')."' WHERE id=$id" ;
$result = mysql_query($sql) or die(mysql_error());
}
//End Payment Status
//Return Status
if(isset($_POST['retcheck'])){$retcheck = $_POST['retcheck'];
if(isset($_POST['ret'])?$ret = $_POST["ret"]:$unret = $_POST["unret"])
$id = "('" . implode( "','", $retcheck ) . "');" ;
$sql="UPDATE repairs SET ret = '".(isset($ret)?'Retuned':'In Office')."' WHERE id=$id" ;
$result = mysql_query($sql) or die(mysql_error());
}
//End Return Status
$sql="SELECT * FROM $tbl_name";
if(isset($_POST['all'])){
$sql="SELECT * FROM $tbl_name";
}
if(isset($_POST['tpc'])){
$sql="select * from $tbl_name WHERE class LIKE '1%'";
}
if(isset($_POST['drc'])){
$sql="select * from $tbl_name WHERE class LIKE 'D%'";
}
if(isset($_POST['bsc'])){
$sql="select * from $tbl_name WHERE class LIKE 'B%'";
}
$result=mysql_query($sql);
?>
<form name="frmactive" method="post" action="">
<input name="activate" type="submit" id="activate" value="Complete Job" />
<input name="paid" type="submit" id="Payment" value="Payment Status" />
<input name="ret" type="submit" id="ret" value="Returned 2 Student" />
<br />
<a id="displayText" href="javascript:toggle();">Show Extra</a>
<div id="toggleText" style="display: none">
<br />
<input name="unret" type="submit" id="unret" value="In Office" />
<input name="unpaid" type="submit" id="unpaid" value="Not Paid" />
<input name="deactivate" type="submit" id="deactivate" value="In Progress" /></div>
<table width="1000" border="0" cellpadding="3" cellspacing="1">
<thead>
<th width="67" align="center"><strong>Start Date</strong></th>
<th width="50" align="center"><strong>Cases</strong></th>
<th width="34" align="center"><strong>Type</strong></th>
<th width="120" align="center"><strong>Damage</strong></th>
<th width="31" align="center"><strong>Comment</strong></th>
<th width="31" align="center"><strong>Cost</strong></th>
<th width="90" align="center"><strong>Payment Status</strong></th>
<th width="100" align="center"><strong>Returned 2 Student</strong></th>
<th width="100" align="center"><strong>Job Status</strong></th>
</thead>
<?php
while($rows=mysql_fetch_array($result)){
?>
<tr>
<td><? echo $rows['start']; ?></td>
<td><? echo $rows['cases']; ?></td>
<td><? echo $rows['type']; ?></td>
<td width="70"><? echo $rows['damage']; ?></td>
<td width="70"><? echo $rows['comment']; ?></td>
<td><? echo "$"; echo $rows['cost']; ?></td>
<!--Payment Display(Start)-->
<?php
if($rows['paid']=="Paid")
{
?>
<td><input name="paycheck[]" type="checkbox" id="paycheck[]" value="<? echo $rows['id']; ?>">
<? echo $rows['paid'];?>
</td>
<?
}
if($rows['paid']=="Unpaid")
{
?>
<td width="21"><input name="paycheck[]" type="checkbox" id="paycheck[]" value="<? echo $rows['id']; ?>">
<? echo $rows['paid']; ?>
</td>
<?
}
if($rows['ret']==""){
?>
<td width="50">No Data</td>
<?
}
?>
Do it with jQuery, a simple example could be:
HTML:
<input type="checkbox" name="option1" value="Milk">
<input type="checkbox" name="option2" value="Sugar">
<input type="checkbox" name="option3" value="Chocolate">
JS:
$("input[type='checkbox']").on('click', function(){
var checked = $(this).attr('checked');
if(checked){
var value = $(this).val();
$.post('file.php', { value:value }, function(data){
// data = 0 - means that there was an error
// data = 1 - means that everything is ok
if(data == 1){
// Do something or do nothing :-)
alert('Data was saved in db!');
}
});
}
});
PHP: file.php
<?php
if ($_POST && isset($_POST['value'])) {
// db connection
$link = mysql_connect('localhost', 'mysql_user', 'mysql_password');
if (!$link) {
// error happened
print(0);
}
mysql_select_db('mydb');
// sanitize the value
$value = mysql_real_escape_string($_POST['value']);
// start the query
$sql = "INSERT INTO table (value) VALUES ('$value')";
// check if the query was executed
if(mysql_query($sql, $link)){
// everything is Ok, the data was inserted
print(1);
} else {
// error happened
print(0);
}
}
?>
这篇关于复选框数据自动保存到上单击数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!