dongxing9219 2011-08-18 14:59
浏览 43
已采纳

如果在Jquery中

i am a noob to jquery and i want to know how to make use of if else for the following:

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

$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' => $row['title'],'age' => $row['age']);
     }
echo json_encode($abc_output);
  }

Now coming to Jquery part :

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

   $(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");  
            }
      });
  });  

Edited:

  $(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"
    );
});
});

PHP side

$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' => $row['title'],'age' => $row['age']);
echo json_encode($row);
} else {
echo json_encode(array());
}
  • 写回答

2条回答 默认 最新

  • dr5779 2011-08-18 15:19
    关注

    There seem to be a few problems:

    1. You are treating the data returned from getJSON as if it is plain text
    2. The php that you are calling from javascript does not always return json
    3. You are doing 2 ajax requests; getJSON and post where you only need one: The first call to getJSON without any data will never reach the else condition

    By the way, where does $name come from in your php script? For your second ajax call to work, it needs to be something like mysql_real_escape_string($_POST['id']) or (int) $_POST['id'] if it is an integer.

    Edit: I think it would be easiest to get rid of the .post and just use the first ajax call. So you will need to change:

    $.getJSON('script_1.php',function(data){
    

    to something like:

    $.getJSON('script_1.php?id=' + $('input[name="id"]').val(), function(data) {
    

    and in your php you need to use something like:

    $name = mysql_real_escape_string($_GET['id']);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥30 win from 窗口最大最小化,控件放大缩小,闪烁问题
  • ¥20 易康econgnition精度验证
  • ¥15 msix packaging tool打包问题
  • ¥28 微信小程序开发页面布局没问题,真机调试的时候页面布局就乱了
  • ¥15 python的qt5界面
  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致