Ajax form not refreshing - displaying number ' 1 ' after success - javascript

I have a form consisting of 2 input fields on submit the ajax form is suppose to refresh the page and fade in the download button, but it goes to the php file ' download.php ' and displays the digit 1 on top left.
Since im receiving the information from the input I'm guessing 1 is for success and 0 is for fail. how do i stop it from doing this and stay on same page / refresh.
TL;DR - I have a form which submits fine and sends the information to the email ID but it does not refresh the page it goes to the php file and displays digit 1 for success.
Please help.
HERE is my jsfiddle
followed by my ajax php 'download.php' code, (it does have php opening tag its not showing here for some reason)
<?php
$name =$_POST['name'];
$email =$_POST['email'];
// Example
$HTML = '<table cellspacing="0" cellpadding="8" border="0" width="600" style="background:#EEE">
<tr>
<td>File Download</td>
<td></td>
</tr>
<tr>
<td>Name:</td>
<td>'.$name.'</td>
</tr>
<tr>
<td>email:</td>
<td>'.$email.'</td>
</tr>
</table>';
$from = 'emailid#gmail.com';
$to = "emailid#gmail.com";
$subject = "Solutions Enquiry by Mr/ Mrs : ".$name;
sendHTMLemail($HTML,$from,$to,$subject);
function sendHTMLemail($HTML,$from,$to,$subject)
{
// First we have to build our email headers
// Set out "from" address
$headers = "From: $from\r\n";
// Now we specify our MIME version
$headers .= "MIME-Version: 1.0\r\n";
// Create a boundary so we know where to look for
// the start of the data
$boundary = uniqid("HTMLEMAIL");
// First we be nice and send a non-html version of our email
$headers .= "Content-Type: multipart/alternative;".
"boundary = $boundary\r\n\r\n";
$headers .= "This is a MIME encoded message.\r\n\r\n";
$headers .= "--$boundary\r\n".
"Content-Type: text/plain; charset=ISO-8859-1\r\n".
"Content-Transfer-Encoding: base64\r\n\r\n";
$headers .= "Message-ID: <".$now." TheSystem#".$_SERVER['SERVER_NAME'].">\r\n";
$headers .= "X-Mailer: PHP v".phpversion()."\r\n";
$headers .= chunk_split(base64_encode(strip_tags($HTML)));
// Now we attach the HTML version
$headers .= "--$boundary\r\n".
"Content-Type: text/html; charset=ISO-8859-1\r\n".
"Content-Transfer-Encoding: base64\r\n\r\n";
$headers .= chunk_split(base64_encode($HTML));
// And then send the email ....
$sent=mail($to,$subject,"",$headers);
if($sent)
echo '1';
else
echo '0';
}
?>

In JSFiddle example you'll forget to add jQuery library in sandbox. That's why your code doesn't work. In pure JS there is no selectors like this: $('#...').
The second problem is - you're add
After that i have no any refresh on the page.
UPDATE:
I recommend you to use JSON to translate data from server to client. Add:
dataType: 'json'
to your $.ajax call and at server side:
echo json_encode(array('status'=>1));
it help's you to provide more details and data from the server to your client script.

Related

Contact form does not work for me with special characters inlcuding UTF-8 [duplicate]

