简体   繁体   中英

How to POST HTML/PHP form? (My HTML/PHP form is not POSTing)

so I started learning PHP today and wanted to start with some form posting stuff. So I followed couple tutorials and found out my code isn't working and I'm unable to find the issue

My code:

<form action="test.php" method="post">
    <input type="text" name="column"><br>
    <input type="submit" name="submit" value="Query!">
</form>

<?php
    if(isset($_POST["submit"])){
        echo "<script> alert('Works!'); </script>";
    }else{print_r($_POST);}
?>

It does nothing, it doesn't do the alert and the printed _POST is just an empty array Maybe it's just a simple mistake and I'm just unable to see

The $_POST["submit"], is the POST variable submit . You would want to make it $_POST["column"] and put your value in there. Your button type='submit' should not have a value. This is assuming this file is called test.php. You can make it, if it is the same file, it will work either way.

<form action="test.php" method="post">
    <input type="text" name="column" value="Query!"><br>
    <input type="submit" name="submit">
</form>

<?php
    if(isset($_POST["column"])){
        echo "<script> alert('Works!'); </script>";
    }else{print_r($_POST);}
?>

I ran your code on my pc and what I discovered is that the print_r($_POST) brings out an empty array because you are not inserting anything. Remove the alert and else statement, you don't need it for this code execution.

<?php
    if(isset($_POST['submit']){
     $name=$_POST['column'];//['column'] is the name assigned to the input field in the form below.
    
    print_r($_POST);
    }
     
 ?>


<form action="test.php" method="POST">
<input type="text" name="column"></br>
<input type="submit" name="submit" value="Query!">

 </form>

The code above should give you this output: Array ( [column] => whatever value you inserted in the text field [submit] => Query! )

It is also good practice to put your php code at the top because most programs run from the top down. Imagine you had errors(echo $error;), you want to display in the form, if php is below you will get undefined variable.

Well, except in cases where you are including footer.php pages which you will learn later since you are new to PHP.

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