如何通过ajax $ .post方法将多个值发布到php脚本?

时间:2013-09-15 18:41:10

标签: php jquery ajax

我在每个图库中都有一个图像按钮。我想在点击图像按钮后将a,b,c,d,e,f,g的值发布到php脚本。如果php脚本成功地将a,b,c,d,e,f,g插入到mysql中,我想更改图像按钮,以便用户知道数据输入成功!

任何人都可以告诉我如何使用多个值制作这样的ajax post方法,以及如果ajax post request成功,如何更改图像按钮?在我的PHP脚本中应该添加什么,以便ajax知道数据是否已成功插入到mysql db?

<html>

<head>

<script>
function postLike(a,b,c,d,e,f,g) {

//after sussceffuly insert to mysql i want to change image button to this  
    $('#like12').html(" <img class='lb-liked' onclick='deleteLike(&quot;" + b + "&quot;)' src='/liked.png' title='like' border='0' />");  


    }

</script>
</head>

<body>

<div id="like12" style="float: left; padding: 10px 2px 2px;"> 
<img class="liker" onclick='postLike("http://somesite.com/12345.jpg","stacy","http://somesite.com/season_456565656.jpg","123456789","http://somesite.com/abddef/","98765432","cool season")' src="./like.png" border="0">
</div>


</html>

2 个答案:

答案 0 :(得分:1)

有很多种方法可以通过HTTP发布多个值,所以它确实取决于。

// post as array
$.post(url, "val[]=" + a + "&val[]=" + b
// post as separate values
$.post(url, "val1=" + a + "&val2=" + b
// post as JSON string
$.post(url, "values=" + JSON.stringify([a,b,c,d,e,f])

答案 1 :(得分:0)

<script>
$(document).ready(function(){
$('#button').click(function(){
$.post(url,{val1: a, val2: b},function(data){
// success
alert("yay");
}
}
}
</script>
<div id="button" style="background-color:#000;width:30px;height:30px;"></div>