推送到数组后,使用ajax传递两个变量

时间:2018-11-07 07:34:22

标签: php jquery ajax

我正在尝试使此代码正常工作,但是当我用ajax传递两个变量时,我看起来做错了,请帮助,这是代码,基本上是在框之间移动项目的拖放代码并在每个盒子内整理。

<script type="text/javascript">
  $(document).ready(function() {

    // Example 1.3: Sortable and connectable lists with visual helper
    $('#sortable-div .sortable-list').sortable({
      connectWith: '#sortable-div .sortable-list',
      placeholder: 'placeholder',
      delay: 150,
      stop: function() {
        var selectedData = new Array();
        $('.sortable-list>li').each(function() {
          selectedData.push([$(this).attr("id"), $(this).attr("pagenum")])

        });
        updateOrder(selectedData);
      }

    });
  });

  function updateOrder(data) {
    $.ajax({
      url: "ajaxPro.php",
      type: 'post',
      data: {
        position: data,
        page: data
      },
      success: function() {
        /* alert('your change successfully saved');*/
      }
    })
  }
</script>

3 个答案:

答案 0 :(得分:0)

您可以使用以下方式实现:

$('.spremiUredivanje').click(function(){

    ai = document.getElementById('ai_mjerila_edit').value;
    broj_mjerila = document.getElementById('broj_mjerila_edit').value;
    adresa = document.getElementById('adresa_edit').value;
    stanje = document.getElementById('stanje_edit').value;
    $( "#datum_edit" ).datepicker( "option", "dateFormat", "yy-mm-dd" ); 
    datum =  document.getElementById('datum_edit').value;
    operater = document.getElementById('operater_edit').value;

    $.ajax({
        url: 'test.php',
        type: 'POST',
        data: {
            post1: broj_mjerila, post2: adresa, post3:stanje, post4: datum, post5: operater, post6:ai
        },
        beforeSend: function(){
            $('#loaderEdit').show();
        },

因此您的php文件应如下所示:

$broj_mjerila = $_POST['post1'];
$adresa = $_POST['post2'];
$stanje = $_POST['post3'];
$datum = $_POST['post4'];
$operater = $_POST['post5'];
$ai = $_POST['post6'];

要控制您的响应,请在ajax调用中使用success

success: function(response) {
            if(response == 1) {
                //alert("success!");
                $('#editUspjesan').show();
                $('#loaderEdit').hide();...

答案 1 :(得分:0)

您能否清楚说明您的要求。您是否要在2个单独的数组中获取页码和ID?

// If page no and id need to be in separate array
var position = [], pages = [];
$('.sortable-list>li').each(function() {
     position.push($(this).attr("id"));
     pages.push($(this).attr("pagenum"));
});
updateOrder(position, pages);

function updateOrder(position, page) {
    $.ajax({
      url: "ajaxPro.php",
      type: 'post',
      data: {
        position: position,
        page: page
      },
      success: function() {
        /* alert('your change successfully saved');*/
      }
    })
  }


// If page no and id can be combined use objects
var selectedData = [];
$('.sortable-list>li').each(function() {
    selectedData.push({id: $(this).attr("id"), page: $(this).attr("pagenum")})
});
function updateOrder(data) {
    $.ajax({
        url: "ajaxPro.php",
        type: 'post',
        data: {
        position: data
    },
    success: function() {
        /* alert('your change successfully saved');*/
    }
  })
}

答案 2 :(得分:0)

<script type="text/javascript">
$(document).ready(function() {

// Example 1.3: Sortable and connectable lists with visual helper
$('#sortable-div .sortable-list').sortable({
  connectWith: '#sortable-div .sortable-list',
  placeholder: 'placeholder',
  delay: 150,
  stop: function() {
    var selectedData = new Array();
    $('.sortable-list>li').each(function() {
      selectedData.push([$(this).attr("id"), $(this).attr("pagenum")])

    });
    updateOrder(selectedData);
  }

});
});

function updateOrder(data) {
$.ajax({
  url: "ajaxPro.php",
  type: 'post',
  data: {
    position: data.id,
    page: data.pagenum
  },
dataType:'json',
  success: function() {
    /* alert('your change successfully saved');*/
  }
})
}
</script>

让我们尝试一下