为什么html输入提交没有分配$ _POST数组值

时间:2016-06-27 08:30:31

标签: php html mysql

请检查代码的最后一部分,其中input type =“submit”name =“save_changes_submit”。此表单未在$ _POST中分配。我在做什么错了?我在这里做错了吗?如何从此提交中获得$ _POST值?

echo '<form action="" method="post">ID Number:<input type="number" name="id_number" value="number"><br><br><input type="submit" value="Search Product" name="id_number_submit">';

if (isset($_POST['id_number_submit'])) {
    $id_number = $_POST['id_number'];

    $q = mysqli_query($conn, "SELECT * FROM product WHERE id='$id_number'");

    $row = mysqli_fetch_array($q);

    if (empty($row)) {
        echo "Error: Invalid product id";
    }else{

    $product_name = $row['product_name'];
    $product_des = $row['product_des'];
    $product_price = $row['product_price'];


echo '<br><br><input type="number" name="product_price" value="'.$product_price.'">';
echo '<br><br><input type="text" name="product_name" value="'.$product_name.'">';
echo '<br><br><textarea rows="8" cols="100" name="product_des">'.$product_des.'</textarea><br><input type="submit" value="Save Changes" name="save_changes_submit">';

if (isset($_POST['save_changes_submit'])) {
    echo $c_product_price = $_POST['product_price'];
    echo $c_product_name = $_POST['product_name'];
    echo $c_product_des = $_POST['product_des'];
}
}
}

echo '</form>';

3 个答案:

答案 0 :(得分:1)

这是因为这种情况if (isset($_POST['id_number_submit']))!单击save_changes_submit提交按钮时,POST数据中不包含id_number_submit。因此,它不会运行您的条件。更改条件的解决方案

echo '<form action="" method="post">ID Number:<input type="number" name="id_number" value="number"><br><br><input type="submit" value="Search Product" name="id_number_submit">';

if (isset($_POST['id_number_submit'])) {
    $id_number = $_POST['id_number'];

    $q = mysqli_query($conn, "SELECT * FROM product WHERE id='$id_number'");

    $row = mysqli_fetch_array($q);

    if (empty($row)) {
        echo "Error: Invalid product id";
    }
    else{

    $product_name = $row['product_name'];
    $product_des = $row['product_des'];
    $product_price = $row['product_price'];


echo '<br><br><input type="number" name="product_price" value="'.$product_price.'">';
echo '<br><br><input type="text" name="product_name" value="'.$product_name.'">';
echo '<br><br><textarea rows="8" cols="100" name="product_des">'.$product_des.'</textarea><br><input type="submit" value="Save Changes" name="save_changes_submit">';    

   }
}
if (isset($_POST['save_changes_submit'])) {
    $c_product_price = $_POST['product_price'];
    $c_product_name = $_POST['product_name'];
    $c_product_des = $_POST['product_des'];
    echo $c_product_price;
    echo $c_product_name;
    echo $c_product_des;
}

echo '</form>';

答案 1 :(得分:1)

if条件if(isset($_POST['save_changes_submit']))位于if(isset($_POST['id_number_submit']))内。这就是问题所在。在这里。

<?php
echo '<form action="" method="post">ID Number:<input type="number" name="id_number" value="number"><br><br><input type="submit" value="Search Product" name="id_number_submit">';

    if(isset($_POST['id_number_submit'])) {
        $id_number = $_POST['id_number'];

        $q = mysqli_query($conn, "SELECT * FROM product WHERE id='$id_number'");

        $row = mysqli_fetch_array($q);

        if (empty($row)){
            echo "Error: Invalid product id";
        }else{
            $product_name = $row['product_name'];
            $product_des = $row['product_des'];
            $product_price = $row['product_price'];


        echo '<br><br><input type="number" name="product_price" value="'.$product_price.'">';
        echo '<br><br><input type="text" name="product_name" value="'.$product_name.'">';
        echo '<br><br><textarea rows="8" cols="100" name="product_des">'.$product_des.'</textarea><br><input type="submit" value="Save Changes" name="save_changes_submit">';
        }
    }
    if(isset($_POST['save_changes_submit'])) {
        echo $c_product_price = $_POST['product_price'];
        echo $c_product_name = $_POST['product_name'];
        echo $c_product_des = $_POST['product_des'];
    }
    echo '</form>';
?>

答案 2 :(得分:0)

因为您没有指定要使用表单处理哪个php文件。更改此表单的操作:

echo '<form action="<?php echo $_SERVER['PHP_SELF'];?>" method="post">ID Number:<input type="number" name="id_number" value="number"><br><br><input type="submit" value="Search Product" name="id_number_submit">';