简体   繁体   English

JSON获取表单数据请求

[英]JSON Get Request of form Data

I am completely new to JQuery / JSON requests. 我对JQuery / JSON请求是完全陌生的。 The code below is the result of 2 days work and I am completely stumped! 下面的代码是2天工作的结果,我完全陷入了困境!

Basically I have a form asking for 基本上我有一个表格要

  • Number of people 人数
  • Size of painting 绘画尺寸
  • Color of painting (B,C) IE black/white or color 绘画颜色(B,C)IE黑色/白色或彩色

Esentially after selecting the dropdowns then clicking on the color radio button, it will send a JSON request to getdata.php?getIDandPrice=C 基本上,选择下拉列表后,单击颜色单选按钮,它将向JSON请求发送getdata.php?getIDandPrice=C

What I am trying to achieve is that it will make this request based on the previous entries: 我想要实现的是,它将根据之前的条目发出此请求:

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

(ie number of people - size - color) (即人数-大小-颜色)

Appreciate your help 感谢您的帮助

<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>

Below is the final working code thanks to I Can Has Cheezburger 由于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>

If I got it right, you want to pass the value C along with the two select options, you could do it like: 如果我做对了,您希望将值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);
    });
}

In the PHP side, you would have to do something like: 在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

Pass the values of the inputs you need to process on the backend: 传递您需要在后端处理的输入值:

$.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);
});

The documentation has several examples: 该文档有几个示例:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM