jQuery.post not POSTing - php

I am trying to use jQuery to post data to an emailus.php script.
Here is the script part:
<script>
jQuery(document).ready(function(){
jQuery('#submitt').click(function(){
jQuery.post("/emailus.php", jQuery("#mycontactform").serialize(), function(response) {
jQuery('#success').html(response);
});
return false;
});
});
</script>
and here is the HTML used:
<form action="" method="get" id="mycontactform" >
<label for="name">Your Name:</label><br />
<input type="text" name="name" class="cleann" /><br />
<label for="email">Your Email:</label><br />
<input type="text" name="email" class="cleann" /><br />
<label for="message">Your Message:</label><br />
<textarea name="message" class="cleann" rows="7"></textarea><br />
<input type="button" value="send" id="submitt" class="cleannsubmit" /><div id="success" style="color:green;"></div>
</form>
and here is the php script:
<?php
// Here we get all the information from the fields sent over by the form.
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$to = 'nohanada#gmail.com';
$subject = 'Fortrove Contact';
$message = 'FROM: '.$name.' Email: '.$email.'Message: '.$message.'\n\nItem:'.$itemname;
print_r($_POST);
if($name && $email && $message){
if (eregi("^[_a-z0-9-]+(\.[_a-z0-9-]+)*#[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$", $email)){
mail($to, $subject, $message);
echo "Your email was sent!";
}
else echo "<span style='color:red'>Invalid email format.</span>";
}
else echo "<span style='color:red'>Please fill all fields</span>";
?>
Problem is that it does not POST the actual fields to the php script. What am i doing wrong?

you have placed the mycontactform inside product_addtocart_form that is the reason which is not allowed so the browser seems to be remocing the mycontactform

Related

How can I add a success message to my mail form code?

I tried using echo but something's wrong in that echo area.
I got syntax errors and and an error saying unexpected token afterecho "Try again."
<?php
add_action("admin_menu", "addMenu");
function addMenu()
{
add_menu_page("smail", "smail", 4, "smail", "sMail" );
}
function sMail()
{
?>
<h2> send mail</h2>
<form method="post" action="">
<?php
if(isset($_POST['submit'])){
$to = sanitize_email( $_POST['email_address'] );
$subject = sanitize_text_field( $_POST['subject'] );
$txt = esc_textarea( $_POST['text'] );
$from = sanitize_email( $_POST['frommail'] );
$headers = "From:" . $from ;
if (mail($to,$subject,$txt,$headers)){
echo "Mail sent successfully.";
} else{
echo "Try again.";
}
}
?>
<label for="frommail">From </label>
<input id="frommail" name="frommail" type="text" maxlength="255" />
<label for="email_address">To </label>
<input id="email_address" name="email_address" type="text" maxlength="255"/>
<label for="subject">Subject </label>
<input id="subject" name="subject" type="text" maxlength="255" />
<label for="text">Text </label>
<textarea id="text" name="text" row="10"></textarea>
         <input value="submit" id="submit" name="submit" type="submit">
</form>
<?php
}
?>
Please don't define functions like in your sMail function.
If you want to print out html content, use echo instead.
You also miss a } from end of file.
Also:
If you want to use the current script for your action path, just leave out the action tag.
Here's your corrected code:
<?php
add_action("admin_menu", "addMenu");
function addMenu()
{
add_menu_page("smail", "smail", 4, "smail", "sMail");
}
function sMail()
{
echo " <h2> send mail</h2> ";
echo " <form method='post' action=''> ";
if (isset($_POST['submit']))
{
$to = sanitize_email($_POST['email_address']);
$subject = sanitize_text_field($_POST['subject']);
$txt = esc_textarea($_POST['text']);
$from = sanitize_email($_POST['frommail']);
$headers = "From:" . $from;
if (mail($to, $subject, $txt, $headers))
{
echo "Mail sent successfully.";
}
else
{
echo "Try again.";
}
}
}
?>

Contact Form Disappearing When Div is Set?

Ok, so I've got a contact form:
<?php
if (isset($_POST['subtest'])) {
$to = 'thomofawsome#gmail.com';
$name = $_POST['firstname'];
$lastName = $_POST['lastname'];
$email = $_POST['email'];
$city = $_POST['city'];
$state = $_POST['state'];
$zip = $_POST['zip'];
$comments = $_POST['comments'];
$Message = <<< STOP
From: $name $lastName
Email: $email
In: $city, $state, $zip
Comments: $comments
STOP;
$subject = "Contact Request";
$headers = 'From: system';
if (mail($to, $subject, $Message, $headers)) {
echo '<div id="thanks">Mail sent</div>';
exit();
}
else {
echo 'Mail Failed';
}
}
?>
<form name="contact_form" action="" method="post">
<input type="hidden" name="subtest" value="true">
First Name:<br>
<input type="text" name="firstname">
<br>
Last Name:<br>
<input type="text" name="lastname">
<br>
Email Address:<br>
<input type="text" name="email">
<br>
City:<br>
<input type="text" name="city">
<br>
State:<br>
<input type="text" name="state">
<br>
Zip:<br>
<input type="text" name="zip">
<br>
Comments:<br>
<textarea name="comments"></textarea>
<br>
<input id="submit" type="submit" name="submit" value="Send">
<br>
</form>
The problem is I want the echo mail sent correctly formatted (with a green color, positioned correctly on the page, etc.). As you can see, I've put it in a div. When I submit the form though, I'm redirected back to the form page, except the entire form and footer disappear, and Mail sent appears on the bottom of the page (correctly formatted).
Any ideas?
The exit() function prevents the rest of the script from executing, which includes your form and footer. Remove it and it will work.
if (mail($to, $subject, $Message, $headers)) {
echo '<div id="thanks">Mail sent</div>';
} else {
echo 'Mail Failed';
}
As Raidenance pointed out, if someone refreshes this page after posting the form it will resend the email address. A better solution to this problem is to post your contact form to another url (/contact/submit for instance) and on completion of the script execution at that url simply redirect back to the contact form with a parameter
header("Location:/contact?success=true");
Then on your contact form page:
if (isset($_GET['success']) && $_GET['success'] == "true") {
echo '<div id="thanks">Mail sent</div>';
} else {
echo 'Mail Failed';
}
This will prevent the issue of the user reloading and receiving the email multiple times.

404 error contact form bootstrap

After reading about every single forum post I still cannot figure out how to get my contact form to work correctly. I got the frontend part of it looking good but I am getting a 404 error every time I try to use it. Obviously, because of that none of the information submitted is getting through. Here is my code:
Here is my contact.php which has a
<script src="email/validation.js" type="text/javascript"></script>
at the top between the header
<div class="span12" id="divMain">
<div id="contact">
<h1>Contact Us</h1></div>
<h3 style="color:#FF6633;"><?php echo $_GET[msg];?></h3>
<hr>
<!--Start Contact form -->
<form name="enq" method="post" action="email/index.php" onsubmit="return
validation();">
<fieldset>
<input type="text" name="name" id="name" value="" class="input-block-level" placeholder="Name" />
<input type="text" name="email" id="email" value="" class="input-block-level" placeholder="Email" />
<textarea rows="9" name="message" id="message" class="input-block- levelplaceholder="Let's hear what you've got to say"> </textarea>
<div class="actions">
<input type="submit" value="Send" name="submit" id="submitButton" class="btn btn-success pull -right" title="Click here to submit your message!" />
</div>
</fieldset>
<hr>
</form>
<!--End Contact form -->
</div>
Next here is my validation.js
function validation()
{
var contactname=document.enq.name.value;
var name_exp=/^[A-Za-z\s]+$/;
if(contactname=='')
{
alert("Name Field Should Not Be Empty!");
document.enq.name.focus();
return false;
}
else if(!contactname.match(name_exp))
{
alert("Invalid Name field!");
document.enq.name.focus();
return false;
}
var email=document.enq.email.value;
//var email_exp=/^[A-Za-z0-9\.-_\$]+#[A-Za-z]+\.[a-z]{2,4}$/;
var email_exp=/^\w+([-+.']\w+)*#\w+([-.]\w+)*\.\w+([-.]\w+)*$/;
if(email=='')
{
alert("Please Enter Email-Id!");
document.enq.email.focus();
return false;
}
else if(!email.match(email_exp))
{
alert("Invalid Email ID !");
document.enq.email.focus();
return false;
}
var message=document.enq.message.value;
if(message=='')
{
alert("Query Field Should Not Be Empty!");
document.enq.message.focus();
return false;
}
return true; }
Followed by my index.php
<?php
if(isset($_POST['submit']))
{
$name = $_POST['name'];
$email = $_POST['email'];
$query = $_POST['message'];
$email_from = $name.'<'.$email.'>';
$to="marketing#durangoconnections.com";
$subject="Enquiry!";
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$headers .= "From: ".$email_from."\r\n";
$message="
Name:
$name
<br>
Email-Id:
$email
<br>
Message:
$query
";
if(mail($to,$subject,$message,$headers))
header("Location:../contact.php?msg=Successful Submission! Thankyou for contacting us.");
else
header("Location:../contact.php?msg=Error To send Email !");
//contact:-your-email#your-domain.com
}
?>
You dont have a contact.php file on your server, you mean contact_us.php
if(mail($to,$subject,$message,$headers))
header("Location:../contact_us.php?msg=Successful Submission! Thankyou for contacting us.");
else
header("Location:../contact_us.php?msg=Error To send Email !");
//contact:-your-email#your-domain.com
}

How to run php script while adding text to same html page?

So basically, I want to have someone complete a form, run a php script, and instead of pulling up the php file's web page, I want to append text at the end of the form.
Currently, I have a form:
<form action="scripts/email.php" method="POST">
<p>Name: <input type="text" name="name" placeholder="Name"></p>
<p>Email: <input type="text" name="email" placeholder="Email"></p>
<p>Subject: <input type="text" name="subject" placeholder="Subject"></p>
<p>Message: </p>
<p><textarea name="message"></textarea></p>
which looks like this:
When 'Submit' is clicked, it leads to my PHP script:
<?php
$from = $_POST['email'];
$to = "comp#c0mp.org";
$subject = $_POST['subject'];
$name = $_POST['name'];
$message = $_POST['message'];
mail($to, $subject, "Name: " . $name . "\nMessage: " . $message, "From:" . $from);
print "Your message has been sent!";
?>
which leads to a blank web page that just says this in plain text:
"Your message has been sent!"
Basically, what I want is text added onto the submit form after the script is ran, such as:
http://i.stack.imgur.com/zha4d.png
How can I achieve running a PHP script on a web page, and instead of loading a new web page of the PHP script's path, but append text on the current HTML web page?
TRY THIS : WRITE THIS CODE INTO A SINGLE PAGE.
<form action="<?php $PHP_SELF ;?>" method="POST">
<p>Name: <input type="text" name="name" placeholder="Name"></p>
<p>Email: <input type="text" name="email" placeholder="Email"></p>
<p>Subject: <input type="text" name="subject" placeholder="Subject"></p>
<p>Message: </p>
<p><textarea name="message"></textarea></p>
<input type="submit" name="submit" value="Submit">
</form>
<?php
if (isset($_POST['submit'] ))
{
$from = $_POST['email'];
$to = "comp#c0mp.org";
$subject = $_POST['subject'];
$name = $_POST['name'];
$message = $_POST['message'];
mail($to, $subject, "Name: " . $name . "\nMessage: " . $message, "From:" . $from);
echo "Your message has been sent";
}
?>
you can use ajax for acheive more secure and fast execution of script.
HTML code:
<form method="POST">
<p>Name: <input type="text" id="name" name="name" placeholder="Name"></p>
<p>Email: <input type="text" id="email" name="email" placeholder="Email"></p>
<p>Subject: <input type="text" id="subject" name="subject" placeholder="Subject"></p>
<p>Message: </p>
<p><textarea id="area" name="message"></textarea></p>
<input type="submit" name="submit" value="Submit" onclick="mail_js();">
</form>
<div id="success_msg" style="display:none;"></div>
Just give proper style to this div.
JS code:
function mail_js()
{
var input_val=$('#name').val();
var email_val=$('#email').val();
var subject_val=$('#subject').val();
var message_val=$('#area').val();
jQuery.ajax({
type: 'post',
data:{
'input_val':input_val,
'email_val':email_val,
'subject_val':subject_val,
'message_val':message_val
},
url: 'email.php',
success:function(response){
$("#success_msg").append(response).show();
},
error: function(errorThrown){
alert('error');
console.log(errorThrown);
}
});
}
PHP script:
$from = $_POST['email_val'];
$to = "comp#c0mp.org";
$subject = $_POST['subject_val'];
$name = $_POST['input_val'];
$message = $_POST['message_val'];
mail($to, $subject, "Name: " . $name . "\nMessage: " . $message, "From:" . $from);
echo "Your message has been sent";

How can I show the PHP results of a contact form with AJAX?

I have a simple html contact form, and the php script to send the emails. It's working good, but I want the result (The email has been sent...) to show in the same page, without changing the page. How can I do this?
HTML:
<form name="contact" action="includes/send.php" id="contact_form">
<input type="text" placeholder="Name" name="name" /> <br />
<input type="email" placeholder="Email Address" name="email" /> <br />
<textarea name="message" placeholder="Message" rows="8"></textarea> <br />
<input type="submit" name="submit" id="submit_btn" value="Send" />
</form>
PHP:
<?php
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$to = 'amar123syla#gmail.com';
$subject = 'Message from AMARSYLA.COM';
$message = 'FROM: '.$name.' Email: '.$email.'Message: '.$message;
$headers = 'From: amar123syla#gmail.com';
if (filter_var($email, FILTER_VALIDATE_EMAIL)) { // this line checks that we have a valid email address
mail($to, $subject, $message) or die('Error sending Mail'); //This method sends the mail.
echo "Your email was sent!"; // success message
}
?>
jQuery.post('email.php', score, function(result) {
jQuery('#textBlock').html(result);
});
Something like that should work(Result is the echo in your php in this case)
A preffered method is to encode your php result to json so you've got a little more control over it in Javascript though.
jQuery.post('email.php', score, function(result) {
if (result.result == true) {
jQuery('#textBlock').html(result.text);
}
});
And in the email.php:
$result['result'] = true;
$result['text'] = 'Message has been sent';
header('Content-Type: application/json');
echo json_encode($result);
Here see if this works:
<form name="contact" action="**submit to same page**" id="contact_form">
<input type="submit" name="submit" id="submit_btn" value="Send" />
</form>
Then for the php (on the same page as the email form) just add if submit
<?php
if(isset($_POST['submit'])){
run your email script, add javascript, etc.
echo "Your email was sent!"; // success message
}
}
?>

Categories