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


return data from php dropdownlist after submit

嘿,伙计们,我已经在这上面花了太多的时间了。

所以我有这个代码在这里,我有一个下拉列表给我的数据从一个SQL数据库从3个单选按钮的选择,这一切都很好。

我的问题来了,当我想提交我的表格,并获得数据的信息,在上述列表。我所要做的就是将选中的单选项和选中的项放到submit .php变量的下拉列表中,该变量位于表单的post方法之后…

不管怎样,这就是我现在想做的

<?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>

我似乎只能得到单选按钮的选项

这是一个一体化的解决方案。您需要更改一些引用,例如文件路径的名称/本地路径,但无论如何,这包含了所有代码。我不能测试DB的东西,但ajax的工作,如果你有jQuery部分正确的url路径。注意,这个解决方案引用自己,而不是一个新的页面:

        // 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 } ?>

引用所有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。