我是 ajax 新手,想知道如何使用 jquery ajax 方法发送数据,任何帮助将不胜感激。 这是我的代码:
<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>
我需要将 select 选项的值发送到 gg.php 页面,然后继续处理。 请帮忙
最佳答案
在选择输入时调用此函数onchange
。
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 - 如何使用jquery ajax方法发送数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23905205/