When I send out the email, the email does not show characters other than english.
It does show like below:
余生ä»ä»
May know actually what cause this?
Even I tried to added Content-type and charset in the script, it still show the same.
I used Mail::Factory("mail");
You can add header "Content-Type: text/html; charset=UTF-8" to your message body.
$headers = "Content-Type: text/html; charset=UTF-8";
If you use native mail() function $headers array will be the 4th parameter
mail($to, $subject, $message, $headers)
If you user PEAR Mail::factory() code will be:
$smtp = Mail::factory('smtp', $params);
$mail = $smtp->send($to, $headers, $body);
I'm using rather specified charset (ISO-8859-2) because not every mail system (for example: http://10minutemail.com) can read UTF-8 mails. If you need this:
function utf8_to_latin2($str)
{
return iconv ( 'utf-8', 'ISO-8859-2' , $str );
}
function my_mail($to,$s,$text,$form, $reply)
{
mail($to,utf8_to_latin2($s),utf8_to_latin2($text),
"From: $form\r\n".
"Reply-To: $reply\r\n".
"X-Mailer: PHP/" . phpversion());
}
I have made another mailer function, because apple device could not read well the previous version.
function utf8mail($to,$s,$body,$from_name="x",$from_a = "info#x.com", $reply="info#x.com")
{
$s= "=?utf-8?b?".base64_encode($s)."?=";
$headers = "MIME-Version: 1.0\r\n";
$headers.= "From: =?utf-8?b?".base64_encode($from_name)."?= <".$from_a.">\r\n";
$headers.= "Content-Type: text/plain;charset=utf-8\r\n";
$headers.= "Reply-To: $reply\r\n";
$headers.= "X-Mailer: PHP/" . phpversion();
mail($to, $s, $body, $headers);
}
If not HTML, then UTF-8 is not recommended. koi8-r and windows-1251 only without problems. So use html mail.
$headers['Content-Type']='text/html; charset=UTF-8';
$body='<html><head><meta charset="UTF-8"><title>ESP Notufy - ESP Сообщения</title></head><body>'.$text.'</body></html>';
$mail_object=& Mail::factory('smtp',
array ('host' => $host,
'auth' => true,
'username' => $username,
'password' => $password));
$mail_object->send($recipents, $headers, $body);
}

send google tag manager results to php page via ajax method GET

i got a few variables on my site that i push on GTM they work perfect, now i need to get variables results from gtm and send it via ajax to a php page where afterwards i'll use it to an email with the results.
<script>
$(document).ready( function() {
var msg = google_tag_manager["GTM-xxxxx"].dataLayer.get('pageName');
var msg1 = google_tag_manager["GTM-xx"].dataLayer.get('MembershipLvl');
var msg2 = google_tag_manager["GTM-xx"].dataLayer.get('CountryRes');
var msg3 = google_tag_manager["GTM-xxxx"].dataLayer.get('pageLanguage');
var getall = [msg, msg1, msg2, msg3,];
// var me = 'just seend'; this do work
console.log(getall);
$.ajax({
type: "GET",
url: "url.cart.php", // some php
data:{info: getall},
datatype: 'html',
success: function(data) {}
});
});
and here is my php
<?php
/*
*
*/
send_mail();
function send_mail(){
if( $_GET["info"] ) {
$mail = $_GET["info"];
$header ="From: no-reply#test.com" . "\r\n";
$for = 'end#emailadaress.com';
$header .= "MIME-Version: 1.0\r\n";
$header .= "Content-Type: text/html; charset=ISO-8859-1\r\n";
$title = 'Mailing list Newsletter';
$message = '<html><body>';
$message .= '<br/> <p>tou got email from</p><br/>';
$message .= '<table rules="all" style="border-color: #666;" cellpadding="2">';
$message .= "<tr><td><strong>Email:</strong> </td><td>" . strip_tags($mail)."</td></tr>";
$message .= "</table>";
$message .= "</body></html>";
if(mail($for, $title, $message, $header)){
die();
}else{
//echo "false";
}
}else{
echo "false";
}
}
?>
the issue is that im not getting GTM variables sent, i do see them in console log, if i change variable like var = test, i do get it, but i just cant get the GTM variable sent
So the problem is that $_GET["info"] contains an array which strip_tags cannot handle. If you look at the response from your call it should have a PHP error. Somewhere along the lines of : Warning: strip_tags() expects parameter 1 to be string, array given. If you want to get the values out of the array you can either build a loop or reference the particular index.
LOOP
foreach ($mail as $gtm) {
$message .= $gtm;
}
INDEX
$message .= $mail[0] // google_tag_manager["GTM-xxxxx"].dataLayer.get('pageName');
$message .= $mail[1] // google_tag_manager["GTM-xx"].dataLayer.get('MembershipLvl');
etc...
PHP strip tags
Foreach loop
How to view a ajax call in chrome

inserting the php sendmail fuction into a confirm button within a pop up box

I am trying to use the sendmail function in php threw a pop up box. i have gotten as far as making the sendmail function and the pop up to work separately. but i haven't been able to figure out how to connect the sendmail in the right way. below is the last working git version i have. any help would be wonderful.
{
<tr>
<th align="center">Instrument Serial Number:</th>
<td align="left"><input type="text" class="cInstTravText" name="SerialNumber" id="idSerialNumber"/></td>
<button onclick="drop_check()">comfirm</button>
<p id="drop_check"></p>
<script>
function sendEmail()
{
$to = 'jonsrod1992#gmail.com';
$subject = 'Test email using PHP';
$message = "test";
//$message = 'hello world.\n\n this is a test for functionality\n\n this is the first try\n place instrument serial Number here--> '<$modelNumber>'<-- there.';
$headers = 'From: jonr#twobtech.com' . "\r\n" . 'Reply-To: jonr#twobtech.com' . phpversion();
mail($to, $subject, $message, $headers, '-fwebmaster#example.com');
}
function drop_check() {
var x;
if (confirm("transfer and send email!") == true) {
x = "transfered and email sent!";
} else {
x = "You pressed Cancel!";
}
document.getElementById("drop_check").innerHTML = x;
}
</script>
</tr>
The approach you're attempting isn't going to work. The php on your page runs first (server side), and then your pop-up is triggered using JavaScript. This means you'll need to send the response from the pop-up to another php page which then handles the sendmail. You can do this via POST in a form submission, or better yet, using an AJAX call, like so (this goes between script tags, preferably at the top or bottom of your page, not in the middle of your HTML):
if (confirm("transfer and send email!")) {
//if needed, get values from one or more HTML inputs like so:
var stuff = $('#someInputId').val();
$.ajax({
type: "POST",
url: "sendmail.php",
data: { data: stuff } //if you don't need to send any data from your HTML elements this can be ommitted
}).success(function() {
//optional: let the user know email was sent
});
}
You would then put your sendmail code in sendmail.php. If you're sending any data in the AJAX call, you can retrieve it from $_POST. sendmail.php would look something like:
$body = isset($_POST['stuff']) ? $_POST['stuff'] : 'no stuff';
$to = 'jonsrod1992#gmail.com';
$subject = 'Test email using PHP';
$headers = 'From: jonr#twobtech.com' . "\r\n" .
'Reply-To: jonr#twobtech.com' . "\r\n" .
"MIME-Version: 1.0\r\n" .
"Content-Type: text/html; charset=ISO-8859-1\r\n";
mail($to,$subject,$body,$headers);
Maybe I'm missing something, but... it looks like you may be trying to just throw PHP into your javascript. That is not going to work. Also, if this is just weird reformatting of the code, the sendEmail function is not being called.

display success message with clear the post data and redirect on same page

I have one web page. In that I have contact form.
What I want that after submit the form it should redirect on same page and display success message(message should be fadeout after few seconds) and the post data of form should be clear. In form i am also cheking verification code and sending mail.
if(md5($_POST['vcode']).'a4xn' == $_COOKIE['tntcon'])
{
$msg="message";
$to = 'test#gmail.com';
$subject = 'subject';
$message = $msg;
$headers = "MIME-Version: 1.0\r\n";
$headers .= "Content-type:text/html;charset=UTF-8" . "\r\n";
$headers .="From: ".$_POST['email']." \r\n";
$mail=mail($to, $subject, $message, $headers);
if($mail==true)
{
echo '<script>$("#deletesuccess").fadeIn();$("#deletesuccess").delay(4000).fadeOut();window.location="index.php";</script>';
//here i want to redirect on same page with clear post data and display success message.
}
}
else
{
echo '<script type="text/javascript">document.getElementById("disp_wrong_code").style.display="inline";</script>';
// This is verification code error.
}
This is custom php page.
For redirecting you can use header function. For storing data between page loads use $_SESSION. Some simplified example:
session_start();
if ($can_do_post)
{
// do some actions here
$mail=mail($to, $subject, $message, $headers);
if ($mail==true) {
// set SESSION to know that it's success result
$_SESSION['form_filled'] = true;
// redirect to the same page
header("Location: /script.php");
die();
}
}
else
{
// no post data here
// check session:
if (isset($_SESSION['form_filled']) && $_SESSION['form_filled']) {
echo '<script>$("#deletesuccess").fadeIn();$("#deletesuccess").delay(4000).fadeOut();window.location="index.php";</script>';
}
}

Submitting form and success message on same page?

So right now I have a basic contact.html with a php script set to email me if someone on my website contacts me. When they click submit, it takes them to contact_sent.php, which is a blank page. I want it so that when they click the submit, it keeps them on the same page, as well as display a success message from SweetAlert, and then clear the form so they can't spam the "Send Message" button.
<?php
if ($_POST['submit']) {
$name=$_POST['name'];
$email=$_POST['email'];
$subject=$_POST['subject'];
$message=$_POST['msg'];
$to_add="***********#gmail.com";
$from_add="form#***********.net";
$email_message="<br /><br /><b>Name:</b><br />$name <br />";
$email_message.="<br /><br /><b>Reply Email:</b><br />$email <br />";
$email_message.="<br /><br /><b>Regarding:</b><br />$subject <br />";
$email_message.="<br /><br /><b>Message:</b><br />$message <br />";
$headers="From: $from_add \r\n";
$headers.="Reply-To: $from_add \r\n";
$headers.="Return-Path: $from_add\r\n";
$headers.="X-Mailer: PHP \r\n";
$headers.="MIME-Version: 1.0\r\n";
$headers.="Content-Type: text/html;\r\n";
mail($to_add,$subject,$email_message,$headers);
}
alert("Sent!");
?>
Edit: The alert at the end doesn't work.
alert is javascript code.So you need your php script to produce that javascipt. Hope it will help you
<?php
if($_POST['submit']) {
//your other codes here
mail($to_add,$subject,$email_message,$headers);
//becareful keep the whole javascript code on mail function's success.
//I just added how to do It.It will alert too if your mail function return false.
?>
<script type="text/javascript">
alert("Sent!");
</script>
<?php
}
?>
<?
if ($_POST['submit'])
{
$name = $_POST['name'];
$email = $_POST['email'];
$subject = $_POST['subject'];
$message = $_POST['msg'];
$to_add = "***********#gmail.com";
$from_add = "form#***********.net";
$email_message = "<br /><br /><b>Name:</b><br />$name <br />";
$email_message .= "<br /><br /><b>Reply Email:</b><br />$email <br />";
$email_message .= "<br /><br /><b>Regarding:</b><br />$subject <br />";
$email_message .= "<br /><br /><b>Message:</b><br />$message <br />";
$headers = "From: $from_add \r\n";
$headers .= "Reply-To: $from_add \r\n";
$headers .= "Return-Path: $from_add\r\n";
$headers .= "X-Mailer: PHP \r\n";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/html;\r\n";
mail($to_add,$subject,$email_message,$headers);
//echo js alert
}else{ // form not submitted yet
//display form code
}
?>
What you're describing is essentially what AJAX is. If you are using jquery you can use its $.ajax function on the button press and send the data via its data property. Based on your current PHP code it might look something like this:
$.ajax({ url: '/route/to/php/function',
data: {name = nameVal,
email = emailVal,
subject = subjectVal,
message = messageVal},
type: 'post',
success: function(output) {
//do whatever after the POST is successful
}
});
You'd put the form clearing code and SweetAlert code in the same event listener for the submit button, either before or after the $.ajax call.

Categories