提交后从php下拉列表返回数据

时间:2014-08-19 21:20:44

标签: php jquery html drop-down-menu

嘿伙计们,我已经通过了更多的时间,然后我原本想要...

所以我这里有这个代码,我有一个下拉列表,从3个单选按钮的选择中给我一个sql数据库的数据,一切正常。

当我想提交表单并获取所述下拉列表中的数据信息时,我的问题就来了。所有我想要的是将选定的无线电和所选项目放在单一下拉列表中的变量中,该变量位于表单的post方法之后的submission.php中...

无论如何这就是我现在想要做的事情

<?php

require "../Scripts/config.php"; // database connection here
?>

<!doctype html public "-//w3c//dtd html 3.2//en">

<html>

<head>
<title>test</title>

<SCRIPT language=JavaScript>
function reload()
{

for(var i=0; i < document.form1.type.length; i++){
if(document.form1.type[i].checked)
var val=document.form1.type[i].value 
}

self.location='bob.php?type=' + val ;

}
</script>
</head>

<body>
<?Php


$tp=$_GET['type']; // getting the value from query string
if(strlen($tp) > 1){$sql="SELECT * FROM Subcategory where cat_id='$tp'";}
echo $sql;


echo "<form name=form1 id=form1 method=post action=submissions2.php>"; 
echo "<select name=Subcategory id=Subcategory value=''>Subcategory</option>"; // printing the list box select command
foreach ($dbo->query($sql) as $row){//Array or records stored in $row
echo "<option value=$row[cat_id]>$row[Subcategory]</option>"; 
/* Option values are added by looping through the array */ 
} echo "</select>";// Closing of list box

echo "<br>";
echo "<br>";
echo "<br>";

echo "
<b>Type</b>
<input type=radio name=type value='1_Cosplay' onclick=\"reload()\";>Cosplay
<input type=radio name=type value='1_Modeling' onclick=\"reload()\";>Modeling
<input type=radio name=type value='1_Zombie' onclick=\"reload()\";>Zombie
<input type=submit value=Submit> </form>";



echo "<br>";
echo "<br>";
echo "<br>";
?>


</body>

</html>

这是submissions2.php

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<script src="../Scripts/jquery-1.8.0.min.js"></script>
</head>
<body>

<?php
function filter($data) {
/*$data = trim(htmlentities(strip_tags($data)));
if (get_magic_quotes_gpc())
$data = stripslashes($data);
$data = mysql_real_escape_string($data);*/
return $data;
return $row;
}
foreach($_POST as $key => $value) {
$mydata[$key] = filter($value);
}

echo $mydata['Subcategory'];
echo "<br>";
?>

</body>
</html>

我似乎只能选择单选按钮。

4 个答案:

答案 0 :(得分:0)

引用所有HTML属性,例如name='Subcategory'

echo "<option value=$row[cat_id]>$row[Subcategory]</option>"

应该是

echo "<option value='{$row['cat_id']}'>{$row['Subcategory']}</option>";

顺便说一句,你的编码练习很可怕。您没有测试MySQL查询中有多少行,并且您不需要在每行上echo。你可以这样做:

echo '<br />'.
'<br />';

当然,使用这样的换行符也是一种不好的做法。使用CSS。

答案 1 :(得分:0)

感谢您的提示,就像我说我已经在尝试解决问题的代码上做了很多工作,我想有些遗留下来了。

我是网络编程的新手,在我能找到的知识很少的情况下尽我所能,我注意到了提示并更改了代码,但它并没有解决我的问题。

我仍然无法将所选项目的值转换为变量以供进一步使用...

答案 2 :(得分:0)

好吧所以我按照phpglue和ajax

的建议完全改变了代码 继承了新代码,但我再也不知道如何返回我的droplists的值

<?php 
include('config.php'); 
$query_parent = mysql_query("SELECT * FROM categories") or die("Query failed:         ".mysql_error());
?>




<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Dependent DropDown List</title>
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript">
$(document).ready(function() {

$("#parent_cat").change(function() {

    $(this).after('<div id="loader"><img src="loading.gif" alt="loading subcategory" />    </div>');
    $.get('loadsubcat.php?parent_cat=' + $(this).val(), function(data) {
        $("#sub_cat").html(data);
        $('#loader').slideUp(200, function() {
            $(this).remove();
        });
    }); 


    });


});





</script>
</head>

<body>
<form name='form1' id='form1' method="get">
    <label for="category">Parent Category</label>
    <select name="parent_cat" id="parent_cat">
        <?php while($row = mysql_fetch_array($query_parent)): ?>
        <option value="<?php echo $row['id']; ?>"><?php echo $row['category_name']; ?>    </option>
        <?php endwhile; ?>
    </select>
    <br/><br/>

    <label>Sub Category</label>
    <select name="sub_cat" id="sub_cat"></select>
</form>
</body>
</html>

继承人的loadubcat.php

<?php 
include('config.php');

