[英]How do i make a simple AJAX request to execute 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. 当我按我网站上的按钮时,它将执行javascript函数。 Within this JS function i create an array.
在此JS函数中,我创建了一个数组。 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.
该数组的顺序是AJAX请求功能的关键,因为重点在于能够根据自己的喜好对数组进行重新排序。
The contents of this array represent mySQL rows. 该数组的内容表示mySQL行。
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. 我希望能够在按下按钮时执行php函数,该函数将采用此数组,并将值设置为表的“ Order”列。 ie: 1st element of the array gets its "order" value set to one.
即:数组的第一个元素的“ order”值设置为1。 Last element of the array gets its "order" value set to N(however many there are).
数组的最后一个元素的“ order”值设置为N(但是有很多)。
Here is my current javascript: 这是我目前的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: 这是我的ajax2.php页面的样子:
<?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".
最后,我希望能够使用javascript数组“ 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.
为了做到这一点,我创建了一个“可排序的” jquery ui接口。 When you press the button on this interface it executes the js function i posted.
当您按下该界面上的按钮时,它将执行我发布的js函数。 It knows the order of the list of categories.
它知道类别列表的顺序。 And it knows the order after you change it.
更改顺序后,它就会知道顺序。
This maybe a little old school but I like using my own xmlHTTP request rather than one provided by jQuery. 这可能有点老了,但是我喜欢使用自己的xmlHTTP请求而不是jQuery提供的请求。 Here is a sample of what I think may help you solve your problem.
以下是我认为可以帮助您解决问题的示例。
HTML and Javascript: HTML和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
test.php
<?php
foreach($_POST as $k => $v){
$sql = "insert into tableName (someField) values ('".$v."');";
//this is uncommented to alert what is happening
echo $sql . "\n";
//mysql stuff here
//this is just an example of looping the $_POST
}
echo "done";
?>
Hope this helps! 希望这可以帮助!
Here`sa sample ajax code based on jquery. 这是一个基于jquery的示例ajax代码。
$.ajax({
url : "path_to_ajax2.php",
type : "POST",
data : ({sorted_ids:num}),
success : function(data){
//show the sorted array here.
}
});
and don`t forget to echo something if your update is successful at ajax2.php which returns control to the ajax calling function. 并且不要忘记在成功更新ajax2.php并将其控制权返回给ajax调用函数的情况下回显某些内容。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.