Home > Backend Development > PHP Tutorial > How to use php combined with ajax to send red envelopes on mobile phone

How to use php combined with ajax to send red envelopes on mobile phone

墨辰丷
Release: 2023-03-28 20:24:01
Original
1198 people have browsed it

Red envelope sending function, when we enter the number and total amount of red envelopes, PHP will randomly allocate each amount based on these two values ​​to ensure that everyone can receive a red envelope. The amount of each red envelope varies, which is the requirement The amount of red envelopes should be different, and the total amount of all red envelopes should be equal to the total amount.

PHP Ajax has many functions that use it. Today, the editor has implemented a red envelope grabbing function using PHP Ajax. Let's take a look at an example of a PHP Ajax mobile phone program for sending red envelopes, as shown below.

PHP basic process of sending red envelopes: After entering the number and total amount of red envelopes, PHP will randomly allocate each amount based on these two values ​​to ensure that everyone can receive a red envelope, and each red envelope The amount varies. That is to say, the amount of red envelopes received by each person must be different, and the total amount of all red envelopes is equal to the total amount.

Principle of how to send red envelopes in php:

Set the total amount to 10 yuan, and N people will receive it randomly:
N=1 The first
Then the amount of the red envelope = red envelopes=10-the amount of the first red envelope;
N=3 The third
red envelope1=a random number between 0.01 and 9.99
red envelope2=0.01 to (10-red envelope1- 0.01) a random number
红包3=10-红包1-红包2
……

So we get a rule, when allocating the current red envelope amount, reserve the remaining red and white first The minimum amount required, and then take a random number between 0.01 and the total amount - the reserved amount. The random number obtained is the amount of the current red envelope distribution.

In actual application, the program first allocates the amount of red envelopes. That is, when sending red envelopes, the number of red envelopes and the amount of each red envelope are allocated. Then when the user comes to grab the red envelope, we randomly return a red envelope to the user. Red envelope is enough.

jquery code:


$(function() { 
 $("button").click(function() { 
 $.ajax({ 
 type: 'POST', 
 url: 'bao.php', 
 dataType: 'json', 
 beforeSend: function() { 
 $("#result").html('正在分配红包'); 
 }, 
 success: function(json) { 
 if (json.msg == 1) { 
  var str = ''; 
  var res = json.res; 
  $.each(res, 
  function(index, array) { 
  str = &#39;<p>第<span>&#39; array[&#39;i&#39;] &#39;</span>个红包,
  金额<span>&#39; array[&#39;money&#39;] &#39;</span>元,余额<span>&#39; 
  array[&#39;total&#39;] &#39;元</span></p>&#39;; 
  }); 
  $("#result").html(str); 
 } else { 
  $("#result").html(&#39;数据出错!&#39;); 
 } 
 } 
 }); 
 }); 
});
Copy after login

PHP code: bao.php



$total=20;//红包总金额 
$num=10;// 分成10个红包,支持10人随机领取 
$min=0.01;//每个人最少能收到0.01元 
 
for ($i=1;$i<$num;$i ) 
{ 
 $safe_total=($total-($num-$i)*$min)/($num-$i);//随机安全上限 
 $money=mt_rand($min*100,$safe_total*100)/100; 
 $total=$total-$money; 
 
 echo &#39;第&#39;.$i.&#39;个红包:&#39;.$money.&#39; 元,余额:&#39;.$total.&#39; 元 &#39;; 
} 
echo &#39;第&#39;.$num.&#39;个红包:&#39;.$total.&#39; 元,余额:0 元&#39;;
Copy after login

The effect is as shown in the picture:


Summary: The above is this The entire content of this article is hoped to be helpful to everyone's study.

Related recommendations:

php

Method to intercept mixed Chinese and English strings
ThinkPHP5 verification The specific use of the processor


JSON In PHP, the method of deserializing a Json string into an object/array


The above is the detailed content of How to use php combined with ajax to send red envelopes on mobile phone. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template