Forum Replies Created

Viewing 5 replies - 1 through 5 (of 5 total)
  • Thread Starter jamiesw

    (@jamiesw)

    In my console output i am now getting a 400 Bad Request (VM2647:1 POST http://my.desktopserver.dev/wp-admin/admin-ajax.php 400 (Bad Request))

    Calling my php function directly on the page works and echo’s out the correct info on the page, so i know the function works.
    also in my jsscript.js i did an alert(residential_visits.ajaxurl); and it shows the admin-ajax.php url as it is meant to.

    JSSCRIPTS FILE:

    jQuery(document).ready(function() {
    jQuery('#ddlSchool').change(function(){
    //var st=$('#category option:selected').text();
    var school=jQuery('#ddlSchool').val();
    jQuery('#ddlResVisit').empty(); //remove all existing options
    jQuery.ajax({
    			type: 'POST',
    			dataType: 'json',
    			contentType: "application/json",
    			url: residential_visits.ajaxurl,
    			data: {
    				'action' : 'residential_visits',
    				'school' : school
    			},
    			complete: function( response ) {
    				console.log(response);
    				//alert(response.responseJSON.body);				
    			}
    		});
    	});
    });

    PHP file in my custom plugin folder (this php file also creates the content used on the page as a shortcode, I dont know if this make a difference? Does my PHP function need to be elsewhere for it to be seen by the ajax handler?

    
    <?php
    // Registration form
    //Load CSS and Javascript Files
    wp_register_style('reg_styles', plugins_url('css/styles.css', __FILE__));
    wp_enqueue_style('reg_styles');
    
    wp_register_script('ajax-script', plugins_url('js/jsscripts.js', __FILE__), array('jquery'),'1.1');
    wp_enqueue_script('ajax-script');
    wp_localize_script( 'ajax-script', 'residential_visits', array( 'ajaxurl' => admin_url( 'admin-ajax.php' ) ) );
     // THE AJAX ADD ACTIONS
    add_action( 'wp_ajax_residential_visits', 'residential_visits');
    add_action( 'wp_ajax_nopriv_residential_visits', 'residential_visits');
    //residential_visits(); -WHEN ENABLED IT SHOWS THE DATA SO IT WORKS
    
    function residential_visits() {
    ob_clean();
    // Handle request then generate response using WP_Ajax_Response
    global $wpdb;
    $result = $wpdb->get_results("select id, group_name from wp_pods_residential_visits ");
    //$main = array('data'=>$result);
    echo json_encode($result);
    // Don't forget to stop execution afterward.
    wp_die();
    }
    }
    if (!empty($_POST)){
    //Pass Form Data to Save Function
    saveRegData($_POST);
    echo "<h2>Thank you for submitting the registration information.</h2>";
    }
    else{
    //Display Registration Form
    ?>
    <form method="post" name="frmVisit" id="frmVisit">
    <table id="tblRegistration" name="tblRegistration" class="tblRegForm">
    <!-- Other form Fields -->
    <tr>
    <td colspan="2"><h3>School Details</h3></td>
    </tr>
    <tr>
    <td colspan="2">
    <?php 
    // Get list of schools
    global $wpdb;
    $schools = $wpdb->get_results("Select abbr_code, school_name from wp_pods_schools order by school_name;");
    ?>
    <label for"ddlSchools">School Attending:</label>
    <select name="ddlSchool" id="ddlSchool">
    <?php
    foreach ($schools as $school){
    ?>
    <option value="<?php echo $school->abbr_code ?>"><?php echo $school->school_name ?></option>
    <?php } ?>
    </select>
    
    <select id="ddlResVisit" name="ddlResVisit">
    </select>
       <!-- Rest of HTML, PHP to save data etc -->
    

    Any clues?

    Jamie

    Thread Starter jamiesw

    (@jamiesw)

    sorry yes it is meant to be $result = $wpdb->get_results('your SQL here'); but even updating that hasn’t made a difference, any where else i have missed a bit out or misspelled something?
    many thanks
    Jamie

    Thread Starter jamiesw

    (@jamiesw)

    Hi theee,
    Thanks for replying, creating the tables with the wordpress database isn’t an option. The other db is an exisiting crm that is in use. From what I have read online so far what i am trying to do should work.
    Thanks
    Jamie

    Thread Starter jamiesw

    (@jamiesw)

    Hi Susan\Justin,
    thanks for the advice. I did think about using triggers in mysql to copy the data from one to another, will look at OAuth as a possiblity as well.
    Thanks again.

    Thread Starter jamiesw

    (@jamiesw)

    Thanks, I will look into PODS, looks promising, if you have any tips or tricks it would be much appreciated.

    Thanks
    Jamie

Viewing 5 replies - 1 through 5 (of 5 total)