JIYIK CN >

Current Location:Home > Learning > PROGRAM > PHP >

PHP with Ajax

Author:JIYIK Last Updated:2025/04/13 Views:

We will use PHP and ajax by printing a simple sum of two numbers 2and . Also, print a php array in JSON.3object

We will also use PHP with ajax by getting the HTML formatted output from the number division in PHP.


Printing simple addition between two numbers using PHP and Ajax

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>

<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.4/jquery.min.js"></script>
<script type="text/javascript">
     $(function(){
       $.get("summation.php", function(data){
          console.log(data);
       });
    });
</script>
</body>
</html>
<?php

  $number1 = 2;
  $number2 = 3;
  $summation = $number1 + $number2;

  echo "Summation, $summation";
?>

Output:

Summation, 5

Printing a PHP array as a JSON object using PHP with Ajax

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>

<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.4/jquery.min.js"></script>
<script type="text/javascript">
     $(function(){
       $.get("json.php", function(data){
          console.log(data);
       });
    });
</script>
</body>
</html>
<?php

  $profile = array('name' => "Kevin Amayi", "age" => 23, "Profession" => "Programmer");
  echo json_encode($profile);
?>

Output:

{"name":"Kevin Amayi","age":23,"Profession":"Programmer"}

Printing numbers divided by 1-20 using PHP and Ajax

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Ajax and PHP Basics</title>
<script src="https://code.jquery.com/jquery-3.5.1.min.js"></script>
<script>
$(document).ready(function(){
    $("button").click(function(){
        var number = $("#number").val();
        $.get("division.php", {number: number} , function(data){
            $("#division").html(data);
        });
    });
});
</script>
</head>
<body>
    <label>Enter a Number: <input type="number" id="number"></label>
    <button type="button">Show Division Table</button>
    <div id="division"></div>
</body>
</html>
<?php
$number =$_GET["number"];
//check if the data entered is a number => if so display a table with the number's division from 1-20
if(is_numeric($number) ){
    echo "<table>";
    for($j=1; $j<21; $j++){
        echo "<tr>";
            echo "<td>$number / $j</td>";
            echo "<td>=</td>";
            echo "<td>". $number / $j . "</td>";
        echo "</tr>";
    }
    echo "</table>";
}
?>

Output:

2 / 1	=	2
2 / 2	=	1
2 / 3	=	0.66666666666667
2 / 4	=	0.5
2 / 5	=	0.4
2 / 6	=	0.33333333333333
2 / 7	=	0.28571428571429
2 / 8	=	0.25
2 / 9	=	0.22222222222222
2 / 10	=	0.2
2 / 11	=	0.18181818181818
2 / 12	=	0.16666666666667
2 / 13	=	0.15384615384615
2 / 14	=	0.14285714285714
2 / 15	=	0.13333333333333
2 / 16	=	0.125
2 / 17	=	0.11764705882353
2 / 18	=	0.11111111111111
2 / 19	=	0.10526315789474
2 / 20	=	0.1

For reprinting, please send an email to 1244347461@qq.com for approval. After obtaining the author's consent, kindly include the source as a link.

Article URL:

Related Articles

Check if a Post exists in PHP

Publish Date:2025/04/13 Views:170 Category:PHP

PHP $_POST is a super global variable that can contain key-value pairs of HTML form data submitted through the post method. We will learn different ways to check $_POST if a and contains some data in this article. These methods will use iss

Store Div Id in PHP variable and pass it to JavaScript

Publish Date:2025/04/13 Views:51 Category:PHP

This article shows you how to div id store a in a PHP variable and pass it to JavaScript code. We will answer the following questions. What is div id ? How to div id store in a PHP variable? How to pass variables to JavaScript code? Let’s

Returns the article tag with ID from the action page

Publish Date:2025/04/13 Views:80 Category:PHP

Let's say you're in a login form and you enter the wrong information; in this case, you probably want to go back to the login page. PHP has a built-in function header() to redirect a page to a specific page. But what if the login page is at

Switching PHP versions on Ubuntu

Publish Date:2025/04/13 Views:78 Category:PHP

Different tasks may require running multiple versions of PHP. You may need to switch PHP versions by running two sites on the same server or testing older versions of code using outdated methods. We can switch PHP versions on Ubuntu using t

Resizing images in PHP

Publish Date:2025/04/13 Views:155 Category:PHP

In this tutorial article, we will discuss about resizing images in PHP. Load the image before resizing Before we can resize an image, we must first load it as an image resource in our script. This is file_get_contents() different from using

PHP upload image

Publish Date:2025/04/13 Views:61 Category:PHP

We can upload images in PHP using simple file upload operation, but first, php.ini file upload should be enabled from Files. This tutorial demonstrates how to upload images in PHP. php.ini Enable file upload from file in PHP to upload image

Creating a signature from Hash_hmac() and Sha256 in PHP

Publish Date:2025/04/13 Views:107 Category:PHP

PHP has one of the best encryption functions for data security. Hash_hmac() The encrypt function is one of the most famous encryptors. We'll show you how to use hash_hmac and sha256 encryptors to create 安全签名 one that you can store i

Updating PHP 7.x to 7.4 on CentOS

Publish Date:2025/04/13 Views:131 Category:PHP

This article shows the steps to update the PHP version from 7.x version to 7.4 in CentOS. How to Update PHP from 7.X to 7.4 in CentOS Update operating system packages. yum update -y Check your PHP version in CentOS. php -v Prints a list of

Displays PHP configuration information on localhost

Publish Date:2025/04/13 Views:107 Category:PHP

phpinfo() is a built-in function in PHP which outputs all the information of PHP configuration on the local host. We have to phpinfo() create a PHP file with a simple function call. Sometimes, the file may not work properly and output a 404

Scan to Read All Tech Tutorials

Social Media
  • https://www.github.com/onmpw
  • qq:1244347461

Recommended

Tags

Scan the Code
Easier Access Tutorial