Answers for "ajax return data"

2

ajax response returns entire page

Use condition to verify if $_POST empty in you main page or index.php to not send the whole page in response ajax
and verif your AJAX URL
 $.ajax({
             type:"POST",
             url:"http://mywebsite.com/php/send_email.php",
   .......
   
index.php
------------------------
<?php 
if(empty($_POST)){ ?>

//
<html>
the header pages
the body
the include pages
the scripts pages
the footer
<?php } ?>
Posted by: Guest on October-30-2020
2

ajax get html response

//your jquery code will be like this:
$.ajax({
      type: 'POST',
      url: $url,
      data: new FormData(this),
      dataType: 'json',
      contentType: false,
      processData:false,//this is a must
      success: function(response){ 
      		$('your_selector').html(response);
      }
});

//php code will be like this
echo '<div>some html code</div>';
die();
Posted by: Guest on June-11-2021
0

simple return data jquery

$(document).ready(function(){
    $.ajax({
        url: 'ajaxfile.php',
        type: 'get',
        dataType: 'JSON',
        success: function(response){
            var len = response.length;
            for(var i=0; i<len; i++){
                var id = response[i].id;
                var username = response[i].username;
                var name = response[i].name;
                var email = response[i].email;

                var tr_str = "<tr>" +
                    "<td align='center'>" + (i+1) + "</td>" +
                    "<td align='center'>" + username + "</td>" +
                    "<td align='center'>" + name + "</td>" +
                    "<td align='center'>" + email + "</td>" +
                    "</tr>";

                $("#userTable tbody").append(tr_str);
            }

        }
    });
});
Posted by: Guest on June-18-2020
0

simple return data jquery

<?php

include "config.php";

$return_arr = array();

$query = "SELECT * FROM users ORDER BY NAME";

$result = mysqli_query($con,$query);

while($row = mysqli_fetch_array($result)){
    $id = $row['id'];
    $username = $row['username'];
    $name = $row['name'];
    $email = $row['email'];

    $return_arr[] = array("id" => $id,
                    "username" => $username,
                    "name" => $name,
                    "email" => $email);
}

// Encoding array in JSON format
echo json_encode($return_arr);
Posted by: Guest on June-18-2020

Browse Popular Code Answers by Language