Before I submit a form in WordPress, I try to pass some form values to a PHP script using jQuery Ajax to check whether similar posts already exist.
The first part of the jQuery works fine. However I am unsure, if the ajax passes the values to the PHP, because it always throws the alert('something went wrong');. I'm quite a bit lost, it's the first time, that I try to use Ajax.
jQuery(document).ready(function($) {
$('.madison').each(function(){
var _this = $(this) // get the loop item's div.madison
_this.find("select[name='trainingsort']").change( function() {
var _trainingsort = $("select[name='trainingsort']").val();
var vonzeit = "select[name='von-uhrzeit-" + _trainingsort + "']";
var biszeit = "select[name='bis-uhrzeit-" + _trainingsort + "']";
var _vonzeit = $(vonzeit).val();
var _biszeit = $(biszeit).val();
var _tag = $("input[name='tag']").val();
var _reitanlage_id = $("input[name='reitanlagen_id']").val();
var ort = _trainingsort + ' / ' + _vonzeit + ' / ' + _biszeit + ' / ' + _reitanlage_id + ' / ' + _tag;
alert( "Yes! " + ort );
_this.find("input[name='wpcf-rask-name-des-menschen']").val(ort);
// ----- everything works fine above this line ---------------------
$.ajax({
type: "POST",
url: "https://test.cuteberry.de/wp-content/toolset-customizations/trainingsanlagen-2-0__ajax-notification.php",
data: {trainingsort: _trainingsort, vonzeit: _vonzeit, biszeit: _biszeit, reitanlagen_id: _reitanlage_id, tag: _tag},
success: function(data){
alert(data);
},
error: function(data){
alert('something went wrong');
}
});
});
});
});
and the PHP ....
PHP:
<?php
/**
* New custom code snippet (replace this with snippet description).
*/
toolset_snippet_security_check() or die( 'Direct access is not allowed' );
// Put the code of your snippet below this comment.
// You can access the values posted by jQuery.ajax
// through the global variable $_POST, like this:
$trainingsort=isset($_POST['trainingsort'])?json_decode($_POST['trainingsort']):null; $vonzeit=isset($_POST['vonzeit'])?json_decode($_POST['vonzeit']):null; $biszeit=isset($_POST['biszeit'])?json_decode($_POST['biszeit']):null;
$reitanlagen_id=isset($_POST['reitanlagen_id'])?json_decode($_POST['reitanlagen_id']):null; $tag=isset($_POST['tag'])?json_decode($_POST['tag']):null;
$response = "";
// ----- everything works fine below this line ---------------------
if (isset($reitanlagen_id) && !empty($reitanlagen_id)) {
for ($i = $vonzeit; $i <= $biszeit; $i++) {
// Warteliste_Posts suchen
$query = new WP_Query(
array(
'post_type' => 'rask',
'post_status' => 'publish',
'posts_per_page' => -1,
'toolset_relationships' => array(
'role' => 'child',
'related_to' => $reitanlagen_id,
'relationship' => 'reitanlage-rask'
),
'meta_query' => array(
'relation' => 'AND',
array(
'key' => 'wpcf-rask-ort-des-geschehens',
'value' => $trainingsort,
'compare' => '='
),
array(
'key' => 'wpcf-rask-welches-journal-termine',
'value' => 1,
'compare' => '='
),
array(
'key' => 'wpcf-rask-von-tag',
'value' => $tag,
'type' => 'numeric',
'compare' => '='
),
array(
'key' => 'wpcf-rask-von-uhrzeit-15-minuten',
'value' => $i,
'type' => 'numeric',
'compare' => '<='
),
array(
'key' => 'wpcf-rask-bis-uhrzeit-15-minuten',
'value' => $i ,
'type' => 'numeric',
'compare' => '>='
),
)
)
);
$pferde_posts = $query->posts;
$count = count($pferde_posts);
// ----- everything works fine above this line ---------------------
if ($count >= 0) {
$response = json_encode("existiert");
break;
}
}
}
echo $response;
I'm thankfull for any hints in the right direction : )