Jaroslaw Jasinski
Jaroslaw Jasinski

Reputation: 15

Ajax returns null

I have a problem with Ajax response. I'm trying to display a property address via specified ID number from mySql database. The problem I found is that, when I set the value of the id as static e.g prop_id = 2, then the address appears is successfully retrieved from db. But when I want to search by the prop_id entered in the text box I keep receiving null value.

html

       <td><label for="propertyId">Property Id:</label></td>
       <td><input name="p_propertyId" id="propertyId" onClick="suggestion_property_id()" onblur="get_property_address_byid()" size="40"></td> <!--onblur="get_idproperty()"-->
       <td><label for="prop_address">Property address:</label></td>
       <td><input name="p_address" id="prop_address" value="" size="40" readonly/></td>

ajax

    var $propertyId = $('#propertyId').val();
       //alert($propertyId);
       $.ajax({
                 type: 'POST',
                 url: 'propertyId.php',
                 data: {
                      type:'prop_address',
                      $propertyId:$propertyId,
                      },
                      async: true,
                      dataType: 'text',
                      success: function(res1){
                            var results = eval(res1);
                            document.getElementById('prop_address').value = results[0];
                            //get_idbooking();
                            alert($propertyId);
                            console.log(res1);
                     },
               }); 
            }

php

if($type == 'prop_address'){
       $prop_address = "no address";
       $propertyId =$_POST['propertyId'];
       $propertyId=155;          
       $properties = get_address_by_propertyid($propertyId);

       for($i=0; $i<sizeof($properties); $i++){
              $address_prop = str_replace(' ', ' ', $properties[$i]['prop_address']);
              if(strpos($propertyId, $address_prop) !== false){
                     $prop_address = $properties[$i]['propertyId'];
                     break;
              }
       }
       $res1 = array($address_prop, $propertyId);
       echo json_encode($res1);
       //echo json_encode($address);
}

Upvotes: 1

Views: 90

Answers (4)

ConroyP
ConroyP

Reputation: 41886

In your ajax block, you're passing across the $propertyId value as both name and value. So, if it's 155, for example, your data block is effectively being sent to the server as:

data: {
    type: 'prop_address',
    155: 155
}

If you update the data block to name the propertyId parameter correctly, then your php script should pick it up.

data: {
    type: 'prop_address',
    propertyId: $propertyId
}

If you run in to these sort of issues in future (script works ok with value hard-coded but not when it's passed from elsewhere) a good place to start debugging is to have the php script print_r($_POST) or var_dump($_POST) to ensure you're passing across the parameters you think you are.

Upvotes: 0

František Maša
František Maša

Reputation: 364

You assign $propertyId to wrong key:

Should be:

...
data: {
    type:'prop_address',
    propertyId: $propertyId
    // $propertyId: $propertyId
 },
...

Upvotes: 0

Kamehameha
Kamehameha

Reputation: 5473

The data that you are sending in the Ajax request seems to be wrong -

$propertyId:$propertyId,
^^^^^^^^^^^

should be -

propertyId:$propertyId,

Upvotes: 1

Elias
Elias

Reputation: 1562

Be sure to heck the incoming data on the server side with, for example, print_r($_POST). It looks like you are sending:

data: {
    type:'prop_address',
    $propertyId:$propertyId,
}

but checking for

$_POST['propertyId']

I don't think they match.

Upvotes: 0

Related Questions