i'm trying try pretty simple request , response thing using $.ajax via. json. have tried alot return simple text phrase php file. js part of code goes calls beemailer.php response.
$.ajax({ type: 'post', datatype: "json", contenttype: "application/json; charset=utf-8", url: 'beemailer.php', data: { 'name': data_name, 'email': data_email, 'message': data_message, 'subject': data_subject }, success: function (data) { var output = jquery.parsejson(data); console.log(output); alert(output); }, error: function (error, x, y) { console.log(error); alert(x, y); } ) };
and beemailer.php this:
<?php $to = "ashish_sharma307@hotmail.com"; $subject = $_post['subject']; $message =$_post['message']; $headers = "from:".$_post['name']."(".$_post['email'].")"; mail($to,$subject,$message,$headers); $response = "the mail has been sent. thank valid mail."; header("content-type: application/json; charset=utf-8", true); echo json_encode($response); ?>
what need $response text alerted. appreciated.
remove below code ajax
call , try:
contenttype: "application/json; charset=utf-8",
jquery.parsejson(data)
not needed content-type
set , encoded in json
below code:
header("content-type: application/json; charset=utf-8", true); echo json_encode($response);
updated code test data:
js
$.ajax({ type: 'post', datatype: "json", url : 'beemailer.php', data: {name: 'rai', email: 'rai@test.com', message: 'test message', subject: 'test subject'}, success : function(data) { console.log(data); alert(data); }, error: function(error, x, y) { console.log(error); alert(x, y); } });
php (beemailer.php)
<?php $to = "ashish_sharma307@hotmail.com"; $subject = $_post['subject']; $message = $_post['message']; $headers = "from:" . $_post['name'] . "(" . $_post['email'] . ")"; mail($to, $subject, $message, $headers); $response = "the mail has been sent. thank valid mail."; header("content-type: application/json; charset=utf-8", true); echo json_encode($response); ?>
example of object:
$result = array(); $result['status'] = 'success'; $result['message'] = 'the mail has been sent. thank valid mail.'; header("content-type: application/json; charset=utf-8", true); echo json_encode($result);
js access object:
$.ajax({ type: 'post', datatype: "json", url : 'beemailer.php', data: {name: 'rai', email: 'rai@test.com', message: 'test message', subject: 'test subject'}, success : function(data) { alert('status: ' + data.status + '\nmessage: ' + data.message); }, error: function(error, x, y) { console.log(error); alert(x, y); } });
Comments
Post a Comment