2013-05-23 24 views
26

list.php: Un semplice codice Ajax che voglio visualizzare solo i record della tabella Mysql:utilizzando jQuery Ajax per recuperare i dati da MySQL

<html> 
     <head> 
     <script src="jquery-1.9.1.min.js"> 
     </script> 

     <script> 
     $(document).ready(function(){ 
     var response = ''; 
     $.ajax({ type: "GET", 
       url: "Records.php", 
       async: false, 
       success : function(text) 
       { 
        response = text; 
       } 
     }); 

     alert(response); 
     }); 
     </script> 
     </head> 
     <body> 

     <div id="div1"><h2>Let jQuery AJAX Change This Text</h2> </div> 
     <button>Get Records</button> 

     </body> 
     </html> 

Records.php è il file per recuperare i record da MySQL.
Nel database ci sono solo due campi: "Nome", "Indirizzo".

 <?php 
       //database name = "simple_ajax" 
       //table name = "users" 

       $con = mysql_connect("localhost","root",""); 
       $dbs = mysql_select_db("simple_ajax",$con); 
       $result= mysql_query("select * from users"); 
       $array = mysql_fetch_row($result); 
       ?> 
        <tr> 
        <td>Name: </td> 
        <td>Address: </td> 
        </tr> 


       <?php 

       while ($row = mysql_fetch_array($result)) 
       { 

        echo "<tr>"; 
        echo "<td>$row[1]</td>"; 
        echo "<td>$row[2]</td>"; 
        echo "</tr>"; 
       } 

       ?> 

Questo codice non funziona.

+2

Cosa succede s quando vai direttamente a 'Records.php'? Ci sono messaggi di errore? Devi essere più preciso con cosa non funziona. –

+1

I nomi 1 e 2 delle colonne nella tabella "utenti"? Se lo sono, prova con echo "". $ Row ['1']. ""; In records.php –

+0

@PhilCross: Non c'è alcun errore dato che viene messo solo come risultato per il contenuto su Respons.php, come file che ho scritto come output. – Ravi

risposta

46

Per il recupero dei dati che utilizzano Ajax + jQuery, è necessario scrivere il codice seguente:

<html> 
<script type="text/javascript" src="jquery-1.3.2.js"> </script> 

<script type="text/javascript"> 

$(document).ready(function() { 

    $("#display").click(function() {     

     $.ajax({ //create an ajax request to display.php 
     type: "GET", 
     url: "display.php",    
     dataType: "html", //expect html to be returned     
     success: function(response){      
      $("#responsecontainer").html(response); 
      //alert(response); 
     } 

    }); 
}); 
}); 

</script> 

<body> 
<h3 align="center">Manage Student Details</h3> 
<table border="1" align="center"> 
    <tr> 
     <td> <input type="button" id="display" value="Display All Data" /> </td> 
    </tr> 
</table> 
<div id="responsecontainer" align="center"> 

</div> 
</body> 
</html> 

Per il collegamento mysqli, scrivere questo:

<?php 
$con=mysqli_connect("localhost","root",""); 
if (!$con) 
{ 
    die('Could not connect: ' . mysqli_error()); 
} 
?> 

Per visualizzando i dati dal database, è necessario scrivere questo:

<?php 
include("connection.php"); 
mysqli_select_db("samples",$con); 
$result=mysqli_query("select * from student",$con); 

echo "<table border='1' > 
<tr> 
<td align=center> <b>Roll No</b></td> 
<td align=center><b>Name</b></td> 
<td align=center><b>Address</b></td> 
<td align=center><b>Stream</b></td></td> 
<td align=center><b>Status</b></td>"; 

while($data = mysqli_fetch_row($result)) 
{ 
    echo "<tr>"; 
    echo "<td align=center>$data[0]</td>"; 
    echo "<td align=center>$data[1]</td>"; 
    echo "<td align=center>$data[2]</td>"; 
    echo "<td align=center>$data[3]</td>"; 
    echo "<td align=center>$data[4]</td>"; 
    echo "</tr>"; 
} 
echo "</table>"; 
?> 
+7

Solo una nota: non c'è ** nessun supporto ** per le funzioni 'mysql_ *', sono [** ufficialmente deprecate **] (https://wiki.php.net/rfc/mysql_deprecation), ** no più a lungo mantenuto ** e sarà [** rimosso **] (http://php.net/manual/en/function.mysql-connect.php # warning) in futuro. Dovresti aggiornare il tuo codice con [PDO] (http://php.net/pdo) o [MySQLi] (http://php.net/mysqli) per garantire la funzionalità del tuo progetto in futuro. –

+0

Ciao Ragazzi, ho provato sopra il codice, ma ha restituito tutti i dati del file di visualizzazione insieme a php. Per favore puoi farmi sapere cosa può essere un problema. Sono uno sviluppatore di .net che sta lavorando su php. – user3816325

+0

@ user3816325 controlla il tuo server. Credo che il tuo server non sia in esecuzione. – prakashchhetri

4

Non è possibile restituire un valore restituito ajax. La variabile globale memorizzata memorizza i valori restituiti dopo la restituzione.
Oppure cambia il tuo codice come questo.

AjaxGet = function (url) { 
    var result = $.ajax({ 
     type: "POST", 
     url: url, 
     param: '{}', 
     contentType: "application/json; charset=utf-8", 
     dataType: "json", 
     async: false, 
     success: function (data) { 
      // nothing needed here 
     } 
    }) .responseText ; 
    return result; 
} 

+0

senza Json che devo fare. – Ravi

+0

@RaviParmar cambia tipo di dati "html". Questo è ad esempio. –

1
$(document).ready(function(){ 
    var response = ''; 
    $.ajax({ type: "GET", 
     url: "Records.php", 
     async: false, 
     success : function(text) 
     { 
      response = text; 
     } 
    }); 

    alert(response); 
}); 

deve essere:

$(document).ready(function(){ 

    $.ajax({ type: "GET", 
     url: "Records.php", 
     async: false, 
     success : function(text) 
     { 
      alert(text); 
     } 
    }); 

}); 
2

Assicurati che il tuo $row[1] , $row[2] contiene valore corretto, noi assumiamo qui che 1 = Name , and 2 here is your Address field?

Supponendo di aver correttamente recuperato i record dal Records.php, Si può fare qualcosa di simile:

$(document).ready(function() 
{ 
    $('#getRecords').click(function() 
    { 
     var response = ''; 
     $.ajax({ type: 'POST', 
       url: "Records.php", 
       async: false, 
       success : function(text){ 
           $('#table1').html(text); 
          } 
      }); 
    }); 

} 

nel codice HTML

<table id="table1"> 
    //Let jQuery AJAX Change This Text 
</table> 
<button id='getRecords'>Get Records</button> 

Una piccola nota:

Prova learing PDO http://php.net/manual/en/class.pdo.php poiché mysql_* functions non sono incoraggiati ..

Problemi correlati