【问题标题】:JSON Get Request of form DataJSON 获取表单数据请求
【发布时间】:2014-05-19 09:50:56
【问题描述】:

我对 JQuery / JSON 请求完全陌生。下面的代码是 2 天工作的结果,我完全被难住了!

基本上我有一个表格要求

  • 人数
  • 画幅
  • 绘画颜色 (B,C) IE 黑/白或彩色

基本上在选择下拉菜单然后单击颜色单选按钮后,它将向getdata.php?getIDandPrice=C发送一个JSON请求

我想要实现的是它会根据之前的条目发出这个请求:

getdata.php?getIDandPrice=1-2x2-C    

(即人数-大小-颜色)

感谢您的帮助

<select id="howmanypeople" name="howmanypeople">
    <option value="" selected="selected">Select how many people</option>
    <option value="1">1</option>
    <option value="2">2</option>
</select>

<select id="size" name="size">
    <option value="" selected="selected">Select size</option>
    <option value="1x1">1x1</option>
    <option value="2x2">2x2</option>
</select>


<label><input type="radio" name="getIDandPrice" value="B" id="method2"/>Black & White</label>
<label><input type="radio" name="getIDandPrice" value="C" id="method2"/>Color</label>  
<label><input type="radio" name="getIDandPrice" value="B" id="method2"/>Black & White</label>    

<div id="post_id"></div>
<div id="_regular_price"></div>

<script src="//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function() {
$('input[name=getIDandPrice]').click(function() {
MethodTwo();
});
});

//Method 2

document.getElementById('getIDandPrice').value = product(2, 3);
function MethodTwo()
{
$.getJSON("getdata.php?getIDandPrice=C", function(response) {
        $('#post_id').html(response.post_id);
        $('#_regular_price').html(response._regular_price);
});
}
</script>

感谢 I Can Has Cheezburger,下面是最终的工作代码

<!-- http://stackoverflow.com/questions/7191910/loading-json-data-with-jquery-php-and-mysql-for-radio-buttons -->
<select id="howmanypeople" name="howmanypeople"> 
    <option value="" selected="selected">Select how many people</option>
    <option value="1">1</option>
    <option value="2">2</option>
</select>

<select id="size" name="size">
    <option value="" selected="selected">Select size</option>
    <option value="1x1">1x1</option>
    <option value="3x3">3x3</option>
</select>
        <label><input type="radio" name="color" value="B" id="color"/>Black & White</label>
        <label><input type="radio" name="color" value="C" id="color"/>Color</label>


<div id="post_id"></div>
<div id="_regular_price"></div>

<script src="//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function() {
    $('input[name=color]').click(function() {
    MethodTwo();
    });
    $('select[name=size]').click(function() {
    MethodTwo();
    });
    $('select[name=howmanypeople]').click(function() {
    MethodTwo();
    });
});


//Method 2
function MethodTwo()
{
//getting the selected value of select#howmanypeople
var people_no = $('#howmanypeople').val();
//getting the selected value of select#size
var size = $('#size').val();
//getting the selected value of select#size
var color = $('#color:checked').val();
//concatenating the values for the URL to look as 
//getdata.php?getIDandPrice=1-2x2-C
$.getJSON("getdata.php?getIDandPrice="+people_no+"-"+size+"-"+color, function(response) {
        $('#post_id').html(response.post_id);
        $('#_regular_price').html(response._regular_price);
    });
}</script>
</body>
</html>

【问题讨论】:

    标签: javascript php jquery json


    【解决方案1】:

    传递您需要在后端处理的输入值:

    $.getJSON("getdata.php?getIDandPrice=C", 
        {
            howMany : $('howmanypeople').val(),
            size : $('size').val(),
        },
        function(response) {
            $('#post_id').html(response.post_id);
            $('#_regular_price').html(response._regular_price);
    });
    

    文档有几个例子:

    http://api.jquery.com/jquery.getjson/

    【讨论】:

      【解决方案2】:

      如果我猜对了,你想传递值 C 以及两个选择选项,你可以这样做:

      function MethodTwo()
      {
      //getting the selected value of select#howmanypeople
      var people_no = $('#howmanypeople').val();
      //getting the selected value of select#size
      var size = $('#size').val();
      //concatenating the values for the URL to look as 
      //getdata.php?getIDandPrice=1-2x2-C
      $.getJSON("getdata.php?getIDandPrice="+people_no+"-"+size+"-C", function(response) {
              $('#post_id').html(response.post_id);
              $('#_regular_price').html(response._regular_price);
          });
      }
      

      在 PHP 方面,您必须执行以下操作:

      $var = $_GET['getIDandPrice'];
      //explode() will break the text at the hyphen `-`
      $var_arr = explode("-",$var);
      //$var_arr[0] = 1, $var_arr[1] = 2x2, $var_arr[2] = C
      

      【讨论】:

      • 嗨 Cheezburger,非常感谢你没想到这么快就有回应!这完美无缺。第一次发帖(不过我浏览了很多天),有什么方法可以通过网站给你买啤酒吗?
      • @RyanNZ 虽然这不是一个非常困难的问题,但是谢谢,你的想法很重要。
      • 对我来说是 :) 如果您不介意,我会稍微调整代码以包含 C 或 B 变量,具体取决于所选的单选按钮,并将函数附加到下拉菜单。我遇到的唯一问题是由于某种原因,它每次都向 B 发送 JSON 请求,无论选择 C ​​还是 B。我将代码贴在下面,因为这里没有足够的字符。
      • @RyanNZ 您可以通过以下方式找到选中的单选按钮的值:$(input[name='color']:checked).val();
      • 字面意思是刚回到这里发布上面的代码!!谢谢队友:)
      猜你喜欢
      • 2020-08-30
      • 1970-01-01
      • 2013-08-15
      • 2019-09-27
      • 2017-07-05
      • 2018-01-06
      • 1970-01-01
      • 2016-12-17
      • 1970-01-01
      相关资源
      最近更新 更多