php
  • jquery
  • ajax
  • 2016-04-14 195 views 0 likes 
    0

    我很努力地將一個叫做$numbers的數組傳遞給$.ajax,就像是1,2,4那樣。它沒有被識別,我不知道如何在下一頁獲取這些信息。這是我的代碼:

    <?php 
        // $numbers = 1,2,4; 
         print 
         "<div class='col-lg-6 col-md-6 col-sm-6 col-xs-12 col-md-pull-6 col-sm-pull-6'>". 
             "<div class='form-group'>". 
              "<div class='col-md-12'><strong>Forename:</strong></div>". 
              "<div class='col-md-12'><input type='text' class='form-control' id='forename'></div>". 
             "</div>". 
             "<div class='form-group'>". 
              "<div class='col-md-12'><strong>Surname:</strong></div>". 
              "<div class='col-md-12'><input type='text' class='form-control' id='surname'></div>". 
             "</div>". 
              "<div class='form-group'>". 
               "<div class='col-xs-12 col-md-6'>". 
                "<button id='button' class='btn btn-success btn_add'>Add</button>". 
               "</div>". 
              "</div>". 
             "</div>". 
         "</div>"; 
    
         } 
    
    ?> 
    
    <script type="text/javascript">  
        $(document).on("click", 'button.btn_add', function(){ 
         var forename = document.getElementById('forename').value; 
         var surname = document.getElementById('surname').value; 
         var number = <?php echo $numbers; ?>; 
         if((forename == null || forename == "") && (surname == null || surname == "")){ 
          alert("Please fill in all fields"); 
         } 
    else { 
        $.ajax({ 
         url: adduserinfo.php, 
         method: 'post', 
         dataType: 'json', 
         data: 'forename=' + forename + '&surname=' + surname + '&numbers=' + number 
        }); 
    
    </script> 
    
    +0

    '&號碼=」 +數字「缺少等號,如果它真的是一條條ng – devpro

    +0

    仍然不工作夥伴 –

    +0

    '$ numbers =「1,2,4」;'也chk yur控制檯 – devpro

    回答

    0

    您需要傳遞對象而不是查詢字符串。

    使用

    如果$號碼被陣列然後使用

    var number = <?php echo json_encode($numbers); ?>; // cant echo array 
    

    如果$號碼被串接着使用

    var number = "<?php echo $numbers; ?>"; // cant echo array 
    

    AJAX調用

    $.ajax({ 
        url: adduserinfo.php, 
        method: 'POST', 
        dataType: 'json', 
        data: { forename : forename, surname : surname, numbers : number } 
    }); 
    
    相關問題