0
我做了一個聯繫表單發送帶有Ajax請求的電子郵件,這個請求從另一個文件調用一個PHP函數。此功能運行良好,但在調用此功能後,每個表單數據都會丟失。Ajax + Php函數
HTML:
<form method="post" id="formContact">
<fieldset>
<legend>Formulaire contact de Bicrav'art</legend>
<div>
<input type="text" placeholder="Nom Prénom" name="name" id="formName">
<input type="tel" placeholder="Téléphone" name="tel" id="formTel">
<input type="email" placeholder="Email" name="email" id="formEmail">
<input type="text" placeholder="Objet" name="subject" id="formSubject">
</div>
<textarea cols="30" rows="10" placeholder="Message" name="message" id="formMessage"></textarea>
<button type="submit" id="formSubmit">Envoyer</button>
</fieldset>
</form>
AJAX:
$(document).ready(function() {
$('#formContact').submit(function(event) {
var formData = {
// 'functionName' : 'contact',
'name' : $('#formName').val(),
'email' : $("#formEmail").val(),
'tel' : $("#formTel").val(),
'subject' : $("#formSubject").val(),
'message' : $("#formMessage").val()
};
console.log(formData);
$.ajax({
url: "includes/functions",
type: "POST",
data: {action: 'contact', contactData: formData},
dataType: 'json',
encode: true
})
.done(function(data){
console.log(formData);
console.log(data);
if(!data.success){
console.log('EMAIL SEND');
}else{
console.log('EMAIL NOT SEND');
}
});
event.preventDefault();
});
});
PHP函數:
<?php
// error_reporting(E_ALL);
// ini_set("display_errors", 1);
if(isset($_POST['action'])) {
$action = $_POST['action'];
switch($action) {
case 'contact' : sendEmail();
}
}
// SEND EMAIL
function sendEmail(){
$data = $_POST['contactData'];
$data = array();
if (!empty($_POST['name'])
&& !empty($_POST['email'])
&& !empty($_POST['tel'])
&& !empty($_POST['subject'])
&& !empty($_POST['message']))
{
// var_dump($_POST['name']);
// var_dump($_POST['email']);
// var_dump($_POST['tel']);
// var_dump($_POST['subject']);
// var_dump($_POST['message']);
$to = '*****@hotmail.fr';
$content = ''.$_POST['message'].'';
$subject = 'Nouvelle demande de contact - Bicrav\'Art';
$from = ''.$_POST['email'].'';
$uri = 'https://mandrillapp.com/api/1.0/messages/send.json';
$api_key = '****************';
$content_text = strip_tags($content);
$postString = '{
"key": "' . $api_key . '",
"message": {
"html": "' . $content . '",
"text": "' . $content_text . '",
"subject": "' . $subject . '",
"from_email": "' . $from . '",
"from_name": "' . $from . '",
"to": [
{
"email": "' . $to . '",
"name": "' . $to . '"
}
],
"track_opens": true,
"track_clicks": true,
"auto_text": true,
"url_strip_qs": true,
"preserve_recipients": true
},
"async": false
}';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $uri);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $postString);
$result = curl_exec($ch);
$data['success'] = true;
}else{
$data['success'] = false;
}
echo json_encode($data);
}
的sendEmail功能運作良好,但形式DATAS不通過這個
if(isset($_POST['action'])) {
$action = $_POST['action'];
switch($action) {
case 'contact' : sendEmail();
}
}
THX對您有所幫助, 對不起我的英文不好
你可以用'print_r($ _ POST)'檢查PHP文件嗎,它返回什麼? – Sadikhasan
我認爲'$ _POST ['name']'應該是''__POST ['contactData'] ['name']'存儲在'contactData'數組中的名稱 –
您沒有使用正確的方式發送您的數據與jQuery阿賈克斯你不會得到任何東西在你的PHP頁面 –