2014-02-25 39 views
0

任何人都可以用我的代碼在ajax中幫助我....我在ajax中的腳本代碼中有一些錯誤,因爲forloop使用不當...任何人都可以用我的代碼來幫助我。如何在ajax中正確使用forloop

我的問題是在ajax腳本代碼.... update.php是好的沒有錯誤,它完美的作品。

腳本代碼:

<script type="text/javascript"> 
$(document).ready(function() { 
     $('#updates').click(function (e) { 

      e.preventDefault(); 


      var $region = $('#t_region').val(); 
      var $town = $('#t_town').val(); 
      var $uniq_id = $('#t_uniq_id').val(); 
      var $position = $('#t_position').val(); 
      var $salary_grade = $('#t_salary_grade').val(); 
      var $salary = $('#t_salary').val(); 

    for($n=1;$n<=15;$n++) { 
      var $id = $('#id'.$n).val(); 
      var $aic = $('#aic'.$n).val(); 
      var $name = $('#name'.$n).val(); 
      var $optA = $('#optA'.$n).val(); 
      var $optB = $('#optB'.$n).val(); 
      var $optC = $('#optC'.$n).val(); 
      var $optD = $('#optD'.$n).val(); 
      var $other_qual = $('#other_qual'.$n).val(); 
      var $interview = $('#interview'.$n).val(); 
      var $total = $('#total'.$n).val(); 


    } 

      $.ajax({ 
       type: "POST", 
       url: "update.php", 
       data: { 
        region_text: $region, 
        town_text: $town, 
        uniq_id_text: $uniq_id, 
        position_text: $position, 
        salary_grade_text: $salary_grade, 
        salary_text: $salary, 

       for($x=1;$x<=15;$x++) { 

        id'.$x.'_text: $id, 
        aic'.$x.'_text: $aic, 
        name'.$x.'_text: $name, 
        optA'.$x.'_text: $optA, 
        optB'.$x.'_text: $optB, 
        optC'.$x.'_text: $optC, 
        optD'.$x.'_text: $optD, 
        other_qual'.$x.'_text: $other_qual, 
        interview'.$x.'_text: $interview, 
        total'.$x.'_text: $total, 
       }}, 
       cache: false, 
       success: function (data) { 


       } 

      }); 

     }); 
    }); 
</script> 

update.php代碼:

<?php 
    include('../connection.php'); 
    date_default_timezone_set('Asia/Manila'); 

    $region  = @$_POST['region_text']; 
    $town   = @$_POST['town_text']; 
    $uniq_id  = @$_POST['uniq_id_text']; 
    $position  = @$_POST['position_text']; 
    $salary_grade = @$_POST['salary_grade_text']; 
    $salary  = @$_POST['salary_text']; 

$dupesql = "SELECT * FROM afnup_worksheet WHERE funiq_id = '$uniq_id'"; 
$duperow = mysql_query($dupesql); 
if(mysql_num_rows($duperow) > 0){ 
    exit; 
}else{ 

    for($n=1;$n<=15;$n++) { 


    $id   = @$_POST['id'.$n.'_text']; 
    $aic   = @$_POST['aic'.$n.'_text']; 
    $name   = @$_POST['name'.$n.'_text']; 
    $optA   = @$_POST['optA'.$n.'_text']; 
    $optB   = @$_POST['optB'.$n.'_text']; 
    $optC   = @$_POST['optC'.$n.'_text']; 
    $optD   = @$_POST['optD'.$n.'_text']; 
    $other_qual = @$_POST['other_qual'.$n.'_text']; 
    $interview = @$_POST['interview'.$n.'_text']; 
    $total  = @$_POST['total'.$n.'_text']; 



if(!empty($name)){ 
$query = "INSERT INTO afnup_worksheet (faic,fregion,ftown,funiq_id,fposition,fsalary_grade,fsalary,fnl_name,edu_attain,experience,seminars,eligibility,other_qual,interview,ftotal,dateinputed) 
VALUES 
('$aic','$region','$town','$uniq_id','$position','$salary_grade','$salary','$name','$optA','$optB','$optC','$optD','$other_qual','$interview','$total',CURRENT_TIMESTAMP)"; 
$resource = mysql_query($query) or die(mysql_error()); 
     } 
    } 
} 
?> 
+1

您不能執行代碼來生成一個對象內聯,你應該如果你想像那樣使用它,在將它傳遞給'ajax'函數之前準備對象。 – gpgekko

回答

1

這應該工作:

<script type="text/javascript"> 
$(document).ready(function() { 
    $('#updates').click(function (e) { 
     e.preventDefault(); 
     var data = {}; 
     data.region_text = $('#t_region').val(); 
     data.town_text = $('#t_town').val(); 
     data.uniq_id_text = $('#t_uniq_id').val(); 
     data.position_text = $('#t_position').val(); 
     data.salary_grade_text = $('#t_salary_grade').val(); 
     data.salary_text = $('#t_salary').val(); 

     for(var $x=1;$x<=15;$x++) { 
      data['id'+$x+'_text'] = $('#id'+$x).val(); 
      data['aic'+$x+'_text'] = $('#aic'+$x).val(); 
      data['name'+$x+'_text'] = $('#name'+$x).val(); 
      data['optA'+$x+'_text'] = $('#optA'+$x).val(); 
      data['optB'+$x+'_text'] = $('#optB'+$x).val(); 
      data['optC'+$x+'_text'] = $('#optC'+$x).val(); 
      data['optD'+$x+'_text'] = $('#optD'+$x).val(); 
      data['other_qual'+$x+'_text'] = $('#other_qual'+$x).val(); 
      data['interview'+$x+'_text'] = $('#interview'+$x).val(); 
      data['total'+$x+'_text'] = $('#total'+$x).val(); 
     } 

     $.ajax({ 
      type: "POST", 
      url: "update.php", 
      data: data, 
      cache: false, 
      success: function (response) { 
      // We are using response to distinguish our outer data variable here from the response 
      } 
     }); 
    }); 
}); 
</script> 
+0

我使用ur代碼,但它有錯誤 – user3340637

+0

錯誤究竟是什麼(看看瀏覽器控制檯)? –

+0

好的,我發現了什麼問題,並更新了代碼。它應該現在工作.. –