4

我需要帮助。我想显示用户从 3 个下拉列表中选择的三个数据值。我创建了三个下拉列表,其中用户将从数据库中为每个选项选择 3 个不同的选项,然后用户将选择的任何内容都必须出现在表中。例如,如果用户为 selection_id 选择 111,为 fixture_id 选择 222,那么 Jesty 作为名称,它应该在表中显示为 111 222 Jesty。现在我只有从数据库中检索信息然后显示下拉菜单的下拉菜单……我需要一种显示用户选择的方法,或者将向用户打印他选择的内容的 javascript

这是我的 3 个下拉列表

require "config.php"; //Database connection
    $resource_selections = mysql_query("SELECT DISTINCT selection_id FROM selections ORDER BY selection_id ASC");
    $selections = array();
    while($row = mysql_fetch_row($resource_selections)){
        $selections[] = $row[0];    
    } 
    $resource_fixtures = mysql_query("SELECT DISTINCT fixture_id FROM selections ORDER BY selection_id ASC"); 
    $fixtures = array();
    while($row = mysql_fetch_row($resource_fixtures)){
        $fixtures[] = $row[0];
    } 
    $resource_names   = mysql_query("SELECT DISTINCT name FROM selections ORDER BY selection_id ASC");    
    $names = array();
    while($row = mysql_fetch_row($resource_names)){
        $names[] = $row[0];
    }
    if(count($selections) <= 0 || count($fixtures) <= 0 || count($names) <= 0){
        echo 'No results have been found.';
    } else {

        // Display form
        echo '<form name="form" method="post" action="selection.php">';

        //SelectionID dropdown:
        echo "<select name='selection_id' id='selections' >"; 
        foreach($selections as $selection) echo "<option id='$selection'>$selection</option>";
        echo '</select>';

        //FixtureID dropdown
        echo "<select name='fixture_id' id='fixtures' >"; 
        foreach($fixtures as $fixture) echo "<option id='$fixture'>$fixture</option>";
        echo '</select>';

        //Name dropdown
        echo "<select name='name' id='names' >"; 
        foreach($names as $name) echo "<option id='$name'>$name</option>";
        echo '</select>';

        echo "<input type='submit' name='submit' value='Submit' />";




        echo '</form>';



    }


    ?>
4

2 回答 2

1

你可以试试这样

    <script type="text/javascript">

    function mergeval(val,flag)
    {
      if(flag==1)
      document.getElementById('mvalue1').innerHTML = val ;
      else if(flag==2)
      document.getElementById('mvalue2').innerHTML = val;
      else if(flag==3)
      document.getElementById('mvalue3').innerHTML = val;
    }

    </script>
<?php
session_start();
$_SESSION['values']=!empty($_SESSION['values']) ? $_SESSION['values']:array();
if(!empty($_REQUEST['selection_id'])&&!empty($_REQUEST['fixture_id'])&&!empty($_REQUEST['name']))
{
 $_SESSION['values'][] = array('id'=>$_REQUEST['selection_id'],'fid'=>$_REQUEST['fixture_id'],'name'=>$_REQUEST['name']);
}
$dispval ="<table>";

foreach(@$_SESSION['values'] as $key=>$val)
{

$dispval .="<tr><td>".$val['id']."&nbsp;".$val['fid']."&nbsp;".$val['name']."</td></tr>";

}
$dispval .="</table>";
echo $dispval;

?>
    <?php
    require "config.php"; //Database connection
        $resource_selections = mysql_query("SELECT DISTINCT selection_id FROM selections ORDER BY selection_id ASC");
        $selections = array();
        while($row = mysql_fetch_row($resource_selections)){
            $selections[] = $row[0];    
        } 
        $resource_fixtures = mysql_query("SELECT DISTINCT fixture_id FROM selections ORDER BY selection_id ASC"); 
        $fixtures = array();
        while($row = mysql_fetch_row($resource_fixtures)){
            $fixtures[] = $row[0];
        } 
        $resource_names   = mysql_query("SELECT DISTINCT name FROM selections ORDER BY selection_id ASC");    
        $names = array();
        while($row = mysql_fetch_row($resource_names)){
            $names[] = $row[0];
        }
        if(count($selections) <= 0 || count($fixtures) <= 0 || count($names) <= 0){
            echo 'No results have been found.';
        } else {

            // Display form
            echo '<form name="form" method="post" action="selection.php">';

            //SelectionID dropdown:
            echo "<select name='selection_id' id='selections' onchange='javascript:mergeval(this.value,1)'>"; 
            foreach($selections as $selection) echo "<option id='$selection'>$selection</option>";
            echo '</select>';

            //FixtureID dropdown
            echo "<select name='fixture_id' id='fixtures' onchange='javascript:mergeval(this.value,2)' >"; 
            foreach($fixtures as $fixture) echo "<option id='$fixture'>$fixture</option>";
            echo '</select>';

            //Name dropdown
            echo "<select name='name' id='names' onchange='javascript:mergeval(this.value,3)'>"; 
            foreach($names as $name) echo "<option id='$name'>$name</option>";
            echo '</select>';

            echo "<input type='submit' name='submit' value='Submit' />";




            echo '</form>';



        }


        ?>
    <table>
    <tr><td >&nbsp;<span id="mvalue1"></span>&nbsp;<span id="mvalue2"></span>&nbsp;<span id="mvalue3"></span></td></tr>
    </table>
于 2013-05-23T06:50:59.943 回答
0

您可以使用 jquery 函数来实现它,我为您粘贴代码。这是一个脚本:

     <script language="JavaScript" type="text/javascript">
     $(document).ready(function(){
         $(".testval1").live('change',function(){
         var x = $(this).val();
         if(x!='')
         {
            $(".displaydata").append("<td>"+x+"</td>");
         }
         });
     });
    </script>

这是html

 <body>
 <form>
    <select name='testval1' class='testval1'>
       <option value=''>select data</option>
       <option value="110">110</option>
       <option value="111">111</option>
    </select>
  <select name='testval2' class='testval1'>
      <option value=''>select data</option>
      <option value="2223">2223</option>
      <option value="222">222</option>
  </select>
  <select name='testval3' class='testval1'>
      <option value=''>select data</option>
      <option value="Jestysd">Jestysd</option>
      <option value="Jesty">Jesty</option>
  </select>
  <table class="displaydata">
  </table>
  </form>

您需要为此添加 jquery-1.9.1.min.js .. 一切顺利

于 2013-05-23T07:45:10.263 回答