2012-04-19 4 views
0

내가 갖고있는 것은 mySQL 데이터베이스의 항목 배열이며 각 항목에는 확인란이 있습니다. 나는 당신이 체크 박스를 클릭 할 때 확인하거나 체크하지 않은 아이템에 대한 정보를 데이터베이스에 제출할 것입니다. 내가 선택하지 않은 경우 = 1이고 확인 된 경우 = 0입니다.이 항목을 표시 할 위치입니다.체크 박스 onChange submit form

내 문제는 내 데이터베이스에 제출할 항목이없는 것 같아요. jQuery를 충분히 이해하지 못해서 함수를 작성할 수 없기 때문에 도움이 필요합니다. 여기 내 코드에 대한 것이있다.

if(isset($_POST['submit'])){ 
     foreach($_POST['id'] as $id){ 
      $value = (isset($_POST['location'][$id]) && $_POST['location'][$id]=="0" ? '0' : '1'); 
      $insert = mysql_query("UPDATE items SET location='$value' WHERE id='$id'") or die('Insert Error: '.mysql_error()); 
     } 
    } 
echo '<form id="form1" method="post"><input type="submit" name="submit" value="Submit">'; 
$result = mysql_query("SELECT * FROM items") 
    or die("Query Failed: ".mysql_error()); 
    $counter = 0; 
    echo '<div class="specialcontainer">'; 
while($row = mysql_fetch_array($result)){ 
    list($id, $item_info, $item_img, $price, $sale, $location) = $row; 
    if($location == '0'){ 
     $set_checked = ' checked="checked" '; 
    }else{ 
     $set_checked = ''; 
    } 
    if($counter % 5==0) { 
     echo '</div>'; 
     echo '<div class="specialcontainer">'; 
    } 
    echo '<div class="special"><img src="../images/items/'.$item_img.'" width="130" /><br />'; 
    echo $item_info.'<br />'; 
    echo 'Reg. $'.$price.'<br />'; 
    echo 'Sale $'.$sale.'<br />'; 
    echo 'Slide Show: <input type="checkbox" id="ch" value="0" name="location['.$id.']"'.$set_checked.' /><br />'; 
    echo '<input type="button" value="Edit" name="edit" onclick="window.location.href=\'specials.php?action=edit&id='.$id.'\'">'; 
    echo '<input type="button" value="Delete" name="Delete" onclick="window.location.href=\'specials.php?action=delete&id='.$id.'\'">'; 
    echo '<input type="hidden" name="id[]" value='.$id.' />'; 
    echo '</div>'; 
    $counter++; 
} 
echo '</div>'; 
echo '<input type="submit" name="submit" value="Submit"></form>'; 

그래서 내가 볼 수있는 것처럼 거기에 제출 버튼이 있지만 내 계획은 onChange 제출을 위해 그것을 제거하는 것입니다. 나는 onchange = "this.form.submit();을 시도했다. 확인란 매개 변수에 있지만 제대로 작동하지 않습니다. 그래서 나는 단지 체크 박스가 어떤 일에 클릭되면 언제든지 제출하기를 원한다.

답변

0

당신은 당신의 체크 박스

+0

ohive는이 하나와 다른 많은 사람들이 시도한 것처럼 $ _post 정보를 보내지 않습니다. 확인란을 클릭하면 페이지가 다시로드되지만 클릭 한 확인란은 변경되지 않습니다. – GuberX

+0

체크 상자 값을 변경하기 전에 onchange 메서드가 호출되었을 수 있습니다. 수표를 제출 한 후 모든 것이 작동하면 문제가됩니다. 그것을 해결하기 위해서 : onchange = "this.checked =! this.checked; document.getElementById ('form1'). submit()"' – Tronix117

+0

예. 나는 거기에 스크립트의 두 번째 조각을 통해 UR을 시도하고 지금은 내 체크 박스도 체크하지 않거나 더 이상 하하 선택을하지 마십시오 – GuberX

1

onchange 방법에 document.getElementById('form1').submit()을 시도해야겠습니까이 작품 같은 아약스 솔루션?

$('ch').click(function() { 
    //this is what goes into $_POST 
    var data = 'id='+ $(this).attr('name') +'&checked=' + $(this).is(':checked'); 
    $.ajax({ 
     //This would be the url that would handle updating the MySQL Record 
     url: my_mysql_handler.php, 
     cache: false, 
     type: 'POST', 
     data: data, 
     success: function(response) { 
      alert(response); //or whatever you want to do with the success/fail notification 
     } 
    }); 
}); 
+0

흠, 나는 그것을 시도하고 아무것도 일어나지 않았다. 나도 몰라 만약 내가 바로 하하 기능을 사용합니다. 이 함수는 ?? – GuberX

+0

예, 이것은 자바 스크립트입니다. – Nate