передать две переменные с помощью ajax после отправки в массив - PullRequest
0 голосов
/ 07 ноября 2018

Я пытаюсь заставить этот код работать, но, похоже, я делаю что-то не так, когда передаю две переменные с помощью 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 голосов
/ 07 ноября 2018

Можете ли вы объяснить ваши требования ясно. Вы пытаетесь получить номер страницы и идентификатор в 2 отдельных массива?

// 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');*/
    }
  })
}
0 голосов
/ 07 ноября 2018
<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>

Давайте попробуем это

0 голосов
/ 07 ноября 2018

Вы можете сделать это следующим образом:

$('.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'];

Для контроля вашего ответа используйте success внутри ajax-вызова:

success: function(response) {
            if(response == 1) {
                //alert("success!");
                $('#editUspjesan').show();
                $('#loaderEdit').hide();...
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...