0

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 : )

  • 2
    Maybe start by reading this: https://stackoverflow.com/questions/43557755/how-to-call-ajax-in-wordpress – Howard E Dec 28 '21 at 00:10
  • 1
    You aren't sending json so get rid of `contentType`. Then inspect the actual request in browser dev tools network and check status, what is sent, what is returned etc to start narrowing down where the problem is – charlietfl Dec 28 '21 at 00:11
  • 1
    Also your endpoint should not be in `wp-content`. – charlietfl Dec 28 '21 at 00:14
  • 1
    What is the response you get from your AJAX payload? You can check console or Network tab. – Twisty Dec 28 '21 at 01:19
  • 1
    https://test.cuteberry.de/wp-content/toolset-customizations/trainingsanlagen-2-0__ajax-notification.php this is return 500 error – Vel Dec 28 '21 at 08:45
  • How do I change this_data: {trainingsort: _trainingsort, vonzeit: _vonzeit, biszeit: _biszeit, reitanlagen_id: _reitanlage_id, tag: _tag}" to json? Is it – Lara Kreisz Dec 28 '21 at 08:47
  • 1
    just remove this 2 lines of code in ajax call `contentType: "application/json; charset=utf-8", dataType: "json",` – Vel Dec 28 '21 at 08:48
  • Thanks @Vel, alright I will do that. If it's not json, how do I access it in the php file? – Lara Kreisz Dec 28 '21 at 08:53
  • 1
    we can access without mention datatype. – Vel Dec 28 '21 at 08:53

0 Answers0