I am trying to make simple tabs. I am having trouble with the contact us tab. I keep getting a 404 error saying the page can not be found. I have copied some supposed to be working examples from online. I know the page exists because it is a email and i emailed it from my other email. This is my first time building a site. I am putting my PHP inside my html. I have tried making my file index.html and index.php and neither worked. It don't matter to me how i do it, ajax or php. I know i did not need to use jQuery to do what i am trying to do, but it was just easier for me. Thanks again for your help, it is much appreciated!
jQuery
$("#phillya11").click(function () {
$("#soon4").show();
$("#soon").hide();
$("#soon1").hide();
$("#soon2").hide();
$("#soon3").hide();
$('#close4').click(function () {
$("#soon4").hide();
});
});
HTML
<div id="phillya11"><a href="#"
style="text-decoration:none;color: #FFF5ee">soon4</a> </div>
</div>
<div id="soon4">
<form action="mail.php" method="POST">
<p>Name</p> <input type="text" name="name">
<p>Email</p> <input type="text" name="email">
<p>Message</p><textarea name="message" rows="6" cols="25"></textarea><br />
<input type="submit" value="Send"><input type="reset" value="Clear">
</form>
</div>
PHP
<?php $name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$formcontent="From: $name \n Message: $message";
$recipient = "myemail#myemail";
$subject = "Contact Form";
$mailheader = "From: $email \r\n";
mail($recipient, $subject, $formcontent, $mailheader) or die("Error!");
echo "Thank You!";
?>
CSS
#soon4{
display:none;
width:380px;
margin:25px;
height: 346px;
font-style: normal;
font-weight: 500;
font-size:1em;
background-color:#F10B78
}
#close4{
color: white;
margin:90px
}
Where are you putting your PHP? In the index.html file or a separate file?
If you note on your form tag, you've defined the action as mail.php, which is a file that must exist for the form to post it's data to. This is probably where the 404 is coming from.
You can either leave your PHP in the index page (make sure you change it to index.php) and change the form action to index.php, or move your PHP code to a separate mail.php file.
Try to add the event.preventDefault() in your click function.
$("#phillya11").click(function (event) {
event.preventDefault()
$("#soon4").show();
//....
});
http://api.jquery.com/event.preventDefault/
Related
I have a few simple html websites with a contact form that sends emails via PHP. They all works great with no issues at all. I am trying to build another website now using the same contact form PHP script. However, when the user submits the form, instead of getting a success message, they get a "internal server error" message. The PHP script is identical (with the exception of the captcha key). The HTML is identical (with the exception of the captcha key) and some css styling.
Below is the HTML:
<form action="scripts/contact.php" method="post" target="resultMsg" style="width:300px; margin:auto;">
<input class="formInput" type="text" name="name" id="name" placeholder="Name"/>
<br/><br/>
<input class="formInput" type="text" name="email" id="email" placeholder="Email"/>
<br/><br/>
<textarea class="formInput" name="message" id="message" placeholder="Message"></textarea>
<br/><br/>
<div style="margin:auto;width:fit-content">
<div class="g-recaptcha" data-sitekey="xxxxxxxxxxxxxxxx"></div>
</div>
<br/><br/>
<input class="send" type="submit" value="Send"/>
</form>
<br /><iframe name="resultMsg" id="resultMsg" scrolling="no" style="border:0; height:30px; width:100%"></iframe>
Below is the PHP code in the scripts/contact.php file:
<?php
if(isset($_POST['g-recaptcha-response'])){
$captcha=$_POST['g-recaptcha-response'];
}
if(!$captcha){
echo("<p style='color:white; width:100%; text-align:center;'>Error: Please check the captcha box.</p>");
exit;
}
$secretKey = "xxxxxxxxxxxxxxxxxxxxxxxx";
$ip = $_SERVER['REMOTE_ADDR'];
$url = 'https://www.google.com/recaptcha/api/siteverify?secret=' . urlencode($secretKey) . '&response=' . urlencode($captcha);
$response = file_get_contents($url);
$responseKeys = json_decode($response,true);
if($responseKeys["success"]) {
$myemail = "myemail#ymail.com";
$subject = "Contact Form Submission";
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$sendMessage = "CONTACT FORM SUBMISSION
NAME:
$name
EMAIL ADDRESS:
$email
MESSAGE:
$message";
mail($myemail, $subject, $sendMessage);
echo("<p style='color:white; width:100%; text-align:center;'>Thank you for your email!</p>");
} else {
echo("<p style='color:white; width:100%; text-align:center;'>Error: message not sent, please refresh the browser.</p>");
}
exit();
?>
I have had issues with this last week, but the hosting company added AddHandler application/x-httpd-ea-php56 .php to the htaccess file and it fixed it. They also added extra htaccess files with this line all over the website directories for some reason. Then I think I may have accidentally deleted or changed some of the htaccess files and now it is not working again. What exactly is needed in which htaccess file for this to work correctly? Any ideas why it doesn't work anymore? Thanks
It turns out the issue was as simple as incorrect file permissions of the PHP script. Changing the permissions of the script to 644 fixed the problem, no more "internal server error".
INEXPERIANCED WITH PHP! SORRY FOR STUPID QUESTION!
My php email form submit is functioning as intended other than the success message. When I call my php from my form submit button the email sends correctly however the "echo "Thank You!" message at the end of the code displays on a new plaintext page ( only thing displayed is "Thank You!" ) with the slug "www.website.com/mail.php" ( the name of the php file ).
I understand this is possibly because I'm calling a separate file instead of running the php within the html document however I was hoping I wouldn't need to have my php in the same document, i'd rather keep it tidy and easier to work on in the future.
I have a few ideas as a workaround -
Instead of using echo I could call a seperate JS script which finds the tag with ID I wish to display the sucess message on.
Unsure if this would work but I could assign a global variable to have the sucess message then have php within my html which echos the variable.
Putting the PHP in the html ( as said above )
Instead using an alert if php is capable of that
However surely there is a better way of doing this? I cannot seem to find anyone who can help, believe me when I say I've googled so many times for a solution. Is my second solution the proper way? How should I be doing this "properly"?
Mail.php:
<?php $name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$formcontent="From: $name \n Email: $email \n Message: $message";
$recipient = "ddddn#gmail.com";
$subject = "Contact Form";
$mailheader = "From: contact#sdasdsdaT.com \r\n";
mail($recipient, $subject, $formcontent, $mailheader);
echo "Thank You!";
?>
HTML form:
<form action="php/mail.php" method="post">
<div class="form-group">
<label for="name">Your Name</label>
<input type="name" class="form-control" name="name" id="name" aria-describedby="Name input feild" placeholder="Name">
</div>
<div class="form-group">
<label for="email">Email address</label>
<input type="email" class="form-control" name="email" id="email" aria-describedby="emailHelp" placeholder="Email Address">
</div>
<div class="form-group">
<label for="Your Message">Your Message</label>
<textarea class="form-control messagebox" placeholder="Any and all enquiries are welcome!" name="message" rows="8" cols="80"></textarea>
</div>
<button type="submit" class="btn btn-primary">Submit</button>
</form>
Not sure what i'd expect from the php currently there, possibly for the echo message to appear within the tag form tag where the php file is called?
Again, I've really used every resource I have and I'm trying my best here. Thank you so much for your time.
I am gonna explain a bit and help out a little bit so as to leave space for your learning.
So, currently what you are doing is you are calling another page when you submit your form.
When the new page loads you're are displaying "thank you" using PHP.
What I understood from the question is you want to stay on the same page while sending the email and then display the text. For this we have to use AJAX in order to stay and do that job. Now, AJAX would be much easier with JQ, however I am guessing you are only new to this so I am gonna put the code in JS.
HTML
<button onclick="call()" type="submit" class="btn btn-primary">Submit</button>
JS
<script>
function call(){
console.log("called");
var xhttp;
xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
console.log(this.responseText);
var txt = this.responseText;
if(txt == "true"){
alert("Thank You");
}
else{
alert("false");
}
}
else{
console.log(this.status);
}
};
xhttp.open("POST", "php/mail.php", true);
xhttp.send();
}
</script>
PHP
<?php
echo "true";
?>
In this you will be able to display alert on the same page. You can beautify it by using Sweet Alert library if you want. I am guessing you can figure out the integration of your code in this. Good Luck!
It always redirects to the success page, but I never receive the email. I've tried rewriting both the PHP and the HTML, but it won't fix anything and I can never find google pages about how to fix this situation (everything appears to be fine, and most people have problems with receiving an email and not a redirect page). I'm not very familiar with PHP but can't find any errors. This is a contact page for a business website.
PHP:
<?php
$name = $_POST['name'];
$email = $_POST['email'];
$phone = $_POST['phone'];
$message = $_POST['message'];
$formcontent="From: $name \n Message: $message";
$to = "arsilhavy#gmail.com";
$subject = "Contact From $name";
$mailheader = "From: $email \n Phone: $phone \n Message $message \r\n";
mail($recipient, $subject, $mailheader, $formcontent);
if (mail($to, $subject, $body))
header('Location: success.html');
else
print "Something went wrong" ;
?>
HTML:
<div id="copy">
<form action="mail.php" method="POST" name="contact">
<div class="name">Name</div>
<input required type="text" name="name" style="border: solid 1px #081B26; width: 200px;" value="">
<div class="email">Email</div>
<input required type="email" name="email" style="border: solid 1px #081B26; width: 200px;" value="">
<div class="tel">Phone</div>
<input type="tel" name="phone" style="border: solid 1px #081B26; width: 200px;" value="">
<div class="message">Message</div>
<textarea required name="message" rows="6" cols="25" style="border: solid 1px #081B26; width: 200px;" value=""></textarea>
<br />
<input type="submit" value="Send" class="send">
<input type="reset" value="Clear" class="clear">
</form>
</div>
If you want to send Mails from PHP you will never be happy with this way. I strongly recommend to you to use something like PHPMailer (Download and Instructions here: https://github.com/PHPMailer/PHPMailer ) If you have an existing and properly working E-Mail use this one and configure PHP Mailer to log in with this adress and send Mails via SMTP. If you need an example I can provide one to you, but it is pretty strqaight foreward and very well explained on the given link.
You did not receive any E-Mail because (most likely) your E-Mail did NOT pass the spam filters on your destination e-mail adress. There are quite a lot of meta information and in general also certificates required for your mail to pass the spamfilters of most to all e-mail provider.
Are you trying to run this from your development computer? Chances are your PHP installation isn't configured to send mails.
Open your php.ini file.
Search for [mail function]
Edit settings to your needs
So I want my contact form to work on my site so I wrote some php to make it work. Here is the code: (form_process.php)
<?php
$name = $_POST('name');
$company = $_POST('company');
$email = $_POST('email');
$message = $_POST('message');
$to ="arp2222#yahoo.com";
$subject="New Message from Kincentive";
mail($to, $subject, $message, "From: ".$name);
echo "Your Message has been sent";
?>
I want to know how I can make this php work with my html file. I put the php file in the root folder with the index.html file and I believe I need to set up a form tag. I believe I need to use the action or method attribute? to setup as
for example.
I am using MAMP PRO as a local host since my site is not live yet and I want to test the contact form and recieve the test to my email.
Any help please i am new to php
in sendEmail.html you should write code as given
<form name="frmEmail" id="frmEmail" action="sendEmail.php" method="post">
<input type="text" name="fName" id="fName">
<input type="text" name="email" id="email">
<input type="text" name="company" id="company">
<textarea name="message" id="message"></textarea>
<input type="submit">
</form>
this form redirect to sendEmail.php
<?php
$name=$_POST['fName'];
$company=$_POST['company'];
$message=$_POST['message'];
$to =$_POST['email'];
$subject="New Message from Kincentive";
mail($to, $subject, $message, "From: ".$name);
echo "Your Message has been sent";
?>
$_POST is an array, so you should reference it like this, using [ brackets instead of curly ones.
$name = $_POST['name'];
$company = $_POST['company'];
$email = $_POST['email'];
$message = $_POST['message'];
In your HTML, wrap your inputs in a form like this, pointing to your Php file:
<form action="form_process.php" method="POST">
<-- input elements here !-->
</form>
I am trying to make a email send in a pop up on my site that you can see with the link below:
http://www.madaxedesign.co.uk
However it redirects perfectly to the thank you message however after it has redirected it does not implement the PHP. Below I have shown the PHP, HTML and Jquery used for this contact form.
HTML:
<form id="submit_message" class="hide_900" action="/send.php" method="post">
<div id="NameEmail">
<div>
<label for="name">Name*</label>
<input type="text" title="Enter your name" name="name"/>
</div>
<div>
<label for="email">Email*</label>
<input type="text" title="Enter your email address" name="email"/>
</div>
</div>
<div id="MessageSubmit">
<div>
<textarea maxlength="1200" title="Enter your message" name="message"></textarea>
<label for="message">Message</label>
</div>
<div class="submit">
<input type="submit" value="Submit"/>
</div>
</div>
</form>
PHP:
<?php
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$formcontent="From: $name \n Message: $message";
$recipient = "maxlynn12#gmail.com";
$subject = "Email From Madaxe";
$mailheader = "From: $email \r\n";
mail($recipient, $subject, $formcontent, $mailheader) or die("Error!");
header('Location: /thanks.html');
exit();
?>
Jquery:
$('form#submit_message').live('submit', function() {
$('#popup').load('/thanks.html');
return false;
});
I was wondering if anyone could quickly look and see if I am missing anything obvious that I can quickly fix or even point me in the right direction.
Thanks
Your jQuery is interfering.
I think this might help, using AJAX to post your form: jQuery Ajax POST example with PHP
You do not need to your jquery code. actually it prevents your default action form action=send.php. and it does not pass your inputs to send.php
$.live() is deprecated in jQuery 1.9 and that's what you appear to be using. Please either downgrade or use an alternative function like .submit().
$('#submit_message').submit(function (e) {
e.preventDefault();
$('#popup').load('/thanks.html');
}
Also, it is crucial that you sanitise your $_POST inputs before using them for your e-mail headers, otherwise a hacker can inject bad things into your headers.
if you are trying to send mail through localhost you need to change some setting in php.ini file. Refer below link to do this.
http://blog.techwheels.net/send-email-from-localhost-wamp-server-using-sendmail/