I'm trying to add a contact form to my website but each time I click send it takes me to a black window and the address bar says contact.php. Also it doesn't redirect back.
My website is hosted as an application using the google app engine.
I tried literally everything but I can't get it to work.
Below is my HTML code:
<form action="contact.php" method="post">
<p>Your name</p>
<input type="text" name="name"/>
<p>Your e-mail</p>
<input type="text" name="email"/>
<p>Message</p>
<textarea name="message"></textarea>
<input type="submit" value="Send"/>
<input type="reset" value="Clear"/>
</form>
and here is my PHP code:
<?php
$field_name = $_POST['name'];
$field_email = $_POST['email'];
$field_message = $_POST['message'];
$mail_to = 'email#here.com';
$subject = 'Message from a site visitor '.$field_name;
$body_message = 'From: '.$field_name."\n";
$body_message .= 'E-mail: '.$field_email."\n";
$body_message .= 'Message: '.$field_message;
$headers = 'From: '.$field_email."\r\n";
$headers .= 'Reply-To: '.$field_email."\r\n";
$mail_status = mail($mail_to, $subject, $body_message, $headers);
if ($mail_status) { ?>
<script language="javascript" type="text/javascript">
alert('Thank you for the message.');
window.location = 'contact.html';
</script>
<?php
}
else { ?>
<script language="javascript" type="text/javascript">
alert('Message failed.');
window.location = 'contact.html';
</script>
<?php
}
?>
You need a mail server, PHP doesn't sent mails by itself. Install one first. And yea, text area is missing close tag.
You're missing the closing textarea tag in your form. I tried the code as contact.php on my server and it works. I agree with itachi, you need to turn on error reporting.
Why not make contact.php a full page and get rid of the redirect altogether?
Working fine for me. Your if else condition is not working properly if fields has no values then still it gives thank you message.
Related
So I have published my website, but am making minor improvements to it as time progresses. Just a note: I am impatient and this is making me want to obliterate myself. Help!
So the question is the title. I have a contact form on my website which uses HTML and PHP.
Here is the form on HTML:
<form action="contact.php" method="post">
Name<br>
<input type="text" name="cf_name"><br>
E-Mail<br>
<input type="text" name="cf_email"><br>
Message<br>
<textarea name="cf_message"></textarea></br>
<input type="submit" value="Send">
<input type="reset" value="Clear">
</form>
And here is the PHP for it:
<?php
$field_name = $_POST['cf_name'];
$field_email = $_POST['cf_email'];
$field_message = $_POST['cf_message'];
$mail_to = 'amir#arshak.co.uk';
$subject = 'ALERT! Somebody has viewed your website!'.$field_name;
$body_message = 'From: '.$field_name."\n";
$body_message .= 'E-mail: '.$field_email."\n";
$body_message .= 'Message: '.$field_message;
$headers = 'From: '.$field_email."\r\n";
$headers .= 'Reply-To: '.$field_email."\r\n";
$mail_status = mail($mail_to, $subject, $body_message, $headers);
if ($mail_status) { ?>
<script language="javascript" type="text/javascript">
alert('Thanks. I have recieved your message. I may reply to the given e-mail.');
window.location = 'contact.html';
</script>
<?php
}
else { ?>
<script language="javascript" type="text/javascript">
alert('Sorry, your message failed to send. Send an email to amir#arshak.co.uk');
window.location = 'contact.html';
</script>
<?php
}
?>
I can't figure out what is wrong with the code!
If the problem is that the email won't send...
You are allowing the from and reply-to addresses to be set to whatever is entered into the form.
A lot of mail servers will only allow an email to be sent where the from address matches the email address used to authenticate with the server.
This could possibly be causing your issue although it's hard to say without much more information.
I have tried looking in similar questions posted here, but cannot find an answer (got about 1900 questions popping up).
I have a contact form on a page at a site I am creating on the webpage hosting site
https://www.one.com/en/
Here is the form on the page contact/contact.shtml
<form action="contact/mail.php" method="post"><br>
Your name<br>
<input type="text" name="cf_name"><br>
Your e-mail<br>
<input type="text" name="cf_email"><br>
Message<br>
<textarea name="cf_message"></textarea><br>
<input type="submit" value="Send">
<input type="reset" value="Clear">
</form>
Here is the content of the contact/mail.php file
<?php
$field_name = $_POST['cf_name'];
$field_email = $_POST['cf_email'];
$field_message = $_POST['cf_message'];
$mail_to = 'clashton#gmail.com';
$subject = ' Contact Form '.$field_name;
$body_message = 'From: '.$field_name."\n";
$body_message .= 'E-mail: '.$field_email."\n";
$body_message .= 'Message: '.$field_message;
$headers = 'From: '.$field_email."\r\n";
$headers .= 'Reply-To: '.$field_email."\r\n";
$mail_status = mail($mail_to, $subject, $body_message, $headers);
if ($mail_status) { ?>
<script language="javascript" type="text/javascript">
alert('Thank you for the message. We will contact you shortly.');
window.location = 'contact.shtml';
</script>
<?php
}
else { ?>
<script language="javascript" type="text/javascript">
alert('Message failed. Please, send an email to clashton#gmail.com');
window.location = 'contact.shtml';
</script>
<?php
}
?>
When I try to send a message a window pops up containing the error message "Message failed. Please, send an email to clashton#gmail.com". In other words the "else" action is happening.
If it seems that this question has a trivial answer, please keep in mind that I am completely new at this.
Thanks for any help.
This question already has answers here:
PHP code is not being executed, but the code shows in the browser source code
(35 answers)
Closed 7 years ago.
I'm completely new to HTML and PHP and am trying to build a simple contact form, so that users can send me a message on the contact page, which then gets directed to my email. Any ideas what I'm doing wrong here:
contact.html:
<section id="secondary">
<form action="contact.php" method="post" class="contactform">
Your Name<br>
<input type="text" maxlength="100" name="cf_name" style="width: 300px" class="contactboxes"><br>
Your E-mail<br>
<input type="text" maxlength="100" name="cf_email" style="width: 300px" class="contactboxes"><br>
The Reason for Contact<br>
<textarea name="cf_message" maxlength="500" style="width: 300px" class="contactboxes"></textarea><br>
<input type="submit" value="Send" class="sendclearbutton">
<input type="reset" value="Clear" class="sendclearbutton">
</form>
</section>
contact.php:
<?php
$field_name = $_POST['cf_name'];
$field_email = $_POST['cf_email'];
$field_message = $_POST['cf_message'];
$mail_to = 'sharan#hotmail.co.uk';
$subject = 'Message from a site visitor '.$field_name;
$body_message = 'From: '.$field_name."\n";
$body_message .= 'E-mail: '.$field_email."\n";
$body_message .= 'Message: '.$field_message;
$headers = 'From: '.$field_email."\r\n";
$headers .= 'Reply-To: '.$field_email."\r\n";
$mail_status = mail($mail_to, $subject, $body_message, $headers);
if ($mail_status) { ?>
<script language="javascript" type="text/javascript">
alert('Thank you for the message. We will contact you shortly.');
window.location = 'contact.html';
</script>
<?php
}
else { ?>
<script language="javascript" type="text/javascript">
alert('Message failed. Please, send an email to sharan#hotmail.co.uk.');
window.location = 'contact.html';
</script>
<?php
}
?>
It looks fine to me, as Lance said, the only reason I can think that it wont be working is that you don't have PHP installed. Download Apache or whatever suits your computer best or use web hosting online. Do say if other code is working, though.
I'm trying to get a 'contact us email form' to work on my website but when I press the 'send' button, it doesnt send the email and it shows the entire contents of the .php file thats supposed to make it send? Can anybody help me please? my html page i called 'Contact_Us.html' and the PHP file is called 'contact.php".
Here is the HTML
<form action="contact.php" method="post">
Your name<br>
<input type="text" name="cf_name"><br>
Your e-mail<br>
<input type="text" name="cf_email"><br>
Message<br>
<textarea name="cf_message"></textarea><br>
<input type="submit" value="Send">
<input type="reset" value="Clear">
</form>
and here is the .PHP
<?php
$field_name = $_POST['cf_name'];
$field_email = $_POST['cf_email'];
$field_message = $_POST['cf_message'];
$mail_to = 'user#domain.com';
$subject = 'Message from a visitor on The New Moston Club Wesbite '.$field_name;
$body_message = 'From: '.$field_name."\n";
$body_message .= 'E-mail: '.$field_email."\n";
$body_message .= 'Message: '.$field_message;
$headers = 'From: '.$field_email."\r\n";
$headers .= 'Reply-To: '.$field_email."\r\n";
$mail_status = mail($mail_to, $subject, $body_message, $headers);
if ($mail_status) { ?>
<script language="javascript" type="text/javascript">
alert('Thank you, we have received your message. We will contact you shortly!');
window.location = 'Contact_Us.html';
</script>
<?php
}
else { ?>
<script language="javascript" type="text/javascript">
alert('Message failed. Please, send an email to thenewmostonclub#outlook.com sorry for any inconvenience');
window.location = 'Contact_Us.html';
</script>
<?php
}
?>
does it show in your browsers URL bar c:/user/.../yourscript.php if yes then try localhost/yourscript.php
I created a form that is linked to a PHP page like this:
<form method="get" action="contact.php">
<p>Nome:</p> <input type="text" name="name" class="name" /><br>
<p>E-mail :</p> <input type="text" name="mail" class="mail" /><br>
<p>Website:</p> <input type="text" name="website" class="website" /><br>
Mensagem: <textarea name="message" class="message" rows="2" cols="20"></textarea>
<input type="submit" value="Enviar" class="submit" />
</form>
The PHP page:
<?php
$field_name = $_GET['name'];
$field_email = $_GET['mail'];
$field_website = $_GET['website'];
$field_message = $_GET['message'];
$mail_to = '__munged__#gmail.com';
$subject = 'Message from a site visitor ' . $field_name;
$body_message = 'From: '.$field_name."\n";
$body_message .= 'E-mail: '.$field_email."\n";
$body_message .= 'Message: '.$field_message;
$headers = "From: $field_email\r\n";
$headers .= "Reply-To: $field_email\r\n";
$mail_status = mail($mail_to, $subject, $body_message, $headers);
if ($mail_status) { ?>
<script language="javascript" type="text/javascript">
alert('Thank you for the message. We will contact you shortly.');
window.location = 'index.html';
</script>
<?php
}
else { ?>
<script language="javascript" type="text/javascript">
// Print a message
alert('Message failed. Please, send an email to __munged__#template-help.com');
window.location = 'index.html';
</script>
<?php
}?>
I can't seem to figure out the problem. Thanks in advance.
If you had access to the web host I would say try to find the apache2.log file on an Ubuntu server this is located in /var/log/ but I don't know where this would be for you. Anyways, if you find this you can look at the last records in the log to see if apache is throwing an error during the execution of this command that is otherwise hidden from the user. It sounds like there is probably a problem with the mail server. Do you have one setup? are you using sendmail or some variant?
I recently had some related problems with sending mail, this might help you (but as I said before it is for linux)
php5 mail() function sendmail error
Check the smtp server settings in [mail function] section of php.ini file. The mail server probably received the message successfully otherwise it would return an error. I would try to use different mail server.
Its a simple method to do.
Remove Unused Spaces from subject.
Do Not use dynamic variable with subject like $field_name.
use php5 #mail function