来自动态下拉列表的POST值

时间:2013-12-27 08:33:38

标签: php jquery mysqli

我有2下拉主要和子。我正在尝试POST两个下拉列表的值,但每次我提交时都会在url dyna.php?main=2&sub=2&submit=Submit中得到这个,并且当我回显帖子值时没有任何内容出现。我想在同一页面中发布两个下拉列表的值,因为我将对该值进行查询。

这是我的完整代码

dyna.php

<body>
    <form id="form1" name="form1" action="<?php $_SERVER['PHP_SELF']?>">
 Drop1
 <?php
    $mysqli = new mysqli("localhost", "root", "", "lists");
    $result = $mysqli->query("SELECT * FROM main_list GROUP BY id ORDER BY id");
    $option = '';
    while($row = $result->fetch_assoc())
    {
       $option .= '<option value = "'.$row['id_no'].'">'.$row['value'].'</option>';
    }
    ?>

     <select id="main" name="main">
     <option selected=selected>Choose</option>
       <?php echo $option; ?>
     </select>
    <div id="sublist"></div>

   <input type="submit" name="submit" value="Submit" />
</form> 
<script type="text/javascript">
$('#main').change(function(){
$.ajax({
url : 'secondlist.php',
data :{mainlist_id : $(this).val()},
dataType:'html',
type:'POST',
success:function(data){
$('#sublist').html(data);
}
});
});
</script>
</body>

Secondlist.php

<?php
$out = $_POST['mainlist_id'];

    $mysqli = new mysqli("localhost", "root", "", "lists");
    $result1 = $mysqli->query("
    select 
        a.id, a.value, a.id_no, b.id, b.category, b.value
    from
    (select *
    from main_list) a 
    left outer join
    (select *
    from sub_list) b 
    on a.id_no=b.category
    WHERE a.id_no='$out'
    ");
    $option1 = '';

    while($row = $result1->fetch_assoc())
    {
        $option1 .= '<option value = "'.$row['category'].'">'.$row['value'].'</option>';
    }

    $output = 'Drop2 ';
    $output .= '<select name="sub" id="sub"  onchange="run()">';
    $output .= '<option value=" " disabled="disabled" selected="selected">Choose one</option>';
    $output .= $option1;
    $output .= '</select> ';
echo $output;
exit;
?>

4 个答案:

答案 0 :(得分:0)

将您的脚本放入.ready of jquery

<script type="text/javascript">
$(document).ready(function(){
    $('#main').change(function(){
        $.ajax({
            url : 'secondlist.php',
            data :{mainlist_id : $(this).val()},
            dataType:'html',
            type:'POST',
            success:function(data){
                $('#sublist').html(data);
            }
        });
    });
});
</script>

答案 1 :(得分:0)

使用按钮提交表单时,如果未指定任何内容,则使用默认的GET方法。 GET将formdata作为url编码的查询字符串附加到您的操作URL。

要使用HTTP POST提交数据,请使用表单属性method="post"

如果要添加文件表单字段,请同时添加参数enctype="multipart/form-data"

<form id="form1" name="form1" action="<?php $_SERVER['PHP_SELF']?>" method="post" enctype="multipart/formdata">
</form>

http://www.w3.org/TR/html401/interact/forms.html#h-17.3

答案 2 :(得分:0)

<script type="text/javascript" src="http://code.jquery.com/jquery-1.10.2.min.js"></script>
<script>
$(document).ready(
function(){
$('#form1').submit(
function(a){
alert($('#form1').serialize());
a.preventDefault();
})
})

</script>


<div>
    <form id="form1" name="form1">
    <select name="opton1">

    <option value="1"> option 11 </option>
    <option value="2"> option 12 </option>
    <option value="3"> option 13 </option>
    <option value="4"> option 14 </option>
    </select>   

    <select name="opton2">
    <option value="21"> option 21 </option>
    <option value="22"> option 22 </option>
    <option value="23"> option 23 </option>
    <option value="24"> option 24 </option>
    </select>

    <input type="submit">
    </form>

</div>

只是一个简单的例子

答案 3 :(得分:0)

dyna.php
首先在表单中添加方法。

<form id="form1" name="form1" action="<?php $_SERVER['PHP_SELF']?>" method="post">

然后<div id="sublist"></div>将其替换为以下内容:

<select name="sub" id="sub"  onchange="run()">
 <option value="">Select</option>
</select>

并在secondlist.php

<?php
    $out = $_POST['mainlist_id'];
    $mysqli = new mysqli("localhost", "root", "", "lists");
    $result1 = $mysqli->query("
    select 
        a.id, a.value, a.id_no, b.id, b.category, b.value
    from
    (select *
    from main_list) a 
    left outer join
    (select *
    from sub_list) b 
    on a.id_no=b.category
    WHERE a.id_no='$out'
    ");
    while($row = $result1->fetch_assoc())
    {
    ?>
        <option value = "<?php echo $row['category'];?>"><?php echo $row['value'];?></option>';
    <?php 
    }
?>

并在dyna.php 将此jquery行$('#sublist').html(data);更改为$('#sub').html(data);

并注意SQL注入。See this