本文介绍了标识复选框序列,然后保存到mysql数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我这里有一个代码,其中我有一个复选框数组填充mysql数据通过查询。我可以成功地将选中的复选框的值提交到数据库中的表中。但是我想做的是根据用户选中复选框的顺序保存它。我被告知,我可以通过javascript实现这一点,但不幸的是,我不熟悉javascript在所有。如果可能,您能提供通过PHP实现这一步的步骤吗?如果没有,你能指导我如何实现这个使用javascript,因为我是一个新手。这里是我的示例代码,让你想象我想做什么..

I have here a code wherein I have an array of checkbox populated with mysql data through a query. I can successfully submit the value of the checked checkbox into a table in a database. But what I want to do is to save it according to the sequence the checkbox was checked by the user. I was told that I can achieve this through javascript but unfortunately, I'm not familiar with javascript at all. If possible, can you provide steps on achieving this through PHP only? If not, can you please guide me on how to achieve this using javascript since I'm a newbie. Here's my sample code to let you imagine what I'm trying to do..

<?php
require("aacfs.php");
echo"<div align= center >
  <table width=300 border= 1 align= right><tr><th align=center bgcolor=silver><font face=consolas>Choose Itinerary</font></th></tr>
    <tr>
      <td></br>
      <form method=post>";


$result=mysql_query(" select * from itinerary group by location");
    $y=mysql_affected_rows();
    for($i=0;$i<$y;$i++)
    {$row=mysql_fetch_array($result);
        $pr=$row['icode'];
        $n=$row['location'];
        $l=$row['btime'];
echo"<table border=1 align=center width=250>
          <tr><td width=15><input name=prod[] type=checkbox value='$pr'></td><td>$n</td></tr>
          </table>";
        $t=$_POST[prod];

        }
echo"</td></tr></table>";

echo"

    <table width= 664 border= 1  align= left >
    <tr><td height= 282 ><p align= center  class= style77>
<p align= right ><h4>$d</h4></p>
<p align= center ><font face=consolas><b>Itinerary List</b></font></p>
<p align=center><font face=consolas>To change your itinerary list, choose again.</font></p>

<br>
  <center><input name=add  type= submit  value='Add to Itinerary List'></center>
<br>
<table width=654  border= 1>
  <tr bgcolor = silver align=center>
    <td>Itinerary</td>
    <td>Block Time</td>

 </tr></form>";


 if(isset($_POST['add']))
{
if(isset($_POST[prod]))
 {  foreach($t as $k)
    {$result=mysql_query("select * from itinerary where icode='$k'");
    $y=mysql_affected_rows();

for($x=0;$x<$y;$x++)
{$row=mysql_fetch_array($result);
        $r=array($row['icode']);
        $n=$row['location'];
        $p=$row['btime'];



    echo"<form method=post><tr>
<td>$n</td>
<td>$p</td>
 </tr>";

    $a=$_POST[pro];
    $stat1='Not Submitted';

foreach($r as $a=>$l)
{

$kdot=mysql_query("select max(reservno) as 'maxr' from reservation") or die(mysql_error());
$row2=mysql_fetch_array($kdot);
$fi=$row2['maxr'];
mysql_query("insert into location_list(reservno, icode, location, status) values('$fi', '$l', '$n', '$stat1')") or die(mysql_error());
}
}}}

}

澄清是最受欢迎的。希望你能帮助我!非常感谢!

Clarifications are most welcome. Hope you can help me! Thanks a lot!

推荐答案

通过此代码检测到复选框序列:

Got the detection of checked checkbox sequence through this code:

<script type="text/javascript">
<!--

$(document).ready(function(){

    var array = [];


    $("input[type=button]").click(function(){
    for (var i = 0; i < array.length; i++){
    if (array[i] == $(this).attr('value')){
    array.splice(i, 1);
    }
    }
    alert(array.length);
    });




    $('input[type="checkbox"]').click(function(){

    if ($(this).attr('checked')){
    // Add the new element if checked:
    array.push($(this).attr('value'));
    //alert(array2.push($(this).attr('name')));
    }

    else{
    // Remove the element if unchecked:
    for (var i = 0; i < array.length; i++){
    if (array[i] == $(this).attr('value')){
    array.splice(i, 1);
    }
    }
    }






        $("#result").show(function(){
        $(this).val("");
        for (var i = 0; i < array.length; i++){
    $(this).val($(this).val() + " " + array[i]+ "\n");
        }
        });

    });

});
//-->
</script>

我在我的复选框中绑定它。

I'm binding it in my checkbox.

$result=mysql_query(" select * from itinerary group by location");
    $y=mysql_affected_rows();
    for($i=0;$i<$y;$i++)
    {$row=mysql_fetch_array($result);
        $pr=$row['icode'];
        $n=$row['location'];
        $l=$row['btime'];
echo"<table border=1 align=center width=250>
          <tr><td width=15>
          <input type='checkbox' value='$n -> $l' id='cb1'>
</td><td>$n</td><td width=4>$l</td></tr>
          </table>";

然后在textarea中输出。

Then output it in a textarea.

<textarea name=resulta id='result' style='height:200px; width:350px' disabled='disabled'></textarea>

我可以通过以下方式将其保存在我的数据库中:

I can save it in my database through this:

echo "<form><center><input type=submit name='pindot' value='Gora Men~'></center></form>";
$val=$_POST['resulta'];
if (isset($_POST['pindot']))
{
$lines = explode("\n", $val);

foreach ($lines as $line) {
    // $line here is a string
    // build a query based on it and execute it
    mysql_query("insert into postflight (sample_lang) values ('$line')") or die(mysql_error());

};
}

感谢大家的帮助!上帝保佑!

Thank you all for your help! God bless!

这篇关于标识复选框序列,然后保存到mysql数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-20 07:34
查看更多