admin管理员组

文章数量:1332383

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

<script>
    
    var eventId;
    var xmlhttp;
    var xmlhttp = new XMLHttpRequest;
    
    function doclick(ddl){
        $evtid=-1;      
        $evtid =ddl.options[ddl.selectedIndex].value;
              document.getElementById('hideme').value = 
              ddl.options[ddl.selectedIndex].value;
              document.getElementById('ename').value = 
              ddl.options[ddl.selectedIndex].text;
              xmlhttp = new XMLHttpRequest();
              xmlhttp.onreadystatechange = callback;
              xmlhttp.open("POST", "/id.php", true);
              xmlhttp.send();            
        }
          
    function callback(){
    
        if(xmlhttp.readyState==4 && xmlhttp.status==200)
        {
            document.getElementById('DESCRIPTION').innerHTML = xmlhttp.responseText;
        }
    }
</script>

$evtid is getting the correct data.

The id.php file:

<?php
$wpdb;
$sql='';
$eid;
$result;
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';

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

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

$evtid is getting the correct data.

The id.php file:

<?php
$wpdb;
$sql='';
$eid;
$result;
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';

Share Improve this question asked Jul 3, 2020 at 19:36 GreatDayDanGreatDayDan 153 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 0
  • 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.

本文标签: How to return a record from a custom table by sending an AJAX request