2013-06-13 27 views
0

我正在開發使用jquery mobile的android應用程序。我在應用程序中有多個頁面。除index.html之外的所有頁面都是由數據庫驅動的。檢查值,然後指向不同的頁面

我想這個ID的特定鏈接沒有任何細節在分貝然後它不應該被定向到轉發頁面。我已經使javascript功能來檢查,但它不起作用。

function check_category(b_id){ 

$.ajax({ 
     url:'http://localhost/ajax_practice/php/get_categories.php?b_id='+b_id, 
     dataType: 'jsonp', 
     jsonp: 'jsoncallback', 
     timeout: 5000, 
     success:function(data,status){ 

      if(data.success==0){ 
       return false; 
      }else{ 
       return true; 
      } 
     } 
    });    
} 

PHP代碼:

<?php 
    header('Content-type: application/json'); 

    include 'connect.php'; 

    $b_id=$_GET['b_id']; 


    $sql_select=mysql_query("SELECT * FROM businesses JOIN business_category ON business_category.b_id = businesses.id WHERE b_id=$b_id") or die(mysql_error()); 

    $records = array(); 

    if(mysql_num_rows($sql_select)>0){ 

     while($row=mysql_fetch_array($sql_select)){ 
      $records[] = $row; 
     } 
     //print_r($records); 
     echo $_GET['jsoncallback'] . '(' . json_encode($records) . ');'; 
    }else{ 

     $records= array(
      'success'=>0, 
     ); 
     echo $_GET['jsoncallback'] . '(' . json_encode($records) . ');'; 
    } 

?> 

基本上我想要的是如果類別表與該企業出口的數據,那麼它必須返回true,否則爲false;這樣href就不會被設置爲true或false。

+0

*旁註:*停止使用廢棄的'mysql_ *'功能。改用MySQLi或PDO。 – Raptor

回答

0

修改你的腳本是這樣的:

function check_category(b_id){ 

$.ajax({ 
    url:'http://localhost/ajax_practice/php/get_categories.php?b_id='+b_id, 
    dataType: 'json', 
    timeout: 5000, 
    success:function(data,status){ 

     if(data.success==0){ 
      return false; 
     }else{ 
      return true; 
     } 
    } 
    });    
} 

而過修改PHP:

if(mysql_num_rows($sql_select)>0){ 
     $records['success']=1; 
     while($row=mysql_fetch_array($sql_select)){ 
      $records['data'][] = $row; 
     } 

    }else{ 
     $records['success']=0; 
    } 
    echo json_encode($records); 
+0

是否有任何其他方式,我可以在jQuery的移動應用程序中使用,如果頁面必須加載,如果數據庫中存在與該鏈接有關的數據,否則不加載頁面 – user2466635

+0

有很多方法來實現這一點。這取決於您的應用程序結構。也許你可以回顯一個只包含有數據的類別的javascript數組,你可以跳過對那些id不在該數組中的類別的Ajax調用。 – HarryFink

相關問題