0

I'm tring to send and receive parameters with AJAX without any sucess

First I choose AREA and than the CITIES in this area.

Can you please tell me what do I do wrong?

Client side:

<script>
$(document).ready(function(){

  $("#first").click( 

    function(){

        var area_id=$("#area_id").val();

        $.ajax({
        type: "POST",
        url: "recs.php",
        data: "area_id="+area_id,
        cache:false,
        success: 
          function(data){
            $("#second").html(data.message); 
          }

        });

      return false;

    });


});


</script>


<form method="post" action="tosomewhere.php">

    <select id="first" name="area_id">
      <option value="1">1</option>
      <option value="2">2</option>
    </select>

    <select id="second" name="section">  </select>

</form>

Server Side:

$areaID = $_POST['area_id'];
$second_option = "";

$query2 = mysql_query("SELECT * FROM `cities` WHERE area_id = $areaID ORDER BY id ASC");
while($index = mysql_fetch_array($query2)) 
{
    $id = $index['id'];
    $name  = $index['name'];

    $second_option .= "<option value='$id'>$name</option>";
}

echo $second_option;
exit;

Thank you in advanced

After editing:

I changed the code to something even simpler:

Client side:

<script>
$(document).ready(function(){
  $("#first").click( 
    function(){
        var area_id=$("#area_id").val();

        $.ajax({
        type: "GET",
        url: "recs.php",
        data: "area_id="+area_id,
        cache:false,
        success: 
          function(data){
            $("#second").html(data); 

          }

        });

      return false;

    });


});


</script>

<form method="post" action="tosomewhere.php">

    <select id="first" name="area_id">
      <option value="1">1</option>
      <option value="2">2</option>
    </select>

    <div id="second"></div>

</form>

Server side:

some text

I'm still not getting the string into

5
  • What is wrong? retrieve some error in your console? Commented Aug 22, 2013 at 8:43
  • I found this nice tuto, so i share it with you w3schools.com/php/php_ajax_database.asp Commented Aug 22, 2013 at 8:43
  • What do you mean by "without success"? What happens when your code runs? Commented Aug 22, 2013 at 8:44
  • Second SELECTBOX doesn't fiiled with data...@AleksG Commented Aug 22, 2013 at 8:49
  • maybe I didn't build the ajax script right...? Commented Aug 22, 2013 at 12:16

4 Answers 4

3

change

$("#second").html(data.message); 

to

$("#second").html(data); 
Sign up to request clarification or add additional context in comments.

3 Comments

I'm still not getting respond from the server side. I tried to type simple string instead of the SQL query, and it's still not send to the client side...
Use echo '<option>test</option>'; instead of echo $second_option;. If you see that then the problem is in your mysql query.
Please check again my first post. I edited it and add new simple code (After editing:)
0
<script>
$(document).ready(function(){
  $("#first").click(function(){
        var area_id=$("#area_id").val();
        $("#second").load('tosomewhere.php?area_id=' + area_id);
      return false;
    });
});
</script>

Changed the jquery a bit. Using GET.

Also the script has changed:

$areaID = (int) $_GET['area_id'];
$second_option = "";

$query2 = mysql_query("SELECT * FROM `cities` WHERE area_id = '$areaID' ORDER BY id ASC");
if (mysql_num_rows($query2) > 0){    
    while($index = mysql_fetch_array($query2)){
        $second_option .= '<option value="'.$index['id'].'">'.$index['name'].'</option>';
    }
    echo $second_option;
} else {
    echo 'No result!';
}

die();

Added (int) before $_GET as a pre-security measurement.

7 Comments

I'm still not getting respond from the server side. I tried to type simple string instead of the SQL query, and it's still not send to the client side...
make sure tosomewhere.php?area_id is pointing to the same directory. Try to add the full path to the script first. Then try this string in your browser: tosomewhere.php?area_id=5 You should get a response. If you dont, then something is wrong in your php script
Try the new serverscript, it will echo something even if there is something wrong with your query
Please check again my first post. I edited it and add new simple code (After editing:)
Try opening F12 in your browser. Go to the network tab and click on the #first. You should see a new request appear. If it is in red, it means that the request url is not correct. If it is not red, you can check the response somewhere there too. I have tried your new script and mine and they are both working. I think the path is not pointing to your server script
|
0

Add this parameter to the ajax function

dataType:'text'

1 Comment

done, but still not getting the string form the php file (server side)
0

You need to debug code where is actually fault whether you ajax call is actually initialize.

i: check whether value properly fetch in "area_id" js variable

alert(area_id);

ii: if it ok check whether data proper returned from server scriptinog

alert(data); or alert(data.message);

iii: for testing whether you receive data properly, just send out test script.

echo "sample text";
exit;

or try to send data in json format

die('{"message" : "sample text"}');

If all three steps working, then there should be fault in data access script.

If you are not getting output in ajax try using firebug to check what is actually happening in sending request and getting response.

2 Comments

Please check again my first post. I edited it and add new simple code (After editing:)
use firebug to track what is actually happening when ajax call initialize.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.