2014-04-07 65 views
1

我完全不熟悉JQuery/JSON請求。下面的代碼是2天工作的結果,我完全被難住了!JSON獲取表單數據請求

基本上我有一個表格要求

  • 人數
  • 顏色畫的(B,C)IE的黑/白或彩色

Esentially的大小選擇下拉菜單後點擊顏色單選按鈕,它會發送一個JSON請求到getdata.php?getIDandPrice=C

我想實現的是,它將使基於以前的條目這個請求:

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> 

下面是最後的工作代碼感謝我能一直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> 

回答

1

如果我這樣做是正確,你想用兩個選擇選項一起傳遞的價值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 
+0

嗨Cheezburger,非常感謝你沒有期待這麼快的迴應!這工作完美無瑕。第一次海報(但我花了很多天瀏覽),有沒有辦法通過網站向你買啤酒? –

+0

@RyanNZ雖然這不是一個非常困難的問題,但是謝謝,你的想法很重要。 –

+0

對我來說,這是:) 如果你不介意,還有一個問題,我稍微調整了代碼,以包括C或B變量取決於所選的單選按鈕,以及將功能附加到下拉菜單。我遇到的唯一問題是由於某種原因,無論是否選擇C或B,它都會每次向B發送一個JSON請求。 我會在下面發佈代碼,因爲這裏沒有足夠的字符。 –

0

合格的,你需要在後端處理的輸入值:

$.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/