1

I need your help as I want to fetch some data from wordpress table based on criteria from a textbox. That data is stored in an array and I want that data to be copied to a selection box with ajax and jquery. The data is not getting copied to the selection list options. I want to send the array for form submission or pressing search to the ajax which use for loop to assign that array results to options in a simple selection box. Below is my code. Thanks everybody

Html Code

<SELECT id=”myselect”></SELECT>

Php Function

add_action( 'wp_ajax_wp_hello', 'wp_hello' );
add_action( 'wp_ajax_nopriv_wp_hello', 'wp_hello');

function wp_hello()
{

$secretcode=$_POST['secretcode'];

//main logic
global $wpdb;
//echo jsonencode($secretcode);

$sql = "SELECT DISTINCT * FROM wp_store_locator WHERE sl_store LIKE '$secretcode%%' ";
$results = $wpdb->get_results($sql) or die(mysql_error());
$takeit = array();

foreach( $results as $result ) {

 echo $takeit[].= $result->sl_store;


    }

exit();

}

Jquery Code

jQuery(function ($) {

    //$("#myform").submit(function (e) { //form is intercepted
      //  e.preventDefault();
        $("#secret").keyup(function(){
       var a = $("#secret").val();

        //serialize the form which contains secretcode
        var sentdata = $(this).serializeArray();

        //Add the additional param to the data        
        sentdata.push({
            name: 'action',
            value: 'wp_hello'
        })

        //set sentdata as the data to be sent
        $.post(yes.ajaxurl, sentdata, function (rez) { //start of funciton

                $("#result").append(rez);
            for (var i=0; i<rez.length; i++)
            {
                $("#myselect").append('<option value="' + rez.eq(i) + '">' + rez[i]+rez+ '</option>'); 


            }
            return false;

        } //end of function
        ,
        'html'); //set the dataType as json, so you will get the parsed data in the callback
        });
Zeeshan
  • 323
  • 1
  • 7
  • 23

0 Answers0