表单动作在同一个文件上调用php

时间:2014-11-30 08:10:57

标签: php forms

要调用与表单位于同一文件中的php代码。 但这有问题..请帮忙!

<form action="<?php echo $_SERVER['PHP_SELF']; ?>" >
<select name = "filter">

</select>
</form>

<?php
   if isset(["filter"]){
  $db = new PDO("mysql:host=localhost;dbname=project", "root","");
            $c_type = ["filter"];
            $rows = $db->query("SELECT * FROM table WHERE type="$c_type");
   }
   else{
     $db = new PDO("mysql:host=localhost;dbname=project", "root","");
            $rows = $db->query("SELECT * FROM table);
}

2 个答案:

答案 0 :(得分:0)

试试这个:

<form action="#" method="POST">
<select name = "filter">

</select>
</form>

<?php
   if (isset($_POST['filter'])){
       echo "ok";
   }
   else{
      echo "not set";
}
?>

答案 1 :(得分:0)

这将解决它:

<?php
if (isset($_POST["filter"])) {
    $db = new PDO("mysql:host=localhost;dbname=project", "root","");
    $c_type = $_POST["filter"];
    $rows = $db->query("SELECT * FROM table WHERE type='{$c_type}'");
}
else
{
    $db = new PDO("mysql:host=localhost;dbname=project", "root","");
    $rows = $db->query("SELECT * FROM table");
}
?>