$parent_cat = $_GET['parent_cat'];

$query = mysql_query("SELECT * FROM subcategories WHERE categoryID = {$parent_cat}");
while($row = mysql_fetch_array($query)) {
    echo "<option value='$row[id]'>$row[subcategory_name]</option>";
}



?>

答案 3 :(得分:0)

这是一个多功能的解决方案。您需要更改一些引用,例如文件路径的名称/本地路径,但无论如何,它包含所有代码。我无法测试数据库的东西,但如果你在jQuery部分中有正确的url路径,则ajax可以工作。注意,此解决方案引用自身,而不是新页面:         

        // Display errors for troubleshooting
        ini_set('display_errors','1');
        error_reporting(E_ALL);

        class CategorySelector
            {
                public  function LoadSubCat()
                    {
                        // You will be subjected to an injection hack if you don't filter or encode this variable
                        // You should do PDO with prepared statements
                        $parent_cat =   htmlspecialchars($_GET['parent_cat'], ENT_QUOTES);
                        $query      =   $this->Fetch("SELECT id,subcategory_name FROM subcategories WHERE categoryID = '$parent_cat'");

                        // Uncomment this to see how this returns
                        // $this->PrintPre($query);  ?>

                        <label for="sub_cat">Sub Category</label>
                        <select name="sub_cat" id="sub_cat">
                        <?php
                        if($query !== 0) {
                                foreach($query as $row) { ?>
                            <option value="<?php echo $row['id']; ?>"><?php echo $row['subcategory_name']; ?></option>
                            <?php
                                    }
                            } ?>

                        </select>

                        <?php
                    }

                public function Form()
                    {
                        // Get rows for categories
                        $results    =   $this->Fetch("SELECT id,category_name FROM categories");

                        // Uncomment this to see how this returns
                        // $this->PrintPre($results); ?>

                    <form name="form1" id="form1" method="post">
                        <label for="parent_cat">Parent Category</label>
                        <select name="parent_cat" id="parent_cat">
                            <?php 
                            if($results !== 0) {
                                foreach($results as $row) { ?>

                            <option value="<?php echo $row['id']; ?>"><?php echo $row['category_name']; ?></option>
                            <?php   }
                            } ?>
                        </select>

                        <!-- This is a container that will load in your next menu -->
                        <div id="sub_cat_container"></div>
                        <input type="submit" name="submit" value="submit" /> 
                    </form>     
                    <?php
                    }

                public  $rowCount;

                // This is strictly a returning engine for SQL statements
                protected   function Fetch($_sql)
                    {
                        include_once('config.php');
                        // You really should do prepared statements (PDO)
                        // This way of calling sql is depricated
                        $query          =   mysql_query($_sql);
                        // Save the row count
                        $this->rowCount =   mysql_num_rows($query);
                        // If there are rows return them
                        if($this->rowCount > 0) {
                                $_array =   array();
                                // Loop through
                                while($result = mysql_fetch_array($query)) {
                                        $_array[]   =   $result;
                                    }
                            }
                        // Send back your query results for processing
                        // If no results, return false/0
                        return  (isset($_array))? $_array:0;
                    }

                protected function PrintPre($_array)
                    { ?>
                        <pre>
                        <?php print_r($_array); ?>
                        </pre>
                        <?php
                    }
            }

    // Uncomment this for testing that the AJAX is working.
    //   print_r($_REQUEST);

    // This is probably not the best way to do this all, but for sake
    // of trying to get it all to work, this whole thing will ajax to
    // itself, but if you can get it to work on this one page, you
    // can split it up into two pages.

    // Ok, so this creates a new instance of this whole system
    $builder    =   new CategorySelector();

    // If this page receives a GET request for $_GET['parent_cat'], just process it.
    // That action is to call the sub_cat dropdown menu from this object
    if(isset($_REQUEST['parent_cat']) && !empty($_REQUEST['parent_cat'])) {
            $builder->LoadSubCat();
        }
    // If there is no request, display the html page
    else {
    // You should not have space before the <!doctype>. Some browsers act funky if there is space before
    ?><!doctype html>
    <html>
    <head>
    <meta charset="utf-8">
    <title>Dependent DropDown List</title>
    <script type="text/javascript" src="jquery.js"></script>
    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
    <script src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.10.4/jquery-ui.min.js"></script>
    <script type="text/javascript">
        // I'm not a javascript person, so this portion is kind of sloppy

        $(document).ready(function(){

            $('#parent_cat').change(function() {

            // Get value
            var ElmVal = $('#parent_cat').val();

            $.ajax({
                    // You need to reference this page in the "thispage.php" whatever this page is called
                    url:"/thispage.php?parent_cat="+ElmVal,
                    success:function(result) {
                        $("#sub_cat_container").html(result);
                }});
              });
            });

    </script>
    </head>
    <body>
        <?php 
        // Display the form.
        $builder->Form(); ?>

    </body>
    </html>
    <?php } ?>