Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/281.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 如何使用AJAX发布和更新jQuery序列化_Php_Jquery_Ajax_Post_Jquery Ui Sortable - Fatal编程技术网

Php 如何使用AJAX发布和更新jQuery序列化

Php 如何使用AJAX发布和更新jQuery序列化,php,jquery,ajax,post,jquery-ui-sortable,Php,Jquery,Ajax,Post,Jquery Ui Sortable,请注意,我已经通过下面的StackOverflow问题,这正是我需要的,但不幸的是,它没有张贴 我的代码- Test.php <script src="https://code.jquery.com/jquery-3.5.1.min.js"></script> <script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script> &

请注意,我已经通过下面的StackOverflow问题,这正是我需要的,但不幸的是,它没有张贴

我的代码-

Test.php

<script src="https://code.jquery.com/jquery-3.5.1.min.js"></script>
<script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>
  <script>
  $( function() {
    $( "#sortable" ).sortable();
    $( "#sortable" ).disableSelection();
  } );
  </script>

<ul id="sortable">
    <li id="item-1">Item 1</li>
    <li id="item-2">Item 2</li>
</ul>
Span 1: <span id="span"></span>
Span2: <span id="span2"></span>

<script>
$(document).ready(function () {
    $('ul').sortable({
        axis: 'y',
        stop: function (event, ui) {
            var data = $(this).sortable('serialize');
            $('#span2').text(data);
            $.ajax({
                data: data,
                type: 'POST',
                url: 'test2.php',
                success: function(data){ 
                    $("#span").text(data); 
                }
            });
    }
    });
});
</script>
<?php 
 include('../db.php'); 

$date = $_POST["data"];

$conn = new mysqli ($servername, $dbusername, $dbpassword, $dbname);
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}

$sql = "UPDATE table SET something = '$something'
            WHERE id = '$id'";
    
    if(mysqli_query($conn, $sql)){
        
        echo $date;
    }
    
$conn->close();

?>
<script src="https://code.jquery.com/jquery-3.5.1.min.js"></script>
<script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>
  <script>
  $( function() {
    $( "#sortable" ).sortable();
    $( "#sortable" ).disableSelection();
  } );
  </script>

<ul id="sortable">
    
<?php
    $q = ' SELECT * FROM temp';
            $result = mysqli_query($conn, $q);
            if ($result->num_rows > 0) {
            while( $items = $result->fetch_assoc() ){
?>
    
    <li id='sort_<?php echo $items['id'] ?>'>
        <ul style="display:inline-block;"><?php echo $items['name'] ?></ul>
        <ul style="display:inline-block;"><?php echo $items['name'] ?></ul>
    </li>
    
<?php
            }
         }
      ?>
    
</ul>


<script>
$(document).ready(function () {
    $('#sortable').sortable({
      opacity: 0.325,
      tolerance: 'pointer',
      cursor: 'move',
      update: function(event, ui) {
         var post = $(this).sortable('serialize');

         $.ajax({
            type: 'POST',
            url: 'test2.php',
            data: post,
            dataType: 'json',
            cache: false,
            success: function(output) {
               console.log('success -> ' + output);
            },
            error: function(output) {
               console.log('fail -> ' + output);
            }
         });

      }
   });
});
  $('#sortable').disableSelection();
</script>
<?php 
$isNum = false;

foreach( $_POST['sort'] as $key => $value ) {
    if ( ctype_digit($value) ) {
        $isNum = true;
    } else {
        $isNum = false;
    }
}

if( isset($_POST) && $isNum == true ){
   $orderArr = $_POST['sort'];
    $order = 0;
    if ($stmt = $conn->prepare(" UPDATE temp SET o_id = ? WHERE id=? ")) {
        foreach ( $orderArr as $item) {
            $stmt->bind_param("ii", $order, $item);
            $stmt->execute();
            $order++;
        }
        $stmt->close();
    }
    echo json_encode(  $orderArr );
    $conn->close();
}

?>

