简体   繁体   中英

isset function not working with form submit and database insert statment

I have 2 pages and there is a form in each page. the first form accepts user input and preview it on the second page (hidden form elements). on the second form the user clicks the submit button and it insert that data into mysql.

The problem, the second page insert data into mysql once I get to that page via the form POST method and I get empty fields in the database and the isset function for the form submit button not working.

First page (form):

<html>
    <body>    
        <form action="review.php" method="post">
            Name: <input type="text" name="name"><br>
            Age: <input type="text" name="age"><br>
            <input type="button" name="submit" value="submit">
        </form>    
    </body>
</html>

I get to the second page for review called review.php with this code:

<?php
if (isset($_POST['submit'])) {
    // Mysql insert statment
    echo "Form has been submitted";
}    
?>

<html>
    <body>    
        <form action="review.php" method="post">
            Name: <input type="hidden" name="name"><p><?php echo $_POST['name']; ?></p><br>
            Age: <input type="hidden" name="age"><p><?php echo $_POST['age']; ?></p><br>
           <input type="submit" name="submit">
        </form>    
    </body>
</html>

Once I click on the review button from the first form, it inserts that into mysql, so the isset function on the review.php is not working.

因为您错过了第一个文件中的这一行

<input type="submit" name="submit" value="Review">
if (isset($_POST['submit'])) { //but there isn't any Html attribute with the name submit

It should be like this:

<input type="submit" name="submit"/>

Now you won't get empty values

将下面的代码放在您的第一页中

<input type="submit" value="submit" name="submit" />

In first page

Use:

<input type="submit" name='submit' value='Submit'>

Instead of

<input type="review">

fine**fine

    -


**They must be separated from surrounding text by blank lines. The begin and end tags of the outermost block element must not be indented. Markdown can't be used within HTML blocks.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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