0

I get an Error by trying to get data from a SQL-Query to append the data in a Dropdown Field

This is the Error:

TypeError: right-hand side of 'in' should be an object, got string

here my funktion

function rob_ajax_tester_admin_pageabc() {
   ob_start();
   ?>
  <div class="wrap">
  <h2>Rob AJAX Tester</h2><br />
  <?php
    global $wpdb;
    $results2 = $wpdb->get_results ("SELECT display_name FROM  wp_users;");
    echo '<td><select id="wp_user" name="wp_user">';
    echo '<option value="">User:</option>';
    foreach ( $results2 as $result ) {
    echo '<option>'.$result->display_name.'</option> <br />';
    }
    echo "</select>";

    echo "</select>
   <select name=sub-category id=sub-category> </select> 
   </body>
   </html>
    ";

   return ob_get_clean();
}
add_shortcode('robajaxabc', 'rob_ajax_tester_admin_pageabc');

My Jquery

enter code here
function rob_ajax_tester_ajax_scriptabc() {
?>
<script src="http://code.jquery.com/jquery-1.11.2.min.js"  type="text/javascript"></script>

var ajaxurl = ''; jQuery(document).ready(function($) { jQuery( '#wp_user' ).change( function() {

  var wp_user = $( '#wp_user' ).val();
  var selection_value = $(this).val();

  $.ajax({
    method: "POST",
    url: ajaxurl,
    dateType: 'JSON',
    data: { 'action': 'rob_ajax_tester_approal_actionabc', 'wp_user': wp_user, 'selection_value': selection_value }
  })

.done(function(data) {
// data = JSON.parse(data);
 data =  JSON.parse(JSON.stringify(data));
  console.log('Successful AJAX Call! /// Return Data: ' + data);
            $.each(data, function(index, subcat) {
                 console.log( index, "index");
                 console.log( subcat, "subcat");
                 var option_html = "<option>" + subcat + "</option>";
                 $("#sub-category").append(option_html);
                 console.log(option_html, "option_html");
    })                                  
  })
 .fail(function( data ) {
  console.log('Failed AJAX Call :( /// Return Data: ' + data);
  });
});
});
</script>
<?php
 }
 add_action( 'wp_footer', 'rob_ajax_tester_ajax_scriptabc' );

my Function to get the Data:

enter code here
function rob_ajax_tester_ajax_handlerabc() {
global $wpdb;
$id = "hallo";
$wp_user = $_POST['wp_user'];


//$var1 = $wp_user;
//$var2 = "value2";
//echo json_encode(array($var1, $var2));


 $data = $wpdb->get_results ("SELECT user_email FROM wp_users;", ARRAY_A);
 if ($data) : foreach($data as $d) : 
 //echo json_encode(array($d['user_email']));
 echo $d['user_email'];
 endforeach; endif; 


 wp_die(); // just to be safe
 }
 add_action( 'wp_ajax_rob_ajax_tester_approal_actionabc', 'rob_ajax_tester_ajax_handlerabc' );
 add_action( 'wp_ajax_nopriv_rob_ajax_tester_approal_actionabc', 'rob_ajax_tester_ajax_handlerabc' );

In The Console i get this Message TypeError: right-hand side of 'in' should be an object, got string

If try to do it with the variables

$var1 = $wp_user;
$var2 = "value2";
echo json_encode(array($var1, $var2));

and in my Skript
data = JSON.parse(data);

Than it wokrs. But the SQL didn´t

Hope you understand my Problem.

I Don´t know how to fix it jet.

Pls Help.

THX Rob

Your Answer

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

Browse other questions tagged or ask your own question.