Jump to content

PHP cann't post in contact form

cakez
Go to solution Solved by tatte,

I'm not familiar with Brackets, but it seems to be nothing but a run of the mill editor. You need to run something like XAMPP to run PHP scripts in your local machine. I'm assuming you have a web hosting where you plan on putting that form when you're done? Have you tried if it works there?

 

Internet is full of bots, and most bots love to spam. If you publish a form like that, you are going to receive a lot of spam. That's what CAPTCHA is for. I would also suggest checking if the form.php actually receives any post, so it doesn't try to send you something each time the file is directly loaded. Internet is a vile place.

I'm building a contact form, but when I press submit/send, it says cannot post /form.php. Can anyone help me fix this?

HTML

 <form name="form" method="post" class="contact" action="form.php">
            <label for="name">Name</label><br>
            <input class="text" name="name" type="text" />
            <br><br>
            
            <label for="email">Email</label><br>
            <input class="text" name="email" type="text" />
            <br><br>
            
            <label for="message">Message</label><br>
            <textarea class="message" name="message"></textarea>
            <br><br>
            
            <input class="submit " name="submit" type="submit"  value="send"/>
            
        </form>

PHP

<?php
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];

$to = 'jsmith@gmail.com'
$subject ='Website User'
$msg = $name . " " . $email . " " . $message;

mail($to, $subject, $msg);

header('Location: Contact.html');
exit;
?>

Thanks

Link to comment
Share on other sites

Link to post
Share on other sites

Does it give you a 404 error?

                     ¸„»°'´¸„»°'´ Vorticalbox `'°«„¸`'°«„¸
`'°«„¸¸„»°'´¸„»°'´`'°«„¸Scientia Potentia est  ¸„»°'´`'°«„¸`'°«„¸¸„»°'´

Link to comment
Share on other sites

Link to post
Share on other sites

1 minute ago, vorticalbox said:

Does it give you a 404 error?

It says

Cannot POST /form.php

 

Link to comment
Share on other sites

Link to post
Share on other sites

Is your PHP file called `form.php`? Are you running on an Apache server? Give us a screenshot of the exact error on your browser or something.

Link to comment
Share on other sites

Link to post
Share on other sites

1 minute ago, Mauv said:

Is your PHP file called `form.php`? Are you running on an Apache server? Give us a screenshot of the exact error on your browser or something.

yes the php file is called form.php

screenshot

Link to comment
Share on other sites

Link to post
Share on other sites

2 minutes ago, cakez said:

yes the php file is called form.php

screenshot

And are they in the same folder? Usually you get a "Cannot post" when it won't find the file specified in the form action. Or for some reason you have a router (which probably is not your case) made in PHP or something that blocks only for a specific method.

Link to comment
Share on other sites

Link to post
Share on other sites

1 minute ago, Mauv said:

And are they in the same folder? Usually you get a "Cannot post" when it won't find the file specified in the form action. Or for some reason you have a router (which probably is not your case) made in PHP or something that blocks only for a specific method.

 

Yep, same folder.In fact next to each other

Link to comment
Share on other sites

Link to post
Share on other sites

Aside from missing semicolons (lines 6 and 7 in form.php) your code is fine, tried it in XAMPP.

 

Problem thereby most likely lies in your environment. What exactly are you running? (Please don't make us ask for a third time.)

 

Unprotected form like this is a bad idea, but that's none of my business. 9_9

Link to comment
Share on other sites

Link to post
Share on other sites

2 minutes ago, tatte said:

Aside from missing semicolons (lines 6 and 7 in form.php) your code is fine, tried it in XAMPP.

 

Problem thereby most likely lies in your environment. What exactly are you running? (Please don't make us ask for a third time.)

 

Unprotected form like this is a bad idea, but that's none of my business. 9_9

 

What do you mean by "what am I running?"I'm using Brackets to code and I'm using the built-in live preview to view the site. Also, I'm pretty new to php, so what's unprotected form?

Link to comment
Share on other sites

Link to post
Share on other sites

I'm not familiar with Brackets, but it seems to be nothing but a run of the mill editor. You need to run something like XAMPP to run PHP scripts in your local machine. I'm assuming you have a web hosting where you plan on putting that form when you're done? Have you tried if it works there?

 

Internet is full of bots, and most bots love to spam. If you publish a form like that, you are going to receive a lot of spam. That's what CAPTCHA is for. I would also suggest checking if the form.php actually receives any post, so it doesn't try to send you something each time the file is directly loaded. Internet is a vile place.

Link to comment
Share on other sites

Link to post
Share on other sites

Seems like Brackets has some build in web server thingy?

 

I noticed to missed some ending ; in your php

 

<?php
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];

$to = 'jsmith@gmail.com';   //Missed it here
$subject ='Website User';	//And missed it here
$msg = $name . " " . $email . " " . $message;

mail($to, $subject, $msg);

header('Location: Contact.html');
exit;
?>

Your form looks okay i guess, if it isnt working try to use a web server that isnt build in a text editor. Like wamp.

 

Quote or mention me if not feel ignored 

Link to comment
Share on other sites

Link to post
Share on other sites

1 minute ago, tatte said:

I'm not familiar with Brackets, but it seems to be nothing but a run of the mill editor. You need to run something like XAMPP to run PHP scripts in your local machine. I'm assuming you have a web hosting where you plan on putting that form when you're done? Have you tried if it works there?

 

Internet is full of bots, and most bots love to spam. If you publish a form like that, you are going to receive a lot of spam. That's what CAPTCHA is for. I would also suggest checking if the form.php actually receives any post, so it doesn't try to send you something each time the file is directly loaded. Internet is a vile place.

 

Thanks

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×