dpgbh20688 2014-05-26 22:46
浏览 78
已采纳

我如何制作一个简单的AJAX请求来执行PHP?

Hey guys this is a problem kind of specific to what i'm trying to do on my site and i cant find an answer so i just throught i'd make my own question

What i'm trying to do:

When i press a button on my site it executes a javascript function. Within this JS function i create an array. The order of this array is key to the functionality of the AJAX request because the whole point is to be able to reorder the array to your liking.

The contents of this array represent mySQL rows.

I would like to be able to execute a php function upon button press which will take this array, and set values to the "Order" column of my table. ie: 1st element of the array gets its "order" value set to one. Last element of the array gets its "order" value set to N(however many there are).

Here is my current javascript:

<script>
  $(document).ready(function(){
    $( "#sortable" ).sortable({axis:"y"});
    $("button").click(function(){
     var sortedIDs = $( "#sortable" ).sortable("toArray");
      var num = sortedIDs[0];
    alert(num);
    $.post({
    url:"ajax2.php",

    });

  });
});
</script>

Here is what my ajax2.php page looks like:

<?php
include 'connect.php';
  $sql = "UPDATE categories
      SET cat_order=1
      WHERE cat_id=5;";

$result = mysql_query($sql);

?>

I just put in numbers to test the functionality. In the end i would like to be able to use the information set in the javascript array "sortedIDs".

To make it clearer what i want to do in general....I'm creating a web forum and i would like to change the order of my forum categories on the main page. I have created a "sortable" jquery ui interface in order to do this. When you press the button on this interface it executes the js function i posted. It knows the order of the list of categories. And it knows the order after you change it.

  • 写回答

2条回答 默认 最新

  • dongshijiao6890 2014-05-27 00:02
    关注

    This maybe a little old school but I like using my own xmlHTTP request rather than one provided by jQuery. Here is a sample of what I think may help you solve your problem.

    HTML and Javascript:

    <!doctype html>
    <html>
    <head>
    <meta charset="utf-8">
    <title>Untitled Document</title>
    <script>
    // JavaScript Document
    
    function xmlHttp()
    {
        this.strHTML;
        this.objXML;
        this.postReturn = function(post, page){
            if (window.XMLHttpRequest){// code for IE7+, Firefox, Chrome, Opera, Safari
                xmlhttp=new XMLHttpRequest();
            }else{// code for IE6, IE5
                xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
            }
            xmlhttp.onreadystatechange=function(){
                if (xmlhttp.readyState==4 && xmlhttp.status==200){
                    this.strHTML = xmlhttp.responseText;
                    this.objXML = xmlhttp.responseXML;
                    pageReturn(this.strHTML, this.objXML);
                }
            }
            xmlhttp.open("POST",page,false);
            xmlhttp.setRequestHeader("Content-type","application/x-www-form-urlencoded");
            xmlhttp.send(post);
        }
    }
    
    function pageReturn(strHTML, objXML){
        alert(strHTML);
    }
    
    function sampleSend(){
        var strPost="";
    
        //dont forget to encodeURIComponent() when you use this for production
        for(var i=0;i<10;i++){
            strPost += (!strPost)?"blah"+i+"=value"+i:"&blah"+i+"=value"+i;
        }
        var xmlhttp = new xmlHttp();
        xmlhttp.postReturn(strPost, "test.php");
    }
    </script>
    </head>
    
    <body>
    <div onclick="sampleSend()">clickMe</div>
    </body>
    </html>
    

    PHP test.php in this sample

    <?php
    foreach($_POST as $k => $v){
        $sql = "insert into tableName (someField) values ('".$v."');";
    
        //this is uncommented to alert what is happening
        echo $sql . "
    ";
        //mysql stuff here
        //this is just an example of looping the $_POST
    }
    echo "done";
    ?>
    

    Hope this helps!

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥30 Unity接入微信SDK 无法开启摄像头
  • ¥20 有偿 写代码 要用特定的软件anaconda 里的jvpyter 用python3写
  • ¥20 cad图纸,chx-3六轴码垛机器人
  • ¥15 移动摄像头专网需要解vlan
  • ¥20 access多表提取相同字段数据并合并
  • ¥20 基于MSP430f5529的MPU6050驱动,求出欧拉角
  • ¥20 Java-Oj-桌布的计算
  • ¥15 powerbuilder中的datawindow数据整合到新的DataWindow
  • ¥20 有人知道这种图怎么画吗?
  • ¥15 pyqt6如何引用qrc文件加载里面的的资源