I am trying to return a record from a custom table. On the client side, I am sending an AJAX request.

    var eventId;
    var xmlhttp;
    var xmlhttp = new XMLHttpRequest;
    function doclick(ddl){
        $evtid =ddl.options[ddl.selectedIndex].value;
              document.getElementById('hideme').value = 
              document.getElementById('ename').value = 
              xmlhttp = new XMLHttpRequest();
              xmlhttp.onreadystatechange = callback;
              xmlhttp.open("POST", "http://www.memoriesof.website/id.php", true);
    function callback(){
        if(xmlhttp.readyState==4 && xmlhttp.status==200)
            document.getElementById('DESCRIPTION').innerHTML = xmlhttp.responseText;

$evtid is getting the correct data.

The id.php file:

echo print_r($result);
echo 'Howdy: '. print_r($eid);
try {
$sql='select description from mo_events where id = '. '11';
$result = $wpdb.get_results($sql);
    echo "Im'''trying...";    
} catch (Exception $exc) {
    echo $exc->getTraceAsString();
    echo 'Caught me.';
} finally {    
    echo "finally...out: ". print_r($result);
echo 'all the way... : '.print_r($result);

This is what shows on the Description textarea: "1Howdy: 1finally...out: 1}

So it looks like I have the client side working. Can someone help me figure out how to get the data back to the client? Thanks...Dan';

  • Your code (both client-side and server-side) makes no sense and is very ugly.
  • You're not loading WordPress in your custom PHP script, so there's no way $wpdb would work.
  • To call a method of an object, you should do $wpdb->get_results('... sql ...').

You shouldn't call PHP scripts directly anyway. It's better to create a REST endpoint or a rewrite endpoint inside your theme or plugin.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.