Sunday 15 July 2012

javascript - AJAX sending and receiving different data type -



javascript - AJAX sending and receiving different data type -

i made basic ajax function calling php file. response html inserted in "content box" of main page

function loadajax_alert_display_one(){ id_alert = <?php echo $id_alerte; ?>; $('.preloader').show(); $.ajax({ mimetype: 'text/html; charset=utf-8', // ! need set mimetype when run local file url: 'ajax/alert_display_one.php', data: "id_alerte="+id_alert, type: 'get', success: function(data) { $('#ajax-content').html(data); $('.preloader').hide(); }, error: function (jqxhr, textstatus, errorthrown) { alert(errorthrown); }, datatype: "html", async: false }); }

that's working fine. called file 'alerts_create.php' runinng php info database , displays using while loop

while ($stmt->fetch()) { echo "<tr>"; echo "<td><a href='#' onclick='loadajax_alert_display_one();'>" . $nom_alerte . "</a></td>"; echo "<td>" . $country . "</td>"; echo "</tr>"; }

my issue cannot pass link create in while loop. $nom_alerte takes value of lastly iteration of loop. ajax take link value; ideas how can that?

to clrify title: issue send php variable called file ('alerts_create.php') , retrieve html result.

solution: had pass php variable ajax function parameter:

function loadajax_alert_display_one(id_alerte){ $('.preloader').show(); $.ajax({ mimetype: 'text/html; charset=utf-8', // ! need set mimetype when run local file url: 'ajax/alert_display_one.php', data: "id_alerte="+id_alerte, type: 'get', success: function(data) { $('#ajax-content').html(data); $('.preloader').hide(); }, error: function (jqxhr, textstatus, errorthrown) { alert(errorthrown); }, datatype: "html", async: false }); }

use

<script type="text/javascript"> var info = 'abc=0'; $.post( 'yoururl.php', info ).success(function(resp){ var json = $.parsejson(resp); console.log(json); });

and in php file utilize this

while( $stmt->fetch() ) { $data[] = array( 'link' => 'your link' ); } echo json_encode($data);

javascript php ajax

No comments:

Post a Comment