If else in Jquery
<p>i am a noob to jquery and i want to know how to make use of if else for the following:</p>

<p>on the server side there is a  <strong>if</strong> for number of rows is equal to 0 and <strong>else</strong> some JSON part.</p>

<pre><code>$age= mysql_query("SELECT title FROM parent WHERE id ='$name'");
$age_num_rows = mysql_num_rows($age);   
  if ($age_num_rows==0) 
  {
   echo "true";  
  }
  else
  {
$sql ="SELECT * FROM parentid WHERE id = '$name'"; //$name is value from html      
$result = mysql_query($sql);
while($row = mysql_fetch_array($result))
     {
$abc_output = array('title' =&gt; $row['title'],'age' =&gt; $row['age']);
     }
echo json_encode($abc_output);
  }
</code></pre>

<p>Now coming to Jquery part :</p>

<p>If the above PHP code go to <strong>if</strong> part then i want to display an alert box or if it goes to <strong>else</strong> part it needs to insert some values into the forms. 
Here is something i tried but it did not work.</p>

<pre><code>   $(document).ready(function(){  
      $("#button1").click(function(){
        $.getJSON('script_1.php',function(data){
            if (data=='true') {
                 alert ('hello')
            }
            else {
                $.post('script_1.php', 
                    { id: $('input[name="id"]', '#myForm').val() }, 
                    function(json) { 
                        $("input[name='title']").val(json.title);
                        $("input[name='age']").val(json.age); 
                    }, 
                    "json");  
            }
      });
  });  
</code></pre>

<p><strong>Edited</strong>:</p>

<pre><code>  $(document).ready(function(){  
$("#button1").click(function(){
    $.post(
        'script.php',
        { id: $('input[name="id"]', '#myForm').val() },
        function(json) { 
            var data = JSON.parse(json);
            if (data.length === 0){
             alert('no data');   
            }
            else{
            $("input[name='title']").val(json.title);
                    $("input[name='age']").val(json.age); 

            }},
        "json"
    );
});
});
</code></pre>

<p>PHP side</p>

<pre><code>$name = mysql_real_escape_string($_POST['id']); 
$sql ="SELECT * FROM parentid WHERE id = '$name'";       
$result = mysql_query($sql);
$row = mysql_fetch_array($result);
if ($row) {
$row=  array('title' =&gt; $row['title'],'age' =&gt; $row['age']);
echo json_encode($row);
} else {
echo json_encode(array());
}
</code></pre>
