2012-08-30 28 views
0

我在這裏有一個代碼,其中我有一個複選框數組填充MySQL數據通過查詢。我可以成功地將選中的複選框的值提交到數據庫中的表中。但是我想要做的是根據用戶檢查複選框的順序保存它。我被告知,我可以通過JavaScript實現這一目標,但不幸的是,我根本不熟悉JavaScript。如果可能的話,你能提供僅通過PHP實現的步驟嗎?如果沒有,請指導我如何使用JavaScript實現這一目標,因爲我是一個新手。這裏是我的示例代碼,讓你想象我想要做什麼..確定選中的複選框序列,然後保存到MySQL數據庫

<?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()); 
} 
}}} 

} 

說明是最受歡迎的。希望你能幫我!非常感謝!

回答

0

撥通了這個代碼檢測選中的複選框序列:

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

我在我的複選框綁定它。

$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中。

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

我可以在數據庫中保存通過此:

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()); 

}; 
} 

謝謝大家的幫助!上帝保佑!

1

您必須藉由checboxes一個隱藏的輸入,那麼當你提交表單,你將有$_POST['sequence']與所有點擊次數依次爲:

​<input id="sequence" name="sequence" /> 
<input type="checkbox" class="check" name="checkbox1" value="checkbox1"​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​> 
<input type="checkbox" class="check" name="checkbox2" value="checkbox2"> 
<input type="checkbox" class="check" name="checkbox3" value="checkbox3"> 
<input type="checkbox" class="check" name="checkbox4" value="checkbox4"> 

然後你在這個與check類中的每個複選框綁定點擊例如

var windowOnload=window.onload||function(){};window.onload=function(){windowOnload();}; 
window.onload = function(){ 

    var all = document.getElementsByTagName("input"); 
    for (var i=0;i<all.length;i++) { 
     if(all[i].className == 'check'){ 
      all[i].onclick = inputClickHandler; 
     }    
    } 
}; 

function inputClickHandler(e){ 
    e = e||window.event; 
    var inputElm = e.target||e.srcElement; 
    var text = inputElm.value; 
    var target = document.getElementById('sequence'); 
    if(inputElm.checked == true){ 
     if(target.value == ''){ 
      target.value = text; 
     } else { 
      target.value = target.value+','+text; 
     } 
    } else { 
     var textToSearch = text+','; 
     var textAlternative = ','+text; 
     var regex = new RegExp('\\b' + textToSearch + '\\b'); 
     var regex2 = new RegExp('\\b' + textAlternative + '\\b'); 
     if(regex.test(target.value)){ 
      target.value = target.value.replace(textToSearch, ''); 
     } else if(regex2.test(target.value)){ 
      target.value = target.value.replace(textAlternative, ''); 
     } else { 
      target.value = target.value.replace(text, ''); 
     } 
    } 
}​ 

您可以使用jQuery也做到這一點(這是更清潔和可靠的):

$(window).load(function(){ 
    $('.check').on('click', function(){ 
     if($(this).attr('checked')){ 
      if($('#sequence').val() == ''){ 
       $('#sequence').val($(this).val()); 
      } else { 
       $('#sequence').val($('#sequence').val()+','+$(this).val()); 
      } 
     } else { 
      var targetValue = $('#sequence').val(); 
      var textToSearch = $(this).val()+','; 
      var textAlternative = ','+$(this).val(); 
      var regex = new RegExp('\\b' + textToSearch + '\\b'); 
      var regex2 = new RegExp('\\b' + textAlternative + '\\b'); 
      if(regex.test(targetValue)){ 
       $('#sequence').val(targetValue.replace(textToSearch, '')); 
      } else if(regex2.test(targetValue)){ 
       $('#sequence').val(targetValue.replace(textAlternative, '')); 
      } else { 
       $('#sequence').val(targetValue.replace($(this).val(), '')); 
      } 
     } 
    }); 
});​ 
+0

這對我來說有點複雜。正如我告訴過你的,我是JavaScript或jQuery的新手。沒有更簡單的方法來實現這一目標嗎?非常感謝您的回覆。 – xjshiya

+0

比這更簡單嗎? ..不這麼認爲.. –

+0

我試着運行你提供的代碼,以及jQuery代碼,但它所做的只是顯示一個文本框和四個複選框。除此之外,它什麼也做不了。我很抱歉我缺乏,但我真的不明白。 :(但仍然,謝謝你的回覆。:) – xjshiya

相關問題