I am trying to send data in ajax post call. but this doesnot sent any data.
我正在尝试用ajax post call发送数据。但这并没有发送任何数据。
My html form is
我的html表单
<form>
<div class="row">
<div class="col-sm-6">
<div class="form-group">
<label for="firstname">Firstname</label>
<input id="firstname" type="text" class="form-control" id="firstname">
</div>
</div>
<div class="col-sm-6">
<div class="form-group">
<label for="lastname">Lastname</label>
<input id="lastname" type="text" class="form-control" id="lastname">
</div>
</div>
<div class="col-sm-6">
<div class="form-group">
<label for="email">Email</label>
<input id="email" type="text" class="form-control" id="email">
</div>
</div>
<div class="col-sm-6">
<div class="form-group">
<label for="subject">Subject</label>
<input id="subject" type="text" class="form-control" id="subject">
</div>
</div>
<div class="col-sm-12">
<div class="form-group">
<label for="message">Message</label>
<textarea id="message" class="form-control"></textarea>
</div>
</div>
<div class="col-sm-12 text-center">
<a type="submit" class="btn btn-template-main" onclick="sendMail()"><i class="fa fa-envelope-o"></i> Send message</a><span id="sentMessage" style="color:green"></span>
</div>
</div>
<!-- /.row -->
</form>
My script is
我的脚本
function sendMail(){
var name = document.getElementById ("firstname").value + " " + document.getElementById ("lastname").value;
var subject = document.getElementById ("subject").value;
var email = document.getElementById ("email").value;
var message = document.getElementById ("message").value;
var body = "<h3>Message from "+ name +"</h3>"+
"<h4>Email: "+ email +"</h4>"+
"<p><strong>Body : </strong> "+ message +"</p>";
var email= 'to=tsmrafee@gmail.com&subject=' + subject + '&body=' + body;
console.log("done");
$.ajax({
url: 'http://trimarkworld.com/email.php',
method: 'POST',
data: {
email: email
},
success: function(result){
console.log("done");
$("#sentMessage").html("Email has been Sent");
}
});
}
my php code is
我的php代码是
if(isset($_POST['email'])){
echo json_decode($_POST['email']);
}
This does not print anything..I think there is a problem in the ajax call.please help..
这没有打印任何东西。我认为ajax调用中有一个问题。请帮助. .
3 个解决方案
#1
3
When you send data with $.ajax
, it's not sent as JSON, it's sent in application/x-www-form-urlencode
format. So there's no need to call json_decode()
. Just use:
当你用$发送数据时。它不是作为JSON发送的,而是通过应用程序/x-www-form-urlencode格式发送的。所以不需要调用json_decode()。只使用:
echo $_POST['email'];
#2
0
You should use json_encode
in your php if your serializing an associative array. Generally I would send back an object, Although in this case if you are just sending a string you don't need to encode the value.
如果序列化关联数组,应该在php中使用json_encode。通常我会返回一个对象,虽然在这种情况下,如果只是发送一个字符串,则不需要对值进行编码。
if(isset($_POST['email'])){
echo json_encode($_POST['email']);
}
#3
0
Your PHP is expecting JSON so you might want to pass stringified JSON.
PHP需要JSON,因此可能需要传递经过字符串化的JSON。
This example will decode the JSON first, then you can use isset.
这个示例将首先解码JSON,然后您可以使用isset。
Use this example to refactor your code: https://www.sitepoint.com/jquery-php-ajax-json/
使用此示例重构代码:https://www.sitepoint.com/jquery-php-ajax-json/
#1
3
When you send data with $.ajax
, it's not sent as JSON, it's sent in application/x-www-form-urlencode
format. So there's no need to call json_decode()
. Just use:
当你用$发送数据时。它不是作为JSON发送的,而是通过应用程序/x-www-form-urlencode格式发送的。所以不需要调用json_decode()。只使用:
echo $_POST['email'];
#2
0
You should use json_encode
in your php if your serializing an associative array. Generally I would send back an object, Although in this case if you are just sending a string you don't need to encode the value.
如果序列化关联数组,应该在php中使用json_encode。通常我会返回一个对象,虽然在这种情况下,如果只是发送一个字符串,则不需要对值进行编码。
if(isset($_POST['email'])){
echo json_encode($_POST['email']);
}
#3
0
Your PHP is expecting JSON so you might want to pass stringified JSON.
PHP需要JSON,因此可能需要传递经过字符串化的JSON。
This example will decode the JSON first, then you can use isset.
这个示例将首先解码JSON,然后您可以使用isset。
Use this example to refactor your code: https://www.sitepoint.com/jquery-php-ajax-json/
使用此示例重构代码:https://www.sitepoint.com/jquery-php-ajax-json/