Pagina 1 di 2 1 2 ultimoultimo
Visualizzazione dei risultati da 1 a 10 su 17
  1. #1

    Leggere array bidimensionale da ajax

    Ciao a tutti!
    Ho una chiamata ajax con la quale popolo un div.
    Finche' il risultato della chiamata e' un vettore con un solo record non ho problemi.
    Ma se devo estrapolare tutti i record per popolare il div non riesco a recuperarli dal msg di risposta di ajax.




    codice:
      <script type="text/javascript">                  
    $(document).ready(function() {                     $("#advancedsearch").click(function(){                                    
    var name = $("#name").val();                 if(name == "")name="%";                 
    var city = $("#city").val();                 if(city == "")city="%";                 
    var uid= "<?php echo $user2["uid"]?>";                 
    $.ajax({                   url: "search/advancedSearch.php",                   
    type: "POST",                   
    data: "name=" + name + "&city=" + city + "&uid=" + uid,                 
    dataType: "html",                 
    success: function(msg) {                     
    alert(msg);                                   
    var arr_responseall=msg;                  
    alert(arr_responseall[0]);                  
    for(i=0;i<arr_responseall.lenght;i++){                                         
    var name=arr_response[0].name;                      
    var surname=arr_response[1].surname;                      
    var url=arr_response[2].id_foto;           
         }                 
    if(name != ""){                 
    $('#result').html(                 '<table><tr><td><div class="caption">[img]'+url+'[/img]<span><strong style="font-size:11px;">'+name+' '+surname+'[/b]</span></div></td></tr>\n\                  <tr><td><p align="center"></tr></table>');                                    }                                      },                
     error: function(){                     
    alert("Chiamata fallita!!!");                 }                  });                  
    return false;                 });             });        </script>

    La pagina advancedSearch.Php



    codice:
    <?php             
    include_once("../include/config.php"); if($_POST){              
    global $_CONFIG;             
    $result = mysql_query("             
    SELECT DISTINCT U.name as name,U.surname as surname,U.uid as uid,F.id_foto as id_foto             
    FROM ".$_CONFIG['table_utenti']." U, ".$_CONFIG['table_location']." L, ".$_CONFIG['table_body']." B, ".$_CONFIG['table_foto']." F             
    WHERE              (U.name LIKE '".$_POST['name']."' OR U.surname LIKE '".$_POST['name']."') AND             
    L.city LIKE '".$_POST['city']."' AND             
                U.uid = L.uid AND             
    U.uid = B.uid AND             
    U.uid = F.uid             
    ORDER BY F.id_foto DESC             ");             
    
    $data = array();             
    while($tmp = mysql_fetch_assoc($result)){                   
    array_push($data, $tmp);             };             
    echo $data;            	 }                         
    ?>

    Come faccio ad inviare dalla pagina php i valori name,surname e id_foto e recuperarli nella pagina javascript per poter popolare il mio div?

    Grazie mille!


    (Il codice che stampa il div sara' modificato oppurtunamente successivamente)

  2. #2
    Utilizza il formato json, che permette appunto lo scambio di dati sotto forma di array
    CODENCODE \ Branding \ Design \ Marketing
    www.codencode.it

  3. #3
    ok ma come devo fare?
    Ho fatto un po' di prove e non ci sono riuscito.

  4. #4
    Posta il codice delle prove
    CODENCODE \ Branding \ Design \ Marketing
    www.codencode.it

  5. #5
    Per prima cosa ho provato a modificare la classe php utilizzando la funzione json_encode come segue:



    (
    codice:
    <?php              
    include_once("../include/config.php"); if($_POST){               
    global $_CONFIG;             
     $result = mysql_query("              
    SELECT DISTINCT U.name as name,U.surname as surname,U.uid as uid,F.id_foto as id_foto              
    FROM ".$_CONFIG['table_utenti']." U, ".$_CONFIG['table_location']." L, ".$_CONFIG['table_body']." B, ".$_CONFIG['table_foto']." F              
    WHERE              (U.name LIKE '".$_POST['name']."' OR U.surname LIKE '".$_POST['name']."') AND             
     L.city LIKE '".$_POST['city']."' AND                          
    U.uid = L.uid AND              
    U.uid = B.uid AND              
    U.uid = F.uid              
    ORDER BY F.id_foto DESC             ");               
    $data = array();              
    while($tmp = mysql_fetch_assoc($result)){                    
    array_push($data, $tmp);             };              
    json_encode($data); 
    echo $data;            	 }                          ?>

    Poi ho provato quest'altra soluzione:


    codice:
    <?php              
    include_once("../include/config.php"); 
    if($_POST){              
     global $_CONFIG;              
    $result = mysql_query("              
    SELECT DISTINCT U.name as name,U.surname as surname,U.uid as uid,F.id_foto as id_foto              
    FROM ".$_CONFIG['table_utenti']." U, ".$_CONFIG['table_location']." L, ".$_CONFIG['table_body']." B, ".$_CONFIG['table_foto']." F              
    WHERE              (U.name LIKE '".$_POST['name']."' OR U.surname LIKE '".$_POST['name']."') AND              
    L.city LIKE '".$_POST['city']."' AND                          
    U.uid = L.uid AND              
    U.uid = B.uid AND              
    U.uid = F.uid              
    ORDER BY F.id_foto DESC             ");             
      $rows = array(); 
    while ($row = mysqli_fetch_assoc($result)) {     
    $rows[] = array(         'name' => $row['name'], 'surname' => $row['surname'], 'id_foto' => $row['id_foto']     ); } 
    echo json_encode($rows);        	 }                         
     ?>

  6. #6
    E lato js? hai lasciato
    codice:
    dataType: "html"
    Devi utilizzare
    codice:
    dataType: "json"
    CODENCODE \ Branding \ Design \ Marketing
    www.codencode.it

  7. #7
    Ecco questo e' un primo errore che facevo.
    Ma le due soluzioni da me proposte sono corrette o errate?

  8. #8
    Non mi sono soffermato più di tanto, in quanto c'era l'altro errore, ma in genere quando si ha un array e lo si da in pasto alla funzione json_encode() funziona correttamente.
    CODENCODE \ Branding \ Design \ Marketing
    www.codencode.it

  9. #9
    mettendo json mi dice chiamata fallita

  10. #10
    alert(msg) cosa ti restituisce? dovrebbe essere un Object.
    CODENCODE \ Branding \ Design \ Marketing
    www.codencode.it

Permessi di invio

  • Non puoi inserire discussioni
  • Non puoi inserire repliche
  • Non puoi inserire allegati
  • Non puoi modificare i tuoi messaggi
  •  
Powered by vBulletin® Version 4.2.1
Copyright © 2025 vBulletin Solutions, Inc. All rights reserved.