我正嘗試在ajax發送調用中發送數據。但是這並沒有發送任何數據。API不會從ajax調用發送任何發佈數據
我的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>
我的劇本是
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= '[email protected]&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");
}
});
}
我的PHP代碼
if(isset($_POST['email'])){
echo json_decode($_POST['email']);
}
這不打印anything..I認爲這是一個問題阿賈克斯call.please幫助..
我該如何發送json?它是否需要在標題中指定? –
你爲什麼要這麼做? PHP不需要JSON,它不會將其解碼爲'$ _POST'。 – Barmar