On Sunday, I made a working script to send form data into my database and email with Mandrill.
Since Wednesday, my script doesn't work anymore. I'm trying to find the error but nothing fixed it.
I think I have an error in my PHP script but I don't know where and what. Console log of my AJAX returns correct data but PHP doesn't do anything. It doesn't receive data.
There is my HTML
<form method="POST" id="formContact" action"#">
<input type="text" placeholder="Prénom" name="name" id="formName">
<input type="email" placeholder="Email" name="email" id="formEmail">
<input type="text" placeholder="Sujet" name="subject" id="formSubject">
<textarea cols="30" rows="10" placeholder="Ton message" name="message" id="formMessage"></textarea>
<button type="submit" id="formSubmit">Envoyer</button>
</form>
JavaScript
$(document).ready(function() {
$('#formContact').submit(function(event) {
var formData = {
'name' : $('#formName').val(),
'email' : $("#formEmail").val(),
'subject' : $("#formSubject").val(),
'message' : $("#formMessage").val()
};
$.ajax({
url: "commons/test.php",
type: "POST",
data: formData,
dataType: 'json',
encode: true
})
.done(function(data){
console.log(formData);
console.log(data);
if( ! data.success){
alert('Error');
}else{
alert('Success');
}
});
event.preventDefault();
});
});
PHP
<?php
error_reporting(E_ALL);
ini_set("display_errors", 1);
include('../adm/dbconnect.php'); <- this work well
$data = array();
if (!empty($_POST['name']) && !empty($_POST['email']) && !empty($_POST['subject']) && !empty($_POST['message']))
{
// var_dump($_POST['name']);
// var_dump($_POST['email']);
// var_dump($_POST['subject']);
// var_dump($_POST['message']);
$to = 'contact@sheguey.land';
$content = ''.$_POST['message'].'';
$subject = 'Nouveau message de '.$_POST['email'].' - Sheguey Land';
$from = ''.$_POST['email'].'';
$uri = 'https://mandrillapp.com/api/1.0/messages/send.json';
$api_key = 'my mandrill 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);
$req = $bdd->prepare('INSERT INTO contactform (name, email, subject, message) VALUES( :name, :email, :subject, :message)');
$req->execute(array(
'name' => $_POST['name'],
'email' => $_POST['email'],
'subject' => $_POST['subject'],
'message' => $_POST['message']
));
$data['success'] = true;
}else{
$data['success'] = false;
}
echo json_encode($data);
After two days of research (changing PHP script, AJAX script, etc), I don't understand why this don't work anymore.