简体   繁体   English

如何从同一页面上的选择框中获取值

[英]How can I get a value from a select box on the same page

I'm trying to use PHP and Javascript to make a category selector box. 我正在尝试使用PHP和Javascript来创建一个类别选择器框。 I've set it up so that the Javascript will show the steps in order of being selected, and hide after being deselected. 我已经进行了设置,以便Javascript按顺序显示所选步骤,并在取消选择后隐藏。

However, I can't figure out how to take the selected options "id" or "value" and pass it to the next line. 但是,我无法弄清楚如何选择选项“id”或“value”并将其传递给下一行。 (once the chosen id or value is passed on, the next list can load) (一旦传递了所选的id或值,下一个列表就可以加载)

Here is my code, Thanks in advance for looking. 这是我的代码,提前感谢您的期待。 And please, if I'm doing something wrong or not the right way. 如果我做错了或者没有正确的方法,请求。 Let me know and/or show me the right way to do it. 让我知道和/或告诉我正确的方法。

<?php
include($_SERVER["DOCUMENT_ROOT"] . "/inc/header.php");
include($_SERVER["DOCUMENT_ROOT"] . "/inc/search.php");
?>

<div class="content">
<form>
    <select name="categorys" class="newcatediv" id="step1" size="3" onchange="mine(this.value)">
        <?php 

        $result_c = mysqli_query($con,"SELECT * FROM categories ORDER BY category_name ASC");

        while($row = mysqli_fetch_array($result_c))
        {
            echo '<option class="mso" id="'. $row['category_nameid'] .'"value="';
            echo $row['category_nameid'] .'">' . $row['category_name'] . '</option>';
        } 

         ?>
    </select>

    <select name="sections" class="newcatediv" id="step2" size="3" onchange="mine2(this.value)">
        <?php 

        $var_c = ????

        $result_s = mysqli_query($con,"SELECT * FROM sections WHERE category_nameid='$var_c' ORDER BY section_name ASC");

        while($row = mysqli_fetch_array($result_s))
        {
            echo '<option class="mso" id="'. $row['section_nameid'] .'"value="';
            echo $row['section_nameid'] .'">' . $row['section_name'] . '</option>';
        } 

        ?>
    </select>

    <select name="subsections" class="newcatediv" id="step3" size="3">
        <?php 

        $var_s = ????

        $result_ss = mysqli_query($con,"SELECT * FROM subsections WHERE section_nameid='$var_s' ORDER BY subsection_name ASC");

        while($row = mysqli_fetch_array($result_ss))
        {
            echo '<option class="mso" id="'. $row['subsection_nameid'] .'"value="';    
            echo $row['subsection_nameid'] .'">' . $row['subsection_name'] . '</option>';
        } 

        ?>
    </select>

</form>
</div>

<?php
include($_SERVER["DOCUMENT_ROOT"] . "/inc/footer.php");
    ?>

By default, the first option in a <select> is selected, so this would work: 默认情况下,选择<select>的第一个选项,因此这将起作用:

<select name="categorys" class="newcatediv" id="step1" size="3" onchange="mine(this.value)">
    <?php 

    $result_c = mysqli_query($con,"SELECT * FROM categories ORDER BY category_name ASC");

    $var_c = null;
    while($row = mysqli_fetch_array($result_c))
    {            
        if($var_c == null) $var_c = $row['category_nameid'];
        echo '<option class="mso" id="'. $row['category_nameid'] .'"value="';
        echo $row['category_nameid'] .'">' . $row['category_name'] . '</option>';            
    } 

     ?>
</select>

<select name="sections" class="newcatediv" id="step2" size="3" onchange="mine2(this.value)">
    <?php         

    $result_s = mysqli_query($con,"SELECT * FROM sections WHERE category_nameid='$var_c' ORDER BY section_name ASC");

    $var_s = null;
    while($row = mysqli_fetch_array($result_s))
    {
        if($var_s == null) $var_s = $row['section_nameid'];
        echo '<option class="mso" id="'. $row['section_nameid'] .'"value="';
        echo $row['section_nameid'] .'">' . $row['section_name'] . '</option>';
    } 

    ?>
</select>

<select name="subsections" class="newcatediv" id="step3" size="3">
    <?php         

    $result_ss = mysqli_query($con,"SELECT * FROM subsections WHERE section_nameid='$var_s' ORDER BY subsection_name ASC");

    while($row = mysqli_fetch_array($result_ss))
    {
        echo '<option class="mso" id="'. $row['subsection_nameid'] .'"value="';    
        echo $row['subsection_nameid'] .'">' . $row['subsection_name'] . '</option>';
    } 

    ?>
</select>

Cheers 干杯

Hi :) You Can't Process that at the same page using Php. 嗨:)你不能使用Php处理同一页面。 But you can do that with this jquery including 3 pages. 但你可以用这个jquery做到这一点,包括3页。 First Page: 第一页:

$(document).ready(function(){
$("#step1").change(function(){
    var id=$("#step1").val();

    alert(id); //shouts the value of the selected step1
    $.post("select_step2.php", {id:id}, function(data){
        $("#step2").empty();
        $("#step2").append(data);

                    $("#step2").change(function(){
                         var id2=$("#step2").val();
                         alert(id2); //shouts the value of the selected step2

                         $.post("select_step3.php", {id:id2}, function(data){
                             $("#step3").empty();
                             $("#step3").append(data);
                          });  
                   });
        });
   });
  });

The above code is for jquery where you can call each data's that depends on each step. 上面的代码用于jquery,您可以在其中调用依赖于每个步骤的每个数据。

<?php
include($_SERVER["DOCUMENT_ROOT"] . "/inc/header.php");
include($_SERVER["DOCUMENT_ROOT"] . "/inc/search.php");
?>


<form>
 First Step: <select name="categorys" class="newcatediv" id="step1" size="3">
    <?php 

    $result_c = mysqli_query($con,"SELECT * FROM categories ORDER BY category_name ASC");

    while($row = mysqli_fetch_array($result_c))
    {
        echo '<option class="mso" id="'. $row['category_nameid'] .'"value="';
        echo $row['category_nameid'] .'">' . $row['category_name'] . '</option>';
    } 

     ?>
</select>

Second Step: <select name="sections" class="newcatediv" id="step2" size="3"></select>

Third Step: <select name="subsections" class="newcatediv" id="step3" size="3"></select>

Code for you select_step2.php: 您的代码select_step2.php:

<?php

 //Please include the connection to your database here :)

 $var_c = trim($_POST['id']);

     $section = "";
    $result_s = mysqli_query($con,"SELECT * FROM sections WHERE category_nameid='$var_c' ORDER BY section_name ASC");

    while($row = mysqli_fetch_array($result_s))
    {
        $section.="<option value='$row[section_nameid]'>$row[section_name]</option>";
    } 
    echo $section;

?>

Code for your select_step3.php: select_step3.php的代码:

<?php 
  //database connection here
   $var_s = trim($_POST['id']);

    $subsection= "";
    $result_ss = mysqli_query($con,"SELECT * FROM subsections WHERE section_nameid='$var_s' ORDER BY subsection_name ASC");

    while($row = mysqli_fetch_array($result_ss))
    {
        $subsection.="<option value='$row[subsection_nameid]'>$row[subsection_name]</option>";
    } 
   echo $subsection;
 ?>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何从选择框中获取值? - How can I get a value from the select box? 如何从选择框中获取值并使用它来获取API的响应? - How can i get a value from a select box and use it to get a response from an API? 我可以从给定的选择框文本中获取选择框值 - Can I get Select box value from given select box text 我如何从具有相同类的不同选择类型中选择值并获取其值 - How can i select values from different select types having same class and get their value 如何从相同格式的相同名称的不同选择选项中获得一个值 - How can i get one value from different select options with the same name in the same form 如何从 select 盒子到表中获取价值? - How to get value from select box to table? 当所有 select 框都有一个名称时,我如何从 DOM 添加/删除 select 框,以便在提交时我从活动的 select 框中获取值? - How can I add/remove select boxes from DOM when all select boxes have one name so that upon Submit I get value from active select box? 如何从JavaScript的选择框中获取价值? - How can i get value from selection box in javascript? 如何获取多个复选框的值并使用复选框中的值执行增量 - how can i get value of multiple checked box and perform increment using value from checked box 在单击之前,我无法获得选择框值 - I can't get a select box value until is clicked
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM