How to popup in the same window? - php

I have set the <form action="sendemail.php">. The below code is written in the sendemail.php.
The problem is that I want the status message be popped up in the same window say contact.html. It should not open a new window which says:
"Thank you for contacting us. As early as possible we will contact you"
It comes in a new blank window with this message ^^^^.
header('Content-type: application/json');
$status = ("Thank you for contacting us. As early as possible we will contact you"
);
$name = #trim(stripslashes($_POST['name']));
$email = #trim(stripslashes($_POST['email']));
$product_id = #trim(stripslashes($_POST['product_id']));
$phonenumber = #trim(stripslashes($_POST['phonenumber']));
$quantity = #trim(stripslashes($_POST['quantity']));
$subject = #trim(stripslashes($_POST['subject']));
$message = #trim(stripslashes($_POST['message']));
$email_from = $email;
$email_to = 'xyz#gmail.com';
$body = 'Name: ' . $name . "\n\n" . 'Email: ' . $email . "\n\n" . 'Product Id: ' . $product_id . "\n\n" . 'Contact Number: ' . $phonenumber . "\n\n" .'Quantity: ' . $quantity . "\n\n" .'Subject: ' . $subject . "\n\n" . 'Complete Address: ' . $message ."\n\n";
$success = #mail($email_to, $subject, $body, 'From: <'.$email_from.'>');
echo json_encode($status);
die;

I am not sure why you are doing jSON, but if jSON not required:
<?php
$name = #trim(stripslashes($_POST['name']));
$email = #trim(stripslashes($_POST['email']));
$product_id = #trim(stripslashes($_POST['product_id']));
$phonenumber = #trim(stripslashes($_POST['phonenumber']));
$quantity = #trim(stripslashes($_POST['quantity']));
$subject = #trim(stripslashes($_POST['subject']));
$message = #trim(stripslashes($_POST['message']));
$email_from = $email;
$email_to = 'xyz#gmail.com';
$body = 'Name: ' . $name . "\n\n" . 'Email: ' . $email . "\n\n" . 'Product Id: ' . $product_id . "\n\n" . 'Contact Number: ' . $phonenumber . "\n\n" .'Quantity: ' . $quantity . "\n\n" .'Subject: ' . $subject . "\n\n" . 'Complete Address: ' . $message ."\n\n";
// If main processes successfully, then continue with message
if(#mail($email_to, $subject, $body, 'From: <'.$email_from.'>')) { ?>
<!-- Just write normally -->
Thank you for contacting us. As early as possible we will contact you.
<?php die; } ?>

Related

How can I style PHP code using HTML tags? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
I am triggering an email to be sent with data from a filled out form once a submit button has been clicked. The email successfully sends with the correct data, however, I am wanting to be able to apply some basic styling to the email such as some <h3> or <strong> tags. Am I able to do this within my php $message variable, and if so, what would that look like?
<?php
$to = "test#test.com"; // this is your Email address
$from = "test#test.com"; // this is the sender's Email address
$company_name = $_POST['company_name'];
$rep_name = $_POST['rep_name'];
$prod_type = $_POST['prod_type'];
$address = $_POST['address'];
$city = $_POST['city'];
$state = $_POST['state'];
$zip = $_POST['zip'];
$phone = $_POST['phone'];
$email = $_POST['email'];
$subject = "New Form Submission";
$message = "New Form Submission" . "\n" . "\n" .
"Company Name: " . $company_name . "\n" .
"Representative Name: " . $rep_name . "\n" .
"Product Type: " . $prod_type . "\n" .
"Address: " . $address . "\n" .
"City: " . $city . "\n" .
"State: " . $state . "\n" .
"Zip: " . $zip . "\n" .
"Phone: " . $phone . "\n" .
"Email: " . $email . "\n";
$headers = "From:" . $from;
mail($to,$subject,$message,$headers);
?>
Edit:
I have tried using ob_start() and ob_get_clean() that I found at this link Defining html code inside PHP variables but had no success
Set Content-Type: text/html header to enable HTML emails;
$headers = "From:" . $from;
$headers .= "Content-Type: text/html";
Add some HTML elements to the email;
$message = <<<EOL
<h1>Hi!</h1>
<h2>HTML Emails are awesome!</h2>
EOL;
mail($to, $subject, $message, $headers);
Small working example;
<?php
$to = '----';
$subject = 'SO test Mail';
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$headers .= 'X-Mailer: PHP/' . phpversion();
$message = <<<EOL
<html>
<body>
<h1>Hi!</h1>
<h2>HTML Emails are awesome!</h2>
</body>
</html>
EOL;
if ($res = (mail($to, $subject, $message, $headers))) {
echo 'OK';
} else {
echo 'Error';
var_dump($res);
}

PHP code wont redirect to the page but shows a blank page

<?php
if (isset($_POST) && !empty($_POST))
{
//pre-defined vals
$to = "salesghana#tallyafrica.com";
$subject = "PRODUCT AND SERVICE REQUEST";
//from post form
$user_name = $_POST['user_name'];
$contact = $_POST['contact'];
$email = $_POST['email'];
$company_name = $_POST['company_name'];
$company_address = $_POST['company_address'];
$user_type = $_POST['user_type'];
$pref_service_one = $_POST['pref_service_one'];
$pref_service_one = $_POST['pref_service_two'];
$addon_one = $_POST['addon_one'];
$addon_two = $_POST['addon_two'];
$comments = $_POST['comments'];
$message = "NAME: " . $user_name . "\n\n" . "CONTACT: " . $contact . "\n\n" . "EMAIL: " . $email . "\n\n" . "COMPANY NAME: " . $company_name . "\n\n" . "COMPANY ADDRESS: " . $company_address . "\n\n" . "TALLY SOFTWARE: " . $user_type . "\n\n" . "PREFERRED SERVICE ONE: " . $pref_service_one . "\n\n" . "PREFERRED SERVICE TWO: " . $pref_service_two . "\n\n" . "ADDON ONE: " . $addon_one . "\n\n" . "ADDON TWO: " . $addon_two . "\n\n" . "COMMENTS: " . $comments;
$headers = "<From: noreply#tallyafrica.com>";
mail($to, $subject, $message, $headers);
header("Location:http://www.prymage.com/thankyou.html");
}
?>
This is my PHP that is sent successfully, but shows a blank white page and doesn't redirect to the header link.
If I had to guess I would say something is causing output to the browser. Either a stray echo or print, or whitespace before/after php tags, or an error/exception and errors are suppressed. The code below will help in the instance that it's the mail function causing output to be generated, but it won't if the output is happening before coming to the if statement.
As a bonus, you can do this to return control to the browser faster and continue processing the email after the user has been redirected.
<?php
if (isset($_POST) && !empty($_POST))
{
// move header up, add ignore user abort for safety.
ignore_user_abort( TRUE );
header("Location:http://www.prymage.com/thankyou.html");
//pre-defined vals
$to = "salesghana#tallyafrica.com";
$subject = "PRODUCT AND SERVICE REQUEST";
//from post form
$user_name = $_POST['user_name'];
$contact = $_POST['contact'];
$email = $_POST['email'];
$company_name = $_POST['company_name'];
$company_address = $_POST['company_address'];
$user_type = $_POST['user_type'];
$pref_service_one = $_POST['pref_service_one'];
$pref_service_one = $_POST['pref_service_two'];
$addon_one = $_POST['addon_one'];
$addon_two = $_POST['addon_two'];
$comments = $_POST['comments'];
$message = "NAME: " . $user_name . "\n\n" . "CONTACT: " . $contact . "\n\n" . "EMAIL: " . $email . "\n\n" . "COMPANY NAME: " . $company_name . "\n\n" . "COMPANY ADDRESS: " . $company_address . "\n\n" . "TALLY SOFTWARE: " . $user_type . "\n\n" . "PREFERRED SERVICE ONE: " . $pref_service_one . "\n\n" . "PREFERRED SERVICE TWO: " . $pref_service_two . "\n\n" . "ADDON ONE: " . $addon_one . "\n\n" . "ADDON TWO: " . $addon_two . "\n\n" . "COMMENTS: " . $comments;
$headers = "<From: noreply#tallyafrica.com>";
mail($to, $subject, $message, $headers);
// added exit for safety in case there is more code below the if statement.
exit();
}
Or the old way but with error reporting:
// add in error reporting
ini_set('display_errors', 'On');
ini_set('html_errors', 0);
error_reporting(-1);
if (isset($_POST) && !empty($_POST))
{
//pre-defined vals
$to = "salesghana#tallyafrica.com";
$subject = "PRODUCT AND SERVICE REQUEST";
//from post form
$user_name = $_POST['user_name'];
$contact = $_POST['contact'];
$email = $_POST['email'];
$company_name = $_POST['company_name'];
$company_address = $_POST['company_address'];
$user_type = $_POST['user_type'];
$pref_service_one = $_POST['pref_service_one'];
$pref_service_one = $_POST['pref_service_two'];
$addon_one = $_POST['addon_one'];
$addon_two = $_POST['addon_two'];
$comments = $_POST['comments'];
$message = "NAME: " . $user_name . "\n\n" . "CONTACT: " . $contact . "\n\n" . "EMAIL: " . $email . "\n\n" . "COMPANY NAME: " . $company_name . "\n\n" . "COMPANY ADDRESS: " . $company_address . "\n\n" . "TALLY SOFTWARE: " . $user_type . "\n\n" . "PREFERRED SERVICE ONE: " . $pref_service_one . "\n\n" . "PREFERRED SERVICE TWO: " . $pref_service_two . "\n\n" . "ADDON ONE: " . $addon_one . "\n\n" . "ADDON TWO: " . $addon_two . "\n\n" . "COMMENTS: " . $comments;
$headers = "<From: noreply#tallyafrica.com>";
mail($to, $subject, $message, $headers);
header("Location:http://www.prymage.com/thankyou.html");
// added exit for safety in case there is more code below the if statement.
exit();
}

webform mail error. messages coming up with sender's details blank

The messages I am receiving from my web form coming up without any sender's details. I am a beginner. Could someone guide me to get it to work. Here is the screenshot of the error and the php and js.
<?php
header('Content-type: application/json');
$status = array(
'type'=>'success',`enter code here`
'message'=>'Email sent!'
);
$name = #trim(stripslashes($_POST['name']));
$email = #trim(stripslashes($_POST['email']));
$subject = #trim(stripslashes($_POST['subject']));
$message = #trim(stripslashes($_POST['message']));
$email_from = $email;
$email_to = 'myname#gmail.com';
$body = 'Name: ' . $name . "\n\n" . 'Email: ' . $email . "\n\n" . 'Subject: '
. $subject . "\n\n" . 'Message: ' . $message;
$success = #mail($email_to, $subject, $body, 'From: <'.$email_from.'>');
echo json_encode($status);
die;
?>
Validation
$('.contact-form').submit(function () {'use strict',
$this = $(this);
$.post("sendemail.php", $(".contact-form").serialize(),function(result){
if(result.type == 'success'){
$this.prev().text(result.message).fadeIn().delay(3000).fadeOut();
}
});
return false;
});

PHP contact form getting error "missing_mailbox#syntax_error"

I have included my PHP code and hopefully someone can spot the error!
I have replaced my email for privacy and spam reasons.
<?php
header('Content-type: application/json');
$status = array(
'type'=>'success',
'message'=>'Thank you for contacting us. We will get back to you as soon as possible. '
);
$name = #trim(stripslashes($_POST['name']));
$email = #trim(stripslashes($_POST['email']));
$subject = #trim(stripslashes($_POST['subject']));
$message = #trim(stripslashes($_POST['message']));
$email_from = $email;
$email_to = 'test#email.com';//replace with your email
$body = 'Name: ' . $name . "\n\n" . 'Email: ' . $email . "\n\n" . 'Subject: ' . $subject . "\n\n" . 'Message: ' . $message;
$success = #mail($email_to, $subject, $body, 'From: <'.$email_from.'>');
echo json_encode($status);
die;
?>

How to add custom message in to my form mailer

I want to add "This message came from etcetc.com" in the e-mail form in the e-mail body itself. Hope this make sense..
my sent_email.php
<?php
$email_to = 'test#test.org';
$name = $_POST['name'];
$email = $_POST['email'];
$subject = $_POST['subject'];
$message = $_POST['message'];
$headers = 'From: ' . $name . ' <' . $email . '>' . "\r\n" . 'Reply-To: ' . $email;
if(mail($email_to, $subject, $message, $headers)) {
echo 'sent'; // we are sending this text to the ajax request telling it that the mail is sent..
} else {
echo 'failed'; // ... or this one to tell it that it wasn't sent
}
?>
Like this:
<?php
$email_to = 'test#test.org';
$name = $_POST['name'];
$email = $_POST['email'];
$subject = $_POST['subject'];
$message = "This message came from etcetc.com \r\n".$_POST['message'];
$headers = 'From: ' . $name . ' <' . $email . '>' . "\r\n" . 'Reply-To: ' . $email;
if(mail($email_to, $subject, $message, $headers)) {
echo 'sent'; // we are sending this text to the ajax request telling it that the mail is sent..
} else {
echo 'failed'; // ... or this one to tell it that it wasn't sent
}
?>
All you need to do is add your text to the end of $message.
Change:
$message = $_POST['message'];
to
$message = $_POST['message'] . "\n\nThis message came from etcetc.com";
Add message before and after as you wish by
$message = "This is before posted message\r\n";
$message .= $_POST["message"];
$message .= "\r\nthis is after the posted message";

Categories