简体   繁体   English

带选择框的html表单无法在另一个php页面中获取值

[英]html form with select box cannot get value in another php page

i have a php page called page1.php with this form 我有这种形式的名为page1.php的php页面

<form id="myForm" action="page2.php" method="post">
<label for="name">a label:</label><input type="submit" name="SubmitCar" value="Done" id="fbutton" /> <br />
<br />
<select name="selectCar">
<?php
session_start(); 

$user = "cardatabase";
$password = "";
$host = "";
$database = "my_cardatabase";
$connessione = mysql_connect($host, $user, $password) or die( mysql_error() . " <br/> could not connect to server");
mysql_select_db($database, $connessione) or die( mysql_error() . " <br/> could not connect datbase");

$id = $_SESSION['myid'];

         $query = "select IDCar
                       from Car";

         $result = mysql_query($query, $connessione) or die(mysql_error());
         if( mysql_num_rows($result) > 0 )  {
              $array = array();
              $i = 0;
              while ($row = mysql_fetch_array($result)) {

                $array[$i] = $row['IDCar'];
                ++$i;
                }

              for ($i = 0; $i < count($array); ++$i) {
                    echo "<option value='$array[$i]'>$array[$i]</option>";
                 }
            }
mysql_close();
?>
</select>
</form>

Simply fill the select box from DB. 只需填写数据库中的选择框。 Now here's the problem. 现在是问题所在。 When i reach page2.php i need the value of the select box and i tried this 当我到达page2.php时,我需要选择框的值,然后我尝试了

page2.php page2.php

<?php
$value = $_POST['selectCar'];
?>

But it's not working, so i tried to use sessions in this way 但是它不起作用,所以我尝试以这种方式使用会话

page1.php page1.php

</form>

    <?php
    if(isset($_POST['SubmitCar'])){
    $_SESSION['idAuto'] = $_POST['selectCar'];
    }
    ?>

out of the form, but still not working. 格式不正确,但仍然无法正常工作。 What can i do to get this value in page2.php?? 我该怎么做才能在page2.php中获得此值?

Try using this code to build your <option> , and use mysqli when working with the database 尝试使用此代码构建<option> ,并在使用数据库时使用mysqli

 $result = mysqli_query($connessione, $query) or die(mysql_error());
         if( mysqli_num_rows($result) > 0 )  {
             $i=0;
              while ($row = mysqli_fetch_row$result)) {

                    echo "<option value='".$row[$i]."'>".$row[$i]."</option>";
                    $i++;
                 }
            }

Also, it's not indicated to have mysql tables with names in uppercase , or fields with names in uppercase . 另外,它不表示具有名称uppercase mysql表或名称uppercase字段。

Page2.php Page2.php

You forgot to echo it. 你忘了回声。

<?php
$value = $_POST['selectCar'];
echo $value;
?>

I found a simple workaround for my problem. 我找到了解决问题的简单方法。 It might not be perfect but it works. 它可能并不完美,但可以正常工作。

  1. i deleted the action="page2.php" in the form of page1 我删除了page1形式的action="page2.php"
  2. in page1.php add this code outside the form 在page1.php中,将此代码添加到表单外部

This will do the trick 这将解决问题

        if(isset($_POST['SubmitCar'])){
        $_SESSION['idCar'] = $_POST['selectCar'];
        header('Location:page2.php');
        }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM