使用php/mysql数据库查询和表单上的单选按钮实时刷新

使用php/mysql数据库查询和表单上的单选按钮实时刷新,php,mysql,forms,Php,Mysql,Forms,我有一个下拉列表,其中填充了数据库“products”中的“product_name”列 每种产品都有一个光洁度等级“好”、“更好”、“最好”,并且与该等级相关的价格不同。(即产品1在3个单独的栏中有3种不同的价格) 我希望在下拉列表下方有标记为“好”、“更好”、“最好”的单选按钮,该下拉列表包含所选的“产品名称”,并在与所选单选按钮完成级别相关的数据库表中查找价格,并将该值放入用户可以看到的单选按钮下方的文本框中,但显然不是编辑 如果用户更改下拉菜单的选择,我希望它能够实时刷新 所有这些都在一

我有一个下拉列表,其中填充了数据库“products”中的“product_name”列

每种产品都有一个光洁度等级“好”、“更好”、“最好”,并且与该等级相关的价格不同。(即产品1在3个单独的栏中有3种不同的价格)

我希望在下拉列表下方有标记为“好”、“更好”、“最好”的单选按钮,该下拉列表包含所选的“产品名称”,并在与所选单选按钮完成级别相关的数据库表中查找价格,并将该值放入用户可以看到的单选按钮下方的文本框中,但显然不是编辑

如果用户更改下拉菜单的选择,我希望它能够实时刷新


所有这些都在一个表单上,表单在最后提交给一个php页面,该页面会做一些数学运算来制作发票。

要在一个问题中回答这个问题,需要的背景知识太多了

您应该从了解javascript开始。 这里有一个很好的教程:

如果您对此感觉良好,请在此处学习AJAX:

您可能应该在受影响的控件(单选按钮和下拉菜单)上查找onChange事件,并在其中一个值发生更改时触发AJAX调用,这将为您提供希望放入第三个输入字段的结果

希望这对您有所帮助:)

这很有效:

<?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']==@$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>

Google异步JavaScript和XML,哦,我是说
AJAX
@HankyPankyㇱ 说AJAX很好。是的,这就是它所代表的。这有助于检索“POST”页面上的数据