jquery - 如何使用jquery ajax方法发送数据

标签 jquery ajax

我是 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+'&param2='+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/

相关文章:

ajax - MVC 4 : Return partial view inside of bootstrap modal upon failed validation

php - 使用 MySQL、jQuery 和 Ajax 添加或删除表行

javascript - 使用 JavaScript 可缩放和可点击的不规则形状图像 map ?

c# - 检测selenium中的ajax请求

javascript - 取消选中复选框时 AJAX 不起作用

javascript - 提交时重置 div 内容

php - 将 AV 扫描添加到文件上传站点

php - 如何根据 session 查询显示用户消息收件箱?

javascript - 如何调试点击事件从按钮中删除的原因?

jquery - jQuery-Google Analytics(分析)未添加页脚