Copy link to clipboard
Copied
Hi everyone!
I'm trying to get myself into web development and i'm pretty new to this so i apologize in advance for any stupid mistake haha.
I'm practicing how to create forms using PHP and i keep getting an error when i use the POST method when submitting information. Here is my first PHP file (form.php) that has the HTML content:
<html>
<body>
<form method="post" action="form_submission.php">
<p>Name: <input name="name" type="text" /> </p>
<p>Size: <select name="size">
<option value="small">Small</option>
<option value="medium">Medium</option>
<option value ="large">Large</option>
</select></p>
<p>Sex: <input type="radio" name="sex" value="female"/> Female
<input type="radio" name="sex" value="male" /> Male</p>
<input type="submit" name="submit" value="Submit!" />
</form>
</body>
</html>
and here is the code for the form_submission.php file:
<?PHP
if(isset($_POST['submit'])){
$name = $_POST['name'];
$size = $_POST['size'];
$sex = $_POST['sex'];
echo "<p>Name: $name <br> Size: $size <br> Sex: $sex </p>";
}
?>
**SIDENOTE: i have another problem with the echo statement in live view and i don't think it's processing properly; it outputs the last five characters ";}?> at the end and it doesn't register the " as the end of the echo.
When i run the form.php and fill it out i get:
{"code":"MethodNotAllowedError","message":"POST is not allowed"}
Your help is greatly appreciated and any pointers are welcomed
Dreamweaver doesnt have a local server. If you are testing php locally you need to download a free one like Wamp or Xampp or Mamp.
Do you have a remote server capable of running php that you can upload the files to to test them out?
Copy link to clipboard
Copied
You dont need to surround the php processing code in brackets to see if the 'submit' button has been set. That really only applies if the php code is included in the same file as your form code. Its intention is to stop the php running until the 'submit' button has been pressed. As you have the php code in another file it cant run. It only runs when the form gets sent to the processing file via the forms 'action' attribute.
See if that fixes your problems
Copy link to clipboard
Copied
i removed it and made the code as follows:
<?PHP
$name = $_POST['name'];
$size = $_POST['size'];
$sex = $_POST['sex'];
echo "<p>Name: $name <br> Size: $size <br> Sex: $sex </p>";
?>
but i still get the same error message when i submit the form.php
i'm using Dreamweaver's local server so i don't think that's part of the problem, but looking at how simple the block of code is i don't think the flaw is in the code.
Copy link to clipboard
Copied
Dreamweaver doesnt have a local server. If you are testing php locally you need to download a free one like Wamp or Xampp or Mamp.
Do you have a remote server capable of running php that you can upload the files to to test them out?
Copy link to clipboard
Copied
I already had MAMP set up but didnt realize that i need to save my php files in the htdocs directory, rookie mistake haha i moved the files and that fixed both issues (the error and the echo).
thanks a lot for your help!
Copy link to clipboard
Copied
Ok have fun, php opens up a whole new world.