【问题标题】:Sending a value from a dropdown box to PHP via jQuery通过 jQuery 将下拉框中的值发送到 PHP
【发布时间】:2011-03-22 18:08:34
【问题描述】:

我正在尝试从两个下拉框中获取值并将它们发送到 PHP 文件,该文件将根据选择的组合从 mySQL 数据库中绘制适当的字段并将其显示在 div 中,而无需使用 AJAX 刷新页面。我已经对第二部分进行了排序,但我被困在了第一部分。

这是 HTML:http://jsfiddle.net/SYrpC/

这是我在主文档头部的 Javascript 代码:

var mode = $('#mode');

function get() {$.post ('data.php', {name: form.him.value, the_key: #mode.val()}, 

function(output) {$('#dare').html(output).show();

});

}

我的 PHP(用于测试目的)是:

$the_key = $_POST['the_key'];
echo $the_key;

在 PHP 中将它作为变量后,我可以对其进行操作,但在获取它时遇到了麻烦。我哪里错了?感谢您的回复!

【问题讨论】:

    标签: php jquery mysql drop-down-menu


    【解决方案1】:

    您还需要一个回调函数来让服务器响应 POST。

    $.post('ajax/test.html', function(data) { $('.result').html(数据); });

    这个 sn-p 将发布到 ajax/test.html 并且匿名函数将在其回复时被调用,参数数据具有响应。然后它在这个匿名函数中将带有结果的类设置为具有服务器响应的值。

    帮助?让我知道,如果您需要更多信息,我们可以解决这个问题。

    另外,jQuery 中的 $.post 是

    的简写形式

    $.ajax({ 类型:'POST', 网址:网址, 数据:数据, 成功:成功 数据类型:数据类型 });

    【讨论】:

    • 感谢您的帮助。有什么地方可以实时通话吗?非常感谢您的帮助!
    • 是的,我们可以,您什么时候有空,我该如何联系您?
    【解决方案2】:

    您的 jquery 选择器错误:

    html:
    <select id="mode">
    
    jquery selector:
    $("#mode").val();
    
    html:
    <select name="player">
    jquery selector:
    $("select[name=player]").val();
    

    【讨论】:

      【解决方案3】:

      你想为你的ajax请求添加一个回调,这并不难,这里我给你一个例子:

      $.ajax({
         url: "http://stackoverflow.com/users/flair/353790.json", //Location of file
         dataType: "josn",//Type of data file holds, text,html,xml,json,jsonp
          success : function(json_data) //What to do when the request is complete
          {
              //use json_data how you wish to.;
          },
          error : function(_XMLHttpRequest,textStatus, errorThrown)
          {
              //You fail
          },
          beforeSend : function(_XMLHttpRequest)
          {
              //Real custom options here.
          }
      });​
      

      上述大多数回调都是可选的,在您的情况下,我会执行以下操作:

      $.ajax({
         url: "data.php",
         dataType: "text",
         data : {name: ('#myform .myinput').val(),the_key: $('#mode').val()},
         success : function(value)
         {
             alert('data.php sent back: ' + value);
         }
      });​
      

      您应该始终设置的是url,successdata,如果需要,请阅读The Documentation 了解更多信息。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2023-03-16
        • 1970-01-01
        • 2011-09-23
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多