2013-04-11 27 views
1

我有一個下拉列表,其中填充了來自數據庫「產品」的列「product_name」。使用php/mysql數據庫查詢和表單上的單選按鈕實時刷新

這些產品中的每一種都具有與該級別相關的不同價格的「良好」,「更好」,「最佳」完成級別。 (即產品1在3個獨立的列中有3種不同的價格)

我希望在選擇「產品名稱」的下拉菜單下面有單選按鈕,標記爲「良好」,「更好」,「最佳」調高與所選單選按鈕完成水平相關聯的數據庫表中的價格,並將該值放入用戶可以看到的單選按鈕下面的文本框中,但顯然不能編輯。

如果用戶更改下拉菜單的選擇,我會希望它實時刷新。

這是所有在一個表單提交到一個PHP頁面,做一些數學發票。

+3

谷歌異步JavaScript和XML,哦,我的意思是'AJAX' – 2013-04-11 16:53:05

+0

@HankyPankyㇱ很好的方式說AJAX。 – 2013-04-11 17:17:46

+0

是的,這就是它的意思 – 2013-04-11 17:18:42

回答

0

這工作:

<?php 
//*************************************** 
// This is downloaded from www.plus2net.com // 
/// You can distribute this code with the link to www.plus2net.com /// 
// Please don't remove the link to www.plus2net.com /// 
// This is for your learning only not for commercial use. /////// 
//The author is not responsible for any type of loss or problem or damage on using this script.// 
/// You can use it at your own risk. ///// 
//***************************************** 

$dbservertype='mysql'; 
$servername='localhost'; 
// username and password to log onto db server 
$dbusername=''; 
$dbpassword=''; 
// name of database 
$dbname='dd'; 

//////////////////////////////////////// 
////// DONOT EDIT BELOW ///////// 
/////////////////////////////////////// 

connecttodb($servername,$dbname,$dbusername,$dbpassword); 

function connecttodb($servername,$dbname,$dbuser,$dbpassword) 
{ 
global $link; 
$link=mysql_connect ("$servername","$dbuser","$dbpassword"); 
if(!$link){die("Could not connect to MySQL");} 
mysql_select_db("$dbname",$link) or die ("could not open db".mysql_error()); 
} 
//////// End of connecting to database //////// 
?> 

<!doctype html public "-//w3c//dtd html 3.2//en"> 

<html> 

<head> 
<title>Multiple drop down list box from plus2net</title> 




<SCRIPT language=JavaScript> 

function reload(form) 
{ 
var val=form.cat.options[form.cat.options.selectedIndex].value; 
self.location='dd.php?cat=' + val ; 
} 

</script> 




</head> 

<body> 
<? 

/* 
If register_global is off in your server then after reloading of the page to get the value of cat from query string we have to take special care. 
To read more on register_global visit. 
http://www.plus2net.com/php_tutorial/register-globals.php 
*/ 


@$cat=$_GET['cat']; // Use this line or below line if register_global is off 
if(strlen($cat) > 0 and !is_numeric($cat)){ // to check if $cat is numeric data or  not. 
echo "Data Error"; 
exit; 
} 


//@$cat=$HTTP_GET_VARS['cat']; // Use this line or above line if register_global is off 

///////// Getting the data from Mysql table for first list box////////// 
$quer2=mysql_query("SELECT DISTINCT category,cat_id FROM category order by category"); 
///////////// End of query for first list box//////////// 

/////// for second drop down list we will check if category is selected else we will display all the subcategory///// 
if(isset($cat) and strlen($cat) > 0) 
    { 
$quer=mysql_query("SELECT DISTINCT subcategory FROM subcategory where cat_id=$cat order by subcategory"); 
} 
else 
{ 
$quer=mysql_query("SELECT DISTINCT subcategory FROM subcategory order by subcategory"); 
} 
////////// end of query for second subcategory drop down list box  /////////////////////////// 


echo "<form method=post name=f1 action='dd-check.php'>"; 
/// Add your form processing page address to action in above line. Example action=dd-check.php//// 


//////////  Starting of first drop downlist ///////// 
echo "<select name='cat' onchange=\"reload(this.form)\"><option value=''>Select one</option>"; 

while($noticia2 = mysql_fetch_array($quer2)) 
{ 
if($noticia2['cat_id'][email protected]$cat) 
{ 
echo "<option selected value='$noticia2[cat_id]'>$noticia2[category]</option>"."<BR>"; 
} 
else 
    { 
    echo "<option value='$noticia2[cat_id]'>$noticia2[category]</option>"; 
    } 
} 
echo "</select>"; 
////////////////// This will end the first drop down list /////////// 



//////////  Starting of second drop downlist ///////// 
echo "<select name='subcat'><option value=''>Select one</option>"; 

while($noticia = mysql_fetch_array($quer)) 
    { 
    echo "<option value='$noticia[subcategory]'>$noticia[subcategory]</option>"; 
    } 

echo "</select>"; 
////////////////// This will end the second drop down list /////////// 



//// Add your other form fields as needed here///// 



echo "<input type=submit value=Submit>"; 
echo "</form>"; 
?> 

<center><a href='http://www.plus2net.com'>PHP SQL HTML free tutorials and scripts</a>  
</center> 
</body> 

</html> 
+0

這有助於檢索「POST」頁面上的數據 '<? $ cat = $ _ POST ['cat']; $ subcat = $ _ POST ['subcat']; echo「\ $ cat = $ cat
的值\ \ subcat = $ subcat的值」; ?>' – user2247238 2013-04-22 19:10:46

0

有太多的背景知識需要回答這個問題。

你應該開始瞭解JavaScript。 有一個很好的教程在這裏:http://www.w3schools.com/js/

當你有一個很好的感覺,瞭解AJAX這裏:http://www.w3schools.com/ajax/default.asp

你或許應該尋找受影響的控件的onChange事件(單選按鈕和下拉菜單)並在其中一個值發生變化時觸發AJAX調用,這會給出您希望放入第三個輸入字段的結果。

希望這有助於你:)