$ _POST没有为PHP脚本中的表单运行

时间:2015-03-09 05:02:13

标签: php html

我在form脚本中制作PHP当我尝试访问某个字段时 form在同一个脚本中它给了我错误未定义索引

这是我的代码:

<?php
    $query="SELECT * FROM `vendor_list`";
    $query_run=mysql_query($query);

    if(mysql_num_rows($query_run)>0)
    {
        echo '<form method="POST">';
        echo 'Book name:<input type="text" name="book_name"  size="40"  maxlength="40"><br><br>';
        echo 'Number Of Copies:<input type="text" size="5" name="num_copies" maxlength="2"><br><br>';

        echo '<select name="vendor_email">';
        while($query_row=mysql_fetch_assoc($query_run))
        {
            $laser=$query_row['vendor_name'];
            $email=$query_row['email'];
            echo "<option value='".$email."'>".$laser."</option>";
        }
        echo '</select><br>';
        echo '<input type="submit" value="submit">';
        echo '</form>';
    }
    else
    {
        echo 'No vendor to display';
    }

    echo $_POST['book_name'];

4 个答案:

答案 0 :(得分:1)

使用它: -

    <?php
        $query="SELECT * FROM `vendor_list`";
        $query_run=mysql_query($query);

        if(mysql_num_rows($query_run)>0)
        {
            echo '<form method="POST">';
            echo 'Book name:<input type="text" name="book_name"  size="40"  maxlength="40"><br><br>';
            echo 'Number Of Copies:<input type="text" size="5" name="num_copies" maxlength="2"><br><br>';

            echo '<select name="vendor_email">';
            while($query_row=mysql_fetch_assoc($query_run))
            {
                $laser=$query_row['vendor_name'];
                $email=$query_row['email'];
                echo "<option value='".$email."'>".$laser."</option>";
            }
            echo '</select><br>';
            echo '<input type="submit" value="submit">';
            echo '</form>';
        }
        else
        {
            echo 'No vendor to display';
        }


if($_POST){
 echo $_POST['book_name'];

}
    ?>

答案 1 :(得分:0)

试试这个

    <?php
            $query="SELECT * FROM `vendor_list`";
            $query_run=mysql_query($query);

            if(mysql_num_rows($query_run)>0)
            {
?>
                <form method="POST" action="">
                Book name:<input type="text" name="book_name"  size="40"  maxlength="40"><br><br>
                Number Of Copies:<input type="text" size="5" name="num_copies" maxlength="2"><br><br>

                <select name="vendor_email">
<?php
                while($query_row=mysql_fetch_assoc($query_run))
                {
                    $laser=$query_row['vendor_name'];
                    $email=$query_row['email'];
                    echo "<option value='".$email."'>".$laser."</option>";
                }
?>
                </select><br>
                <input type="submit" name="submit" value="submit">
                </form>
<?php
            }
            else
            {
                echo 'No vendor to display';
            }


    if(isset($_POST["submit"]))){
     echo $_POST['book_name'];

    }
        ?>

答案 2 :(得分:0)

使用下面的代码,这也将检查你是否有表单提交,除非这个代码是剪切/复制粘贴..看起来它将处理不应该的部分..例如echo post ..

就我个人而言,我自己会设定行动=&#34;&#34;许多年前,PHP_SELF遇到了问题,很快,当我的跨浏览器足够/较旧的用户更新浏览器时,我会开始使用HTML5。

处理或显示帖子但不是两者的示例。     

        echo $_POST['book_name'];
        echo "POST Dump: " . print_r($_POST, true);
    } else {

        // No Form Submission, So we generate the form instead
        // QUERY - data
        $query = "SELECT * FROM 'vendor_list'";
        $query_run = mysql_query($query);

        if(mysql_num_rows($query_run)>0) {
            // FORM - Name/#Copies
            echo '<form method="POST" action="' . $_SERVER['PHP_SELF'] . '">';
            echo 'Book name:<input type="text" name="book_name"  size="40"  maxlength="40"><br><br>';
            echo 'Number Of Copies:<input type="text" size="5" name="num_copies" maxlength="2"><br><br>';

            // FORM - Select Vendor
            echo '<select name="vendor_email">';
            while($query_row=mysql_fetch_assoc($query_run))
            {
                $laser=$query_row['vendor_name'];
                $email=$query_row['email'];
                echo "<option value='".$email."'>".$laser."</option>";
            }
            echo '</select><br>';

            // FORM - Submit / Close Form
            echo '<input type="submit" value="submit">';
            echo '</form>';
        } else {
            echo 'No vendor to display';
        }

    }
?>

或使用显示表单并处理任何$ _POST

的代码
<?php
    // QUERY - data
    $query = "SELECT * FROM 'vendor_list'";
    $query_run = mysql_query($query);

    if(mysql_num_rows($query_run)>0) {
        // FORM - Name/#Copies
        echo '<form method="POST" action="' . $_SERVER['PHP_SELF'] . '">';
        echo 'Book name:<input type="text" name="book_name"  size="40"  maxlength="40"><br><br>';
        echo 'Number Of Copies:<input type="text" size="5" name="num_copies" maxlength="2"><br><br>';

        // FORM - Select Vendor
        echo '<select name="vendor_email">';
        while($query_row=mysql_fetch_assoc($query_run))
        {
            $laser=$query_row['vendor_name'];
            $email=$query_row['email'];
            echo "<option value='".$email."'>".$laser."</option>";
        }
        echo '</select><br>';

        // FORM - Submit / Close Form
        echo '<input type="submit" value="submit">';
        echo '</form>';
    } else {
        echo 'No vendor to display';
    }

    if (isset($_POST['book_name'])) {
        echo "<b>Displaying post data</b> <br />";
        echo "Example book: " . $_POST['book_name'] . "<br />";
        echo "POST Dump: <br /><pre>" . print_r($_POST, true) . "</pre>";
    } else {
        echo "Page has not been subitted yet, please select an item";
    }
?>

答案 3 :(得分:0)

清洁方式

你在该代码中遇到的所有休息都是一团糟......

提示当您回显太多HTML时,只需将其包装在if..else中..就像这样......

<?php
  $query = "SELECT * FROM `vendor_list`";
  $query_run = mysql_query($query);
?>

<?php if(mysql_num_rows($query_run)>0): ?>
  <form method="POST">
    <label for="book_name">Book name:</label>
    <input type="text" name="book_name" id="book_name" size="40"  maxlength="40">
    <label for="book_name"Number Of Copies:</label>
    <input type="text" size="5" name="num_copies" maxlength="2">
    <select name="vendor_email">
      <?php while($query_row=mysql_fetch_assoc($query_run)): ?>
          <option value="<?=$query_row['email']?>"><?=$query_row['vendor_name']?></option>
      <?php endwhile; ?>
    </select>
    <input type="submit" value="submit">
  </form>
<?php else: ?>
    <p>No vendor to display</p>
<?php endif; ?>

<?php  if (isset($_POST['book_name'])) echo $_POST['book_name']; ?>