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条)

报告相同问题?

悬赏问题

  • ¥15 目前主流的音乐软件,像网易云音乐,QQ音乐他们的前端和后台部分是用的什么技术实现的?求解!
  • ¥60 pb数据库修改与连接
  • ¥15 spss统计中二分类变量和有序变量的相关性分析可以用kendall相关分析吗?
  • ¥15 拟通过pc下指令到安卓系统,如果追求响应速度,尽可能无延迟,是不是用安卓模拟器会优于实体的安卓手机?如果是,可以快多少毫秒?
  • ¥20 神经网络Sequential name=sequential, built=False
  • ¥16 Qphython 用xlrd读取excel报错
  • ¥15 单片机学习顺序问题!!
  • ¥15 ikuai客户端多拨vpn,重启总是有个别重拨不上
  • ¥20 关于#anlogic#sdram#的问题,如何解决?(关键词-performance)
  • ¥15 相敏解调 matlab