duanpaxin3531 2013-04-11 16:48
浏览 58
已采纳

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

I have a drop down list that is populated with a column “product_name” from a database “products”.

Each of these products has a finish level, “good”, “better”, “best” with a different price associated with that level. (i.e. product 1 has 3 different prices in 3 separate columns)

I would like to have radio buttons labeled “good”, “better”, “best” underneath the drop down that take the selected “product name” and look up the price in the database table associated with the selected radio button finish level and place that value into a text box underneath the radio buttons that the user can see, but obviously not edit.

I would want it to refresh in real time if the user changes the selection of the drop down menu.

This is all on a form that is submitted at the end to a php page that does some math to make an invoice.

  • 写回答

2条回答 默认 最新

  • douhuan1648 2013-04-22 19:04
    关注

    This works:

    <?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>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?
  • ¥15 求daily translation(DT)偏差订正方法的代码
  • ¥15 js调用html页面需要隐藏某个按钮
  • ¥15 ads仿真结果在圆图上是怎么读数的
  • ¥20 Cotex M3的调试和程序执行方式是什么样的?
  • ¥20 java项目连接sqlserver时报ssl相关错误