简体   繁体   中英

why my array value not able to retrieve and isset doesn't work for me

I'm trying to create a dynamic searching function but there is a problem I am not able to solve.

What's wrong with the following code?

JSON:

...
$result=$obj->Decode_Serialize($resource);// method to unserialize string value


$text= isset($result['text_department_code']) ? 
    $result['text_department_code']: 'a';

if(!empty($search) && !empty( $checkbox) && !empty($match) && !empty($sort) && !empty($text))
{
    echo $text= $result['text_department_code'];  // error Notice: Undefined index: text_department_code in line 18
    print $text;
    print_r($result);
}
...

I don't know why its just simply not work even isset have been included. //$result['text_department_code'];

print_r($result) output:
Array
(
    [search_deparment_code] => department_code
    [checkbox_deparment_code] => true
    [match_deparment_code] => all
    [sort_deparment_code] => ASC
    [text_deparment_code] => aa
)

//Jquery passing serialize string to ajax

function getInstantSearchResult(){
    $('#keypress').keyup(function(){      
        var Post_String = $('#forma').serialize();
        //  ajax_get_data(1,st);
        //alert(Post_String);
        $.ajax({
            type : 'POST',
            url : 'jsonAjaxFinder.php',
            data : {Query_String:Post_String},
            success : function(value){
                alert(value);
                ajax_get_data(1);
            }
        });
    });        
} 
 function ajax_get_data(page){
       var businessUnitRequest;        

       if (window.XMLHttpRequest)
       {
         businessUnitRequest=new XMLHttpRequest();
       }
       else
       {
         businessUnitRequest=new ActiveXObject("Microsoft.XMLHTTP");
       }

        var frame = document.getElementById("frame");  
        frame.style.left='20px';
        var paginations = document.getElementById("pagination");    

        businessUnitRequest.open("POST", "jsonAjaxFinder.php", true);
        businessUnitRequest.setRequestHeader("Content-type","application/x-www-form-urlencoded"); 
        businessUnitRequest.onreadystatechange = function (){
                 if((businessUnitRequest.readyState == 4) && (businessUnitRequest.status=200))
                 {

                      frame = document.getElementById("frame");  
                       var leng= frame.rows.length;
                      for (z = leng - 1; z > 0; z--){
                         frame.deleteRow(z);            
                       }
                    var d = JSON.parse(businessUnitRequest.responseText);
                    for(var o in d){
                        var row = frame.insertRow(1);
                        var cell1 = row.insertCell(0);
                        var cell2 = row.insertCell(1);

                        cell1.style.paddingLeft='15%';
                        cell2.style.paddingLeft='1%';
                        cell1.innerHTML=  d[o].department_code;
                        cell2.innerHTML= d[o].description;
                    }    

                 }
        }

             businessUnitRequest.send('no_page='+parseInt(page));
             //+'&serialize='+ serialize
             businessUnitRequest.innerHTML="requesting....";
    }

Can someone explain to me my problem because i don't know how to solve it? that

I'm not sure what you're trying to achieve here?

echo $search = $result['text_department_code']; 

If you want to echo whether or not $search matches $result[...], do this:

echo $search == $result['text_department_code'];

If you want to store $result[...] in $search and echo whether or not it fails, you'll have to check if $result[...] is set again:

echo (isset($result['text_department_code']) && $search = $result['text_department_code']);

Look at your var_dump you provided and compare it to your code. You have typos...

print_r($result) output:
Array
(
    [search_deparment_code] => department_code
    [checkbox_deparment_code] => true
    [match_deparment_code] => all
    [sort_deparment_code] => ASC
    [text_deparment_code] => aa
)

You have "Deparment" in the dump and "department" in the array indexes.

Essentially, all of your if(isset()) lines are returning false.

im not sure if this helps, but in the if statement u referenced $result['text_department_code'] but it should be $text

if(!empty($search) && !empty( $checkbox) && !empty($match) && !empty($sort) && !empty($text))
{
    echo $search = $text;
    print $search;
    print_r($result);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM