Hướng dẫn addition of two numbers using ajax in php - phép cộng hai số bằng cách sử dụng ajax trong php

Viết một chương trình PHP đơn giản để thực hiện AJAX để thêm hai số

Thêm.php

& nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; Nhập số đầu tiên: & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; Nhập số thứ hai: & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp;

  
  



  
 
  
    
    
    
    
    
    
    
    
      
    
Enter first number :
Enter second number :



ajax.js

hàm thêm [obj] {& nbsp; & nbsp; var xmlHttprequestObject = false; & nbsp; & nbsp; if [window.xmlhttprequest] & nbsp; & nbsp; {& nbsp; & nbsp; & nbsp; & nbsp; XMLHTTPrequestObject = new xmlHttPrequest []; & nbsp; & nbsp; & nbsp; } & nbsp; & nbsp; khác if [window.activeXObject] & nbsp; & nbsp; & nbsp; {& nbsp; & nbsp; & nbsp; & nbsp; & nbsp; XmlHttprequestObject = new ActiveXObject ["microsoft.xmlhttp"]; & nbsp; & nbsp; & nbsp; } & nbsp; & nbsp; var str1 = "num1 ="+document.getEuityById ["num1"]. value; & nbsp; & nbsp; var str2 = "& num2 ="+document.getEuityById ["num2"]. value; & nbsp; & nbsp; XmlHttprequestObject.onReadyStateChange = show; & nbsp; XmlhttprequestObject.open ['post', 'value.php', true]; & nbsp; XmlhttprequestObject.setRequestHeader ["loại nội dung", "application/x-www-form-urlencoded"]; & nbsp; XmlhttprequestObject.send [str1+str2]; & nbsp; Hiển thị chức năng [] & nbsp; {& nbsp; & nbsp; & nbsp; if [xmlhttprequestObject.ReadyState == 4] & nbsp; & nbsp; & nbsp; {& nbsp; & nbsp; & nbsp; & nbsp; if [xmlhttprequestObject.status == 200] & nbsp; & nbsp; & nbsp; & nbsp; {& nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; result = xmlhttprequestObject.responsetext; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; document.getEuityById ['txt']. Internhtml = result; & nbsp; & nbsp; & nbsp; & nbsp; } & nbsp; & nbsp; } & nbsp; }}
{
   var XMLHttpRequestObject=false;
   if[window.XMLHttpRequest]
   {
     XMLHttpRequestObject=new XMLHttpRequest[];
    }
   else if[window.ActiveXObject]
    {
      XMLHttpRequestObject=new ActiveXObject["Microsoft.XMLHTTP"];
    }
   var str1="num1="+document.getElementById["num1"].value;
   var str2="&num2="+document.getElementById["num2"].value;
   XMLHttpRequestObject.onreadystatechange=show;
  XMLHttpRequestObject.open['POST','value.php',true];
  XMLHttpRequestObject.setRequestHeader["Content-type","application/x-www-form-urlencoded"];
  XMLHttpRequestObject.send[str1+str2];
  function show[]
  {
    if[XMLHttpRequestObject.readyState==4]
    {
     if[XMLHttpRequestObject.status==200]
     {
       result=XMLHttpRequestObject.responseText;
       document.getElementById['txt'].innerHTML=result;
     }
   }
  }
}

value.php

Bài viết phổ biến từ blog này

Đây là mã HTML của tôi:

   







Đây là mã jQuery của tôi:

       $["#sum"].click[function[] {
                        var num1 = $["#num1"].val[];
                        var num2 = $["#num2"].val[];


                        var dataString = "num1="+ num1 + "&num2=" + num2; 

                        $.ajax[{
                        type: "POST",
                        url: "apotelesma.php",
                        data: dataString,
                        success: function[] {
                           alert[dataString]

                                return false;
                        }
                }];
                return false;
        }];

Và đây là tập lệnh apotelesma.php:


Tôi không thể tìm ra làm thế nào để có được kết quả. Tôi không biết có đúng không để bao gồm tập lệnh PHP để hiển thị kết quả.

Có ba phương pháp để thêm hai số:

  • Thêm vào mã đơn giản trong PHP
  • Thêm vào hình thức trong PHP
  • Thêm mà không sử dụng toán tử số học [+].

Thêm vào mã đơn giản

Bổ sung hai số 15 và 30 được hiển thị ở đây.

Example:

Output:

Thêm vào hình thức

Hai số có thể được thêm vào bằng cách chuyển giá trị đầu vào trong biểu mẫu.

Example:

Output:

Thêm vào mã đơn giản

Bổ sung hai số 15 và 30 được hiển thị ở đây.

Output:

Thêm vào hình thức

Nhận xét

  • Gửi phản hồi của bạn đến [Email & NBSP; được bảo vệ]

Giúp đỡ người khác, xin vui lòng chia sẻ

Làm thế nào tôi có thể nhận được tổng của hai số trong PHP?

Có ba phương pháp để thêm hai số: thêm vào mã đơn giản trong PHP.Thêm vào hình thức trong PHP.Thêm mà không sử dụng toán tử số học [+] ...
Giúp đỡ người khác, xin vui lòng chia sẻ
$x=15;.
$y=30;.
$z=$x+$y;.
echo "tổng:", $ z ;.

Làm thế nào có thể thêm hai số trong TextBox trong PHP?

Chương trình để thêm hai số vào PHP với Mẫu PHP if [iset [$ _ post ['gửi']]] {$ sum = 0;$ một = $ _ bài ['một'];$ hai = $ _ bài ['hai'];// Echo $ một;$ sum = $ một+$ hai;echo $ sum;lối ra;}?>

AJAX có thể được sử dụng với PHP không?

Bắt đầu sử dụng AJAX ngay hôm nay trong hướng dẫn PHP của chúng tôi, chúng tôi sẽ chứng minh cách AJAX có thể cập nhật các phần của trang web, mà không cần tải lại toàn bộ trang.Tập lệnh máy chủ sẽ được viết bằng PHP.Nếu bạn muốn tìm hiểu thêm về Ajax, hãy truy cập hướng dẫn Ajax của chúng tôi.The server script will be written in PHP. If you want to learn more about AJAX, visit our AJAX tutorial.

Làm cách nào để thêm hai số trong HTML?

Sau đó, bạn có thể thêm các số vào hai hộp văn bản đầu tiên và lưu trữ chúng trong một biến như "kết quả", như sau: var result = number [box1.value] + số [box2.value];Box3.var result = Number[box1. value] + Number[box2. value]; box3.

Bài Viết Liên Quan

Chủ Đề