$(函数(){
$(“#可排序”).sortable();
$(“#可排序”).disableSelection();
} );
    第1项 第2项
跨度1: Span2: $(文档).ready(函数(){ $('ul')。可排序({ 轴:‘y’, 停止:功能(事件、用户界面){ var data=$(this.sortable('serialize'); $('#span2')。文本(数据); $.ajax({ 数据:数据, 键入:“POST”, url:'test2.php', 成功:函数(数据){ $(“#span”)。文本(数据); } }); } }); });
Test2.php

<script src="https://code.jquery.com/jquery-3.5.1.min.js"></script>
<script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>
  <script>
  $( function() {
    $( "#sortable" ).sortable();
    $( "#sortable" ).disableSelection();
  } );
  </script>

<ul id="sortable">
    <li id="item-1">Item 1</li>
    <li id="item-2">Item 2</li>
</ul>
Span 1: <span id="span"></span>
Span2: <span id="span2"></span>

<script>
$(document).ready(function () {
    $('ul').sortable({
        axis: 'y',
        stop: function (event, ui) {
            var data = $(this).sortable('serialize');
            $('#span2').text(data);
            $.ajax({
                data: data,
                type: 'POST',
                url: 'test2.php',
                success: function(data){ 
                    $("#span").text(data); 
                }
            });
    }
    });
});
</script>
<?php 
 include('../db.php'); 

$date = $_POST["data"];

$conn = new mysqli ($servername, $dbusername, $dbpassword, $dbname);
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}

$sql = "UPDATE table SET something = '$something'
            WHERE id = '$id'";
    
    if(mysqli_query($conn, $sql)){
        
        echo $date;
    }
    
$conn->close();

?>
<script src="https://code.jquery.com/jquery-3.5.1.min.js"></script>
<script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>
  <script>
  $( function() {
    $( "#sortable" ).sortable();
    $( "#sortable" ).disableSelection();
  } );
  </script>

<ul id="sortable">
    
<?php
    $q = ' SELECT * FROM temp';
            $result = mysqli_query($conn, $q);
            if ($result->num_rows > 0) {
            while( $items = $result->fetch_assoc() ){
?>
    
    <li id='sort_<?php echo $items['id'] ?>'>
        <ul style="display:inline-block;"><?php echo $items['name'] ?></ul>
        <ul style="display:inline-block;"><?php echo $items['name'] ?></ul>
    </li>
    
<?php
            }
         }
      ?>
    
</ul>


<script>
$(document).ready(function () {
    $('#sortable').sortable({
      opacity: 0.325,
      tolerance: 'pointer',
      cursor: 'move',
      update: function(event, ui) {
         var post = $(this).sortable('serialize');

         $.ajax({
            type: 'POST',
            url: 'test2.php',
            data: post,
            dataType: 'json',
            cache: false,
            success: function(output) {
               console.log('success -> ' + output);
            },
            error: function(output) {
               console.log('fail -> ' + output);
            }
         });

      }
   });
});
  $('#sortable').disableSelection();
</script>
<?php 
$isNum = false;

foreach( $_POST['sort'] as $key => $value ) {
    if ( ctype_digit($value) ) {
        $isNum = true;
    } else {
        $isNum = false;
    }
}

if( isset($_POST) && $isNum == true ){
   $orderArr = $_POST['sort'];
    $order = 0;
    if ($stmt = $conn->prepare(" UPDATE temp SET o_id = ? WHERE id=? ")) {
        foreach ( $orderArr as $item) {
            $stmt->bind_param("ii", $order, $item);
            $stmt->execute();
            $order++;
        }
        $stmt->close();
    }
    echo json_encode(  $orderArr );
    $conn->close();
}

?>

对于遇到相同问题的人,以下是解决方案:

Test.php

<script src="https://code.jquery.com/jquery-3.5.1.min.js"></script>
<script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>
  <script>
  $( function() {
    $( "#sortable" ).sortable();
    $( "#sortable" ).disableSelection();
  } );
  </script>

<ul id="sortable">
    <li id="item-1">Item 1</li>
    <li id="item-2">Item 2</li>
</ul>
Span 1: <span id="span"></span>
Span2: <span id="span2"></span>

<script>
$(document).ready(function () {
    $('ul').sortable({
        axis: 'y',
        stop: function (event, ui) {
            var data = $(this).sortable('serialize');
            $('#span2').text(data);
            $.ajax({
                data: data,
                type: 'POST',
                url: 'test2.php',
                success: function(data){ 
                    $("#span").text(data); 
                }
            });
    }
    });
});
</script>
<?php 
 include('../db.php'); 

$date = $_POST["data"];

$conn = new mysqli ($servername, $dbusername, $dbpassword, $dbname);
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}

$sql = "UPDATE table SET something = '$something'
            WHERE id = '$id'";
    
    if(mysqli_query($conn, $sql)){
        
        echo $date;
    }
    
$conn->close();

?>
<script src="https://code.jquery.com/jquery-3.5.1.min.js"></script>
<script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>
  <script>
  $( function() {
    $( "#sortable" ).sortable();
    $( "#sortable" ).disableSelection();
  } );
  </script>

<ul id="sortable">
    
<?php
    $q = ' SELECT * FROM temp';
            $result = mysqli_query($conn, $q);
            if ($result->num_rows > 0) {
            while( $items = $result->fetch_assoc() ){
?>
    
    <li id='sort_<?php echo $items['id'] ?>'>
        <ul style="display:inline-block;"><?php echo $items['name'] ?></ul>
        <ul style="display:inline-block;"><?php echo $items['name'] ?></ul>
    </li>
    
<?php
            }
         }
      ?>
    
</ul>


<script>
$(document).ready(function () {
    $('#sortable').sortable({
      opacity: 0.325,
      tolerance: 'pointer',
      cursor: 'move',
      update: function(event, ui) {
         var post = $(this).sortable('serialize');

         $.ajax({
            type: 'POST',
            url: 'test2.php',
            data: post,
            dataType: 'json',
            cache: false,
            success: function(output) {
               console.log('success -> ' + output);
            },
            error: function(output) {
               console.log('fail -> ' + output);
            }
         });

      }
   });
});
  $('#sortable').disableSelection();
</script>
<?php 
$isNum = false;

foreach( $_POST['sort'] as $key => $value ) {
    if ( ctype_digit($value) ) {
        $isNum = true;
    } else {
        $isNum = false;
    }
}

if( isset($_POST) && $isNum == true ){
   $orderArr = $_POST['sort'];
    $order = 0;
    if ($stmt = $conn->prepare(" UPDATE temp SET o_id = ? WHERE id=? ")) {
        foreach ( $orderArr as $item) {
            $stmt->bind_param("ii", $order, $item);
            $stmt->execute();
            $order++;
        }
        $stmt->close();
    }
    echo json_encode(  $orderArr );
    $conn->close();
}

?>

$(函数(){
$(“#可排序”).sortable();
$(“#可排序”).disableSelection();
} );
    $(文档).ready(函数(){ $(“#可排序”)。可排序({ 不透明度:0.325, 公差:“指针”, 光标:“移动”, 更新:功能(事件、用户界面){ var post=$(this).sortable('serialize'); $.ajax({ 键入:“POST”, url:'test2.php', 资料来源:波斯特, 数据类型:“json”, cache:false, 成功:功能(输出){ console.log('success->'+输出); }, 错误:函数(输出){ console.log('fail->'+输出); } }); } }); }); $(“#可排序”).disableSelection();
    Test2.php

    <script src="https://code.jquery.com/jquery-3.5.1.min.js"></script>
    <script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>
      <script>
      $( function() {
        $( "#sortable" ).sortable();
        $( "#sortable" ).disableSelection();
      } );
      </script>
    
    <ul id="sortable">
        <li id="item-1">Item 1</li>
        <li id="item-2">Item 2</li>
    </ul>
    Span 1: <span id="span"></span>
    Span2: <span id="span2"></span>
    
    <script>
    $(document).ready(function () {
        $('ul').sortable({
            axis: 'y',
            stop: function (event, ui) {
                var data = $(this).sortable('serialize');
                $('#span2').text(data);
                $.ajax({
                    data: data,
                    type: 'POST',
                    url: 'test2.php',
                    success: function(data){ 
                        $("#span").text(data); 
                    }
                });
        }
        });
    });
    </script>
    
    <?php 
     include('../db.php'); 
    
    $date = $_POST["data"];
    
    $conn = new mysqli ($servername, $dbusername, $dbpassword, $dbname);
    if ($conn->connect_error) {
        die("Connection failed: " . $conn->connect_error);
    }
    
    $sql = "UPDATE table SET something = '$something'
                WHERE id = '$id'";
        
        if(mysqli_query($conn, $sql)){
            
            echo $date;
        }
        
    $conn->close();
    
    ?>
    
    <script src="https://code.jquery.com/jquery-3.5.1.min.js"></script>
    <script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>
      <script>
      $( function() {
        $( "#sortable" ).sortable();
        $( "#sortable" ).disableSelection();
      } );
      </script>
    
    <ul id="sortable">
        
    <?php
        $q = ' SELECT * FROM temp';
                $result = mysqli_query($conn, $q);
                if ($result->num_rows > 0) {
                while( $items = $result->fetch_assoc() ){
    ?>
        
        <li id='sort_<?php echo $items['id'] ?>'>
            <ul style="display:inline-block;"><?php echo $items['name'] ?></ul>
            <ul style="display:inline-block;"><?php echo $items['name'] ?></ul>
        </li>
        
    <?php
                }
             }
          ?>
        
    </ul>
    
    
    <script>
    $(document).ready(function () {
        $('#sortable').sortable({
          opacity: 0.325,
          tolerance: 'pointer',
          cursor: 'move',
          update: function(event, ui) {
             var post = $(this).sortable('serialize');
    
             $.ajax({
                type: 'POST',
                url: 'test2.php',
                data: post,
                dataType: 'json',
                cache: false,
                success: function(output) {
                   console.log('success -> ' + output);
                },
                error: function(output) {
                   console.log('fail -> ' + output);
                }
             });
    
          }
       });
    });
      $('#sortable').disableSelection();
    </script>
    
    <?php 
    $isNum = false;
    
    foreach( $_POST['sort'] as $key => $value ) {
        if ( ctype_digit($value) ) {
            $isNum = true;
        } else {
            $isNum = false;
        }
    }
    
    if( isset($_POST) && $isNum == true ){
       $orderArr = $_POST['sort'];
        $order = 0;
        if ($stmt = $conn->prepare(" UPDATE temp SET o_id = ? WHERE id=? ")) {
            foreach ( $orderArr as $item) {
                $stmt->bind_param("ii", $order, $item);
                $stmt->execute();
                $order++;
            }
            $stmt->close();
        }
        echo json_encode(  $orderArr );
        $conn->close();
    }
    
    ?>
    
    
    

    希望它能帮助有需要的人。

    首先用try-and-catch包装您的代码,这样会显示错误,还可以在ajax中添加错误函数,这将在ajax中捕获错误。其次,让ajax调用排序停止函数是一个糟糕的想法:您对参数化的预处理语句非常开放,应该使用参数化的预处理语句,而不是手动构建查询。它们由或提供。永远不要相信任何形式的输入!即使您的查询仅由受信任的用户执行。