将html组合框值传递给同一文件中的php而不提交

时间:2014-06-27 05:47:28

标签: php html combobox

我想根据另一个组合框1中的选择更改组合框2。 我想从数据库中检索combobox2的条目,我需要在php中执行此操作。 为此,我需要将combobox1中的选定值传递给php作为php变量。尝试了很多......但还没有得到输出。 这是我的代码

Degree Type<select id="Select3" name="deg">
            <option value="UG">UG</option>
             <option value="PG">PG</option>
        </select>
Department Name<?php 

        $host="localhost" ; 
    $mysql_db="db" ; 
    $mysql_u="root" ; 
    $mysql_p="" ; 
    mysql_connect( "$host", "$mysql_u", "$mysql_p"); 
    mysql_select_db( "$mysql_db"); 
    $sel="select Dept_Name from dept_mast where Deg_Type='???' "; 

    $val=mysql_query($sel);
    $selectbox='<select name=\'userst\'>';
while ($row = mysql_fetch_assoc($val)) {
$selectbox.='<option value=\"' . $row['Dept_Name'] . '\">' . $row['Dept_Name'] .  '</option>';
}
$selectbox.='</select>';
echo $selectbox;
        ?>

如何在???

的位置传递度数类型组合框中的选定值

帮助我! 谢谢!

2 个答案:

答案 0 :(得分:0)

如果要在不刷新整个页面的情况下获取值,则需要使用ajax。在这个例子中,它的jQuery是$.ajax。 (简单的同一页ajax)

<?php

if(isset($_POST['get_values'])) {
    $data = array();
    $selected = $_POST['value'];
    $host = "localhost";
    $mysql_db = "db";
    $mysql_u = "root";
    $mysql_p = "";
    $con = new mysqli($host, $mysql_u, $mysql_p, $mysql_db);
    $stmt = $con->prepare('SELECT Dept_Name FROM dept_mast WHERE Deg_Type = ?');
    $stmt->bind_param('s', $selected);
    $stmt->execute();
    $result = $stmt->get_result();
    while ($row = $result->fetch_assoc()){
        $data[] = $row;
    }

    // return value as json
    echo json_encode($result);
    exit;
}

?>

<select id="Select3" name="deg">
    <option value="UG">UG</option>
    <option value="PG">PG</option>
</select>
<div id="second_select"></div>

<script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<!-- <script src="jquery.min.js"></script> -->
<script type="text/javascript">
$(document).ready(function(){

    $('#Select3').on('change', function(){
    // when user selects a value
        var selected = $(this).val(); // get the value
            // request to server
        $.ajax({
            url: document.URL, // same page process
            type: 'POST',
            data: {get_values: true, value: selected},
            dataType: 'JSON',
            success: function(response) {
                        // get the response of the server, and put it inside a new select box
                $('#second_select').html('<select name="userst"></select>');
                var options = '';
                $.each(response, function(index, element){
                    options += '<option value="'+element.Dept_Name+'">'+element.Dept_Name+'</option>';
                });
                $('select[name="userst"]').html(options);
            }
        });
    });


});
</script>
  

旁注:不要气馁,避免!使用mysql_ *函数,使用NEWER和IMPROVED版本 MYSLQI 或PDO

答案 1 :(得分:0)

    js:
    ----

    function get_fields() { 

       var cource = $("#cource option:selected").val();

        $.ajax({
             type: "POST",
             url: 'get_fields.php?cource='+cource,
             //Specify the datatype of response if necessary
             data: $("#form_id").serialize(),
             success: function(data){
                    $("span#column").html(data);
             }
        });

    }


    form.html
    ---------


    // Select the cource

    <select name="cource" id="cource"  onchange="get_fields()">
        <option value="Select" >Select</option>
        <option value="PG" >PG</option>
        <option value="UG" >UG</option>
    </select>

    <span id="column">
        <select name="department" id="department">
            <option value="Select" >Select</option> 
        </select>
    </span>


    get_fields.php
    ---------------

    <?php 

      $select = "select * from department where cource_code = '$_REQUEST['cource']' ";
      $department = mysql_query($select);

     ?>

     <select  name="department" id="department"  >
       <option value="select">Select</option>
        <?php
        if($department){
            while ($row = mysql_fetch_array($department)) {  ?>
                <option value="<?php echo $row['code'] ?>"><?php echo strtoupper($row['department_name']) ;?></option>
          <?php  }
        }
        ?>  
     </select>



Try this..Now you can filter with out submit using this code...