Добрый день !
Проблема такая , есть форма, пользователь ее заполняет и должна создасться запись в админке WordPress, но ничего не работает !
Форма:
<form id="form" action="" method="post" enctype="multipart/form-data">
<input id="name" type="text" name="name" placeholder="Ваше имя" required>
<input id="email" type="text" name="email" placeholder="Email" required></br>
<select id="sele" name="sel">
<option value="Ищу инвестиции">Ищу инвестиции</option>
<option value="Хочу продать">Хочу продать</option>
</select></br>
<textarea id="mesage" name="mesage" placeholder="Сообщение" required></textarea></br>
<input id="file" type="file" name="file[]" multiple="multiple">
<span class="erro-f" style="display:block; text-align:center; margin-top:10px;"></span>
<button class="submit">Отправить заявку</button>
</form>
Обработчик Ajax :
$(document).ready(function(){
jQuery('.submit').on('click', function(e){
e.preventDefault();
var name = jQuery('#name').val().replace(/(<([^>]+)>)/ig,"");
var email = jQuery('#email').val().replace(/(<([^>]+)>)/ig,"");
var mesage = jQuery('#mesage').val().replace(/(<([^>]+)>)/ig,"");
var sele = jQuery('#sele').val().replace(/(<([^>]+)>)/ig,"");
if (name === "" || email === "" || mesage === "") {
$('.erro-f').text('Заполните все поля').css("color", "red");
return false;
}
var objTable = {
name: name,
email: email,
mesage: mesage,
sele: sele
};
console.log(objTable);
$.ajax({
url: '/wp-content/themes/buyinvest/form/mail.php',
type: "POST",
data: objTable,
success: function(response) {
$(".erro-f").text('Заявка отправленна!').css("color", "green");
$('#form').find('input[type=text], textarea').val('');
},
error: function(error) {
console.log("error");
}
});
});
});
Обработчик PHP и тут же должен добавлять посты и сохранять их:
<?php
$name = $_POST['name'];
$email = $_POST['email'];
$mesage = $_POST['mesage'];
$sele = $_POST['sel'];
/*$name = htmlspecialchars($name);
$email = htmlspecialchars($email);
$mesage = htmlspecialchars($mesage);
$sele = htmlspecialchars($sele);
$name = urldecode($name);
$email = urldecode($email);
$mesage = urldecode($mesage);
$sele = urldecode($sele);
$name = trim($name);
$email = trim($email);
$mesage = trim($mesage);
$sele = trim($sele);*/
$post_data = array(
'post_title' => $email,
'post_content' => "Имя:" . '<br/>'. $name . '<br/>'."Email:" . '<br/>'. $email . '<br/>'."Услуга:" . '<br/>'. $sele . '<br/>'. '<br/>'. "Сообщение:" . $mesage,
'post_status' => 'publish',
'post_author' => 1,
'post_type' => 'post'
);
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
require_once( ABSPATH . 'wp-admin/includes/image.php' );
require_once( ABSPATH . 'wp-admin/includes/file.php' );
require_once( ABSPATH . 'wp-admin/includes/media.php' );
$files = $_FILES["file"];
foreach ($files['name'] as $key => $value) {
if ($files['name'][$key]) {
$file = array(
'name' => $files['name'][$key],
'type' => $files['type'][$key],
'tmp_name' => $files['tmp_name'][$key],
'error' => $files['error'][$key],
'size' => $files['size'][$key]
);
$_FILES = array("file" => $file);
$attachment_id = media_handle_upload("file", 0);
if (is_wp_error($attachment_id)) {
// There was an error uploading the image.
echo "Error adding file";
} else {
// The image was uploaded successfully!
echo "File added successfully with ID: " . $attachment_id . "<br>";
echo wp_get_attachment_image($attachment_id, array(800, 600)) . "<br>";
}
}
}
};
$post_id = wp_insert_post($post_data);
update_post_meta($post_id,'file',$attachment_id);
?>