Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use PHP+Ajax to realize the function of sending red packets on mobile phone

2025-03-11 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article introduces the relevant knowledge of "how to use PHP+Ajax to send red packets on mobile phones". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

Basic process: after entering the number of red packets and the total amount, PHP will randomly allocate each amount according to these two values to ensure that everyone can receive a red packet, and the amount of each red packet varies, and the total amount of all red packets is equal to the total amount.

The principle of implementation:

Set the total amount to 10 yuan, and N individuals will receive it at random:

The first one in Niss1

Then the amount of red packet = X yuan

Ninten2, second one.

In order to ensure that the second red packet can be sent normally, the amount of the first red packet is a random number between 0.01 and 9.99

Second red packet = 10-first red packet amount

The third Nissan 3

A random number between red packet 1, 0.01 and 9.99.

A random number from red packet 2 to (10-red packet 1-0.01)

Red packet 3: 10-Red packet 1-Red packet 2

……

So we get a rule that when allocating the current red packet amount, we first reserve the minimum amount needed for the remaining red and white, and then take a random number from 0.01 to the total amount-reserved amount, and the random number obtained is the current red packet allocation amount.

In practical application, the program first allocates the amount of red packets, that is, when sending red packets, the number of red packets and the amount of each red packet are allocated, then when the user comes to grab the red packet, we will randomly return a red packet to the user.

Js Code:

$(function () {$("button") .click (function () {$.ajax ({type: 'POST', url:' bao.php', dataType: 'json', beforeSend: function () {$("# result") .click (' assigning red packets') }, success: function (json) {if (json.msg = = 1) {var str ='; var res = json.res; $.each (res, function (index, array) {str + ='

The first'+ array ['i'] + 'red packet, the amount' + array ['money'] +' yuan, the balance'+ array ['total'] +' yuan

';}); $("# result") .html (str);} else {$("# result") .html (' data error!') ;})

Bao.php source code:

The total amount of $total=20;// red packets $num=10;// is divided into 10 red packets, which supports 10 people to receive $min=0.01;// randomly. Each person can receive at least 0.01RMB for.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report