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
2
Answers
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.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.
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.