如何使用jqueryajax方法发送数据
我是ajax新手,想知道如何使用jQueryAjax方法发送数据,非常感谢您的帮助。 这是我的代码:如何使用jqueryajax方法发送数据,jquery,ajax,Jquery,Ajax,我是ajax新手,想知道如何使用jQueryAjax方法发送数据,非常感谢您的帮助。 这是我的代码: <head> <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"> </script> <script> function show(str){ $(document).ready(fu
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"> </script>
<script>
function show(str){
$(document).ready(function(){
$("#games").change(function(){
valcc = $("#games").val();
$("#div1").load("gg.php");
});
});
}
</script>
</head>
<body>
<select id="games" name="games" onchange="show(this.value)">
<option value="cricket">cricket</option>
<option value="soccer">soccer</option>
<option value="chess">chess</option>
</select>
<input type="button" name="button" value="button" id="button" />
<div id="dd">Please select a Game</div>
<div id="div1" style="width:300px; height:200px; border:1px solid #999;"></div>
功能显示(str){
$(文档).ready(函数(){
$(“#游戏”).更改(函数(){
valcc=$(“#游戏”).val();
$(“#div1”).load(“gg.php”);
});
});
}
板球
足球
国际象棋
请选择一个游戏
我需要将select选项的值发送到gg.php页面,然后处理它。
请帮助在更改选定输入的时调用此函数
function show(str)
{
$.ajax({
type:'post', // the type of request POST,GET etc
url:'gg.php', // url to which request is send
datatype:'html', // datatype like html,text,json etc
data:'games='+str, // pass the data; if there are multiple parameters you have to append it like data:'param1='+val1+'¶m2='+val2 etc
success:function(response) // on success get response
{
}
});
}
现在,您可以在gg.php中处理通过ajax传递的数据。当您通过POST
传递数据时,必须按如下方式访问值:
$value=$_POST['games']; // index as the parameter name passed through ajax
注意:无论您在gg.php中回显什么,都将作为对ajax函数的响应发送
比如说,
在ajax响应中,警告响应
function show(str)
{
.............
success:function(response) // on success get response
{
alert(response);
}
}
现在尝试在gg.php中重复游戏的价值
<?php
echo $value=$_POST['games'];
exit;
?>
现在您可以清楚地了解ajax的工作原理了。正如我所看到的,您使用的是jQuery,因此非常简单 您可以这样做,首先您不需要onchange=“show(this.value)” 在gg.php,您可以通过$_POST['game']获得该值
有关AJAX的更多信息,请访问您不必将doc ready块放入这样的函数中,您可以使用
$.get()
AJAX方法发送值并获取响应。试试这个:
$(document).ready(function(){
$("#games").change(function(){
var valcc = $(this).val(); // get the current value of selected
$.get("gg.php", {data: valcc}, function(resp){ // pass that in a object
$("#div1").html(resp); // place the comming response html
});
});
});
您可以提取此内联脚本
onchange=“show(this.value)”
$(“#div1”).load(“gg.php?valcc=“+valcc”)代码>?然后在gg.php
页面中使用$\u GET['valcc']
,非常感谢您的时间和帮助,我真的很荣幸,我会这样做的,这是我来这里的第一天,所以我很困惑感谢您的建议。我很高兴来到这里。
$(document).ready(function(){
$("#games").change(function(){
var valcc = $(this).val(); // get the current value of selected
$.get("gg.php", {data: valcc}, function(resp){ // pass that in a object
$("#div1").html(resp); // place the comming response html
});
});
});