<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>运达&#039;s  blog &#187; Ajax</title>
	<atom:link href="https://www.yunda51.com/?feed=rss2&#038;tag=ajax" rel="self" type="application/rss+xml" />
	<link>https://www.yunda51.com</link>
	<description>运达的博客</description>
	<lastBuildDate>Wed, 12 Nov 2025 07:58:26 +0000</lastBuildDate>
	<language>zh-CN</language>
		<sy:updatePeriod>hourly</sy:updatePeriod>
		<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=4.0.19</generator>
	<item>
		<title>PHP+Ajax手机发红包</title>
		<link>https://www.yunda51.com/?p=1673</link>
		<comments>https://www.yunda51.com/?p=1673#comments</comments>
		<pubDate>Tue, 26 Jan 2016 03:50:54 +0000</pubDate>
		<dc:creator><![CDATA[运达]]></dc:creator>
				<category><![CDATA[php技术]]></category>
		<category><![CDATA[Ajax]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[红包]]></category>

		<guid isPermaLink="false">http://www.yunda51.com/?p=1673</guid>
		<description><![CDATA[PHP发红包基本流程：当输入完红包数量和总金额后，PHP会根据这两个值进行随机分配每个金额，保证每个人都能领取<a href="https://www.yunda51.com/?p=1673" class="read-more">Continue Reading</a>]]></description>
				<content:encoded><![CDATA[<p>PHP发红包基本流程：当输入完红包数量和总金额后，PHP会根据这两个值进行随机分配每个金额，保证每个人都能领取到一个红包，且每个红包金额不等。也就是每个人领取的红包金额要不同，并且所有红包金额总额等于总金额。<strong>如图：</strong></p>
<p><a href="http://www.yunda51.com/wp-content/uploads/2016/01/big.jpg.png"><img class="size-full wp-image-1677 aligncenter" src="http://www.yunda51.com/wp-content/uploads/2016/01/big.jpg.png" alt="big.jpg" width="256" height="210" /></a></p>
<p>php发红包实现原理：</p>
<p>设定总金额为10元，有N个人随机领取：</p>
<p>N=1 第一个</p>
<p>则红包金额=X元；</p>
<p>N=2 第二个</p>
<p>为保证第二个红包可以正常发出，第一个红包金额=0.01至9.99之间的某个随机数</p>
<p>第二个红包=10-第一个红包金额；</p>
<p>N=3 第三个</p>
<p>红包1=0.01至9.99之间的某个随机数</p>
<p>红包2=0.01至(10-红包1-0.01)的某个随机数</p>
<p>红包3=10-红包1-红包2</p>
<p>……</p>
<p>于是我们得到一个规律，在分配当前红包金额时，先预留剩余红白所需最少金额，然后在0.01至总金额-预留金额间取随机数，得到的随机数就是当前红包分配的金额。</p>
<p>实际应用中，程序先将红包金额分配好，即发红包时，红包个数以及每个红包的金额都分配好了，那么用户来抢红包时，我们随机给用户返回一个红包即可。<br />
<strong>jQuery代码：</strong></p>
<pre class="wp-code-highlight prettyprint">
$(function() { 
    $(&quot;button&quot;).click(function() { 
        $.ajax({ 
            type: &#039;POST&#039;, 
            url: &#039;bao.php&#039;, 
            dataType: &#039;json&#039;, 
            beforeSend: function() { 
                $(&quot;#result&quot;).html(&#039;正在分配红包&#039;); 
            }, 
            success: function(json) { 
                if (json.msg == 1) { 
                    var str = &#039;&#039;; 
                    var res = json.res; 
                    $.each(res, 
                    function(index, array) { 
                        str += &#039;&lt;p&gt;第&lt;span&gt;&#039; + array[&#039;i&#039;] + &#039;&lt;/span&gt;个红包，
                        金额&lt;span&gt;&#039; + array[&#039;money&#039;] + &#039;&lt;/span&gt;元，余额&lt;span&gt;&#039; + 
                        array[&#039;total&#039;] + &#039;元&lt;/span&gt;&lt;/p&gt;&#039;; 
                    }); 
                    $(&quot;#result&quot;).html(str); 
                } else { 
                    $(&quot;#result&quot;).html(&#039;数据出错！&#039;); 
                } 
            } 
        }); 
    }); 
});
</pre>
<p><strong>PHP代码：bao.php</strong></p>
<pre class="wp-code-highlight prettyprint">$total=20;//红包总金额    
$num=10;// 分成10个红包，支持10人随机领取    
$min=0.01;//每个人最少能收到0.01元    
  
for ($i=1;$i&amp;lt;$num;$i++)    
{    
    $safe_total=($total-($num-$i)*$min)/($num-$i);//随机安全上限    
    $money=mt_rand($min*100,$safe_total*100)/100;    
    $total=$total-$money;   
      
    echo &#039;第&#039;.$i.&#039;个红包：&#039;.$money.&#039; 元，余额：&#039;.$total.&#039; 元 &#039;;    
}    
echo &#039;第&#039;.$num.&#039;个红包：&#039;.$total.&#039; 元，余额：0 元&#039;;
</pre>
<p><strong>点击下载：</strong><a style="background: transparent none repeat scroll 0% 0%;" href="http://pan.baidu.com/s/1eQUrneq" target=""><img class="alignnone size-full wp-image-1075" title="btn_load" src="http://www.yunda51.com/wp-content/uploads/2013/11/btn_load3.png" alt="" width="86" height="28" /></a></p>
]]></content:encoded>
			<wfw:commentRss>https://www.yunda51.com/?feed=rss2&#038;p=1673</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>Thinkphp原创Ajax分页加搜索查询</title>
		<link>https://www.yunda51.com/?p=1667</link>
		<comments>https://www.yunda51.com/?p=1667#comments</comments>
		<pubDate>Tue, 26 Jan 2016 02:59:27 +0000</pubDate>
		<dc:creator><![CDATA[运达]]></dc:creator>
				<category><![CDATA[php技术]]></category>
		<category><![CDATA[Ajax]]></category>
		<category><![CDATA[php]]></category>

		<guid isPermaLink="false">http://www.yunda51.com/?p=1667</guid>
		<description><![CDATA[搜索#keyword和加载内容区域#ajax_lists &#60;input id=&#34;keyword<a href="https://www.yunda51.com/?p=1667" class="read-more">Continue Reading</a>]]></description>
				<content:encoded><![CDATA[<p>搜索#keyword和加载内容区域#ajax_lists</p>
<pre class="wp-code-highlight prettyprint">&lt;input id=&quot;keyword&quot; class=&quot;input&quot; type=&quot;text&quot; value=&quot;&quot; placeholder=&quot;请输入搜索关键词&quot; /&gt;
&lt;input class=&quot;btn&quot; type=&quot;button&quot; value=&quot;搜索&quot; /&gt; 
</pre>
<pre class="wp-code-highlight prettyprint">var url_ajax = &quot;__APP__/Box/orders&quot;; 
$(function() { 
    $(&quot;#ajax_lists&quot;).delegate(&quot;.pager a&quot;, &quot;click&quot;, function() { 
        var page = $(this).attr(&quot;data-page&quot;);//获取当前点击分页 
        getPage(page); 
    }) 
    getPage(1); //初始化分页 
 
}) 
function getPage(page) { 
    $(&quot;#ajax_lists&quot;).html(&quot;

</pre>
<div class="loading"><img src="__PUBLIC__/images/loading.gif" alt="loading" /></div>
<p>"); var keyword = $("#keyword").val(); $.get(url_ajax, {keyword: keyword, p: page}, function(data) { $('#ajax_lists').html(data); }) }</p>
<p>远程ajax加载订单列表</p>
<pre class="wp-code-highlight prettyprint">public function orders() { 
    $sql = &quot;1=1&quot;; 
 
    $keyword = trim(I(&#039;get.keyword&#039;)); 
    if (!empty($keyword)) { 
        $sql .= &quot; AND name like &#039;%&quot; . $keyword . &quot;%&#039;&quot;; 
    } 
    $count = M(&#039;js&#039;)-&amp;gt;where($sql)-&amp;gt;count();    //计算总数 
    $Page = new \Think\PageAjax($count, 10); 
//           
    $lists = M(&#039;js&#039;)-&amp;gt;where($sql)-&amp;gt;limit($Page-&amp;gt;firstRow . &#039;,&#039; . $Page-&amp;gt;listRows)-&amp;gt;order(&#039;id DESC&#039;)-&amp;gt;select(); 
 
    $this-&amp;gt;assign(&quot;page&quot;, $Page-&amp;gt;show()); 
    $this-&amp;gt;assign(&quot;lists&quot;, $lists); 
    $this-&amp;gt;assign(&quot;keyword&quot;, $keyword); 
    $this-&amp;gt;display(); 
}
</pre>
<p><strong>如图：</strong><br />
<a href="http://www.yunda51.com/wp-content/uploads/2016/01/20160126105520.png"><img class="alignnone size-full wp-image-1668" src="http://www.yunda51.com/wp-content/uploads/2016/01/20160126105520.png" alt="20160126105520" width="743" height="468" /></a></p>
<p><strong>源码下载：</strong><a style="background: transparent none repeat scroll 0% 0%;" href="http://pan.baidu.com/s/1hrtVAte" target=""><img class="alignnone size-full wp-image-1075" title="btn_load" src="http://www.yunda51.com/wp-content/uploads/2013/11/btn_load3.png" alt="" width="86" height="28" /></a></p>
]]></content:encoded>
			<wfw:commentRss>https://www.yunda51.com/?feed=rss2&#038;p=1667</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Ajax表单验证</title>
		<link>https://www.yunda51.com/?p=1295</link>
		<comments>https://www.yunda51.com/?p=1295#comments</comments>
		<pubDate>Thu, 08 May 2014 03:04:04 +0000</pubDate>
		<dc:creator><![CDATA[运达]]></dc:creator>
				<category><![CDATA[php技术]]></category>
		<category><![CDATA[Thinkphp]]></category>
		<category><![CDATA[Ajax]]></category>

		<guid isPermaLink="false">http://www.yunda51.com/?p=1295</guid>
		<description><![CDATA[最近没什么可以的了,只能随便凑点东西了哈~~ ajax表单验证,测试过的,直接代码了! &#60;div cla<a href="https://www.yunda51.com/?p=1295" class="read-more">Continue Reading</a>]]></description>
				<content:encoded><![CDATA[<p>最近没什么可以的了,只能随便凑点东西了哈~~ ajax表单验证,测试过的,直接代码了!</p>
<pre class="wp-code-highlight prettyprint">
   &lt;div class=&quot;bdbox&quot;&gt;
        &lt;ul&gt;  
	    &lt;li&gt;&lt;span class=&quot;bdl&quot;&gt;&lt;em&gt;*&lt;/em&gt;企业名称:&lt;/span&gt;&amp;nbsp;&amp;nbsp;&lt;span class=&quot;bdr&quot;&gt;&lt;input id=&quot;companynames&quot; name=&quot;companyname&quot; size=&quot;30&quot; class=&quot;txt&quot; /&gt;&lt;/span&gt;&lt;/li&gt;
            &lt;li&gt;&lt;span class=&quot;bdl&quot;&gt;&lt;em&gt;*&lt;/em&gt;企业网址:&lt;/span&gt;&amp;nbsp;&amp;nbsp;&lt;span class=&quot;bdr&quot;&gt;&lt;input id=&quot;companyurls&quot; name=&quot;companyurl&quot; size=&quot;30&quot; class=&quot;txt&quot; /&gt;&lt;/span&gt;&lt;/li&gt;
            &lt;li&gt;&lt;span class=&quot;bdl&quot;&gt;&lt;em&gt;*&lt;/em&gt;您的称呼:&lt;/span&gt;&amp;nbsp;&amp;nbsp;&lt;span class=&quot;bdr&quot;&gt;&lt;input id=&quot;names&quot; name=&quot;name&quot; size=&quot;30&quot; class=&quot;txt&quot; /&gt;&lt;/span&gt;&lt;/li&gt;
	    &lt;li&gt;&lt;span class=&quot;bdl&quot;&gt;&lt;em&gt;*&lt;/em&gt;您的手机:&lt;/span&gt;&amp;nbsp;&amp;nbsp;&lt;span class=&quot;bdr&quot;&gt;&lt;input id=&quot;phones&quot; name=&quot;phone&quot; size=&quot;30&quot; class=&quot;txt&quot; /&gt;&lt;/span&gt;&lt;/li&gt;
	    &lt;li class=&quot;gaob&quot;&gt;&lt;span class=&quot;bdl&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;备注留言:&lt;/span&gt;&lt;span class=&quot;bdr&quot;&gt;&lt;textarea id=&quot;notes&quot; name=&quot;note&quot; rows=&quot;3&quot; cols=&quot;26&quot; onfocus=&quot;if(value==&#039;请填写您想说的其他内容！&#039;) {value=&#039;&#039;}&quot; onblur=&quot;if (value ==&#039;&#039;){value=&#039;请填写您想说的其他内容！&#039;}&quot;&gt;请填写您想说的其他内容！&lt;/textarea&gt;&lt;/span&gt;&lt;/li&gt;
	    &lt;li class=&quot;gaoc&quot;&gt;
            &lt;input id=&quot;wfsubmits&quot; type=&quot;image&quot; src=&quot;__PUBLIC__/images/lijishenqing.jpg&quot; 
           onclick=&quot;customerAdds()&quot; name=&quot;wfsubmits&quot; value=&quot;&quot; class=&quot;send&quot; /&gt;
	   &lt;input type=&quot;hidden&quot; value=&quot;&quot; name=&quot;web_ref&quot; id=&quot;web_ref&quot; /&gt;
	    &lt;/li&gt;
        &lt;/ul&gt;	
   &lt;/div&gt;  	
</pre>
<pre class="wp-code-highlight prettyprint">
&lt;script type=&quot;text/javascript&quot;&gt;
      function customerAdds(){
	  $(&quot;#wfsubmits&quot;).attr(&#039;disabled&#039;,&#039;disabled&#039;);
	  var companyname=$(&quot;#companynames&quot;).val();
	  var companyurl=$(&quot;#companyurls&quot;).val();
	  var name=$(&quot;#names&quot;).val();
	  var phone=$(&quot;#phones&quot;).val();
	  var note=$(&quot;#notes&quot;).val();
	
	  if(companyname == &#039;&#039;){
		ui.error(&quot;企业名称不能为空！&quot;);
		$(&quot;#wfsubmits&quot;).removeAttr(&#039;disabled&#039;);
		return false;
	  }
	  if(name == &#039;&#039;){
		ui.error(&quot;您的称呼不能为空！&quot;);
		$(&quot;#wfsubmits&quot;).removeAttr(&#039;disabled&#039;);
		return false;
	  }
	 if(phone == &#039;&#039;){
		ui.error(&quot;手机号码不能为空！&quot;);
		$(&quot;#wfsubmits&quot;).removeAttr(&#039;disabled&#039;);
		return false;
	 }else if(isPhone(phone)==false){
		ui.error(&quot;手机号码格式不正确！&quot;);
		$(&quot;#wfsubmits&quot;).removeAttr(&#039;disabled&#039;);
		return false;
	 }
	 $.post(&quot;{:U(&#039;Home/Index/ajaxAddCustomer&#039;)}&quot;,{companyname:companyname,companyurl:companyurl,name:name,phone:phone,note:note},function(res){
		if(res&gt;0) {
			$(&quot;#companynames&quot;).val(&#039;&#039;);
			$(&quot;#companyurls&quot;).val(&#039;&#039;);
			$(&quot;#names&quot;).val(&#039;&#039;);
			$(&quot;#phones&quot;).val(&#039;&#039;);
			$(&quot;#notes&quot;).val(&#039;&#039;);
			ui.success(&#039;提交成功&#039;);
		}else {
			ui.error(&#039;提交失败&#039;);
		}
		$(&quot;#wfsubmits&quot;).removeAttr(&#039;disabled&#039;);
	});
}
     function isPhone(str){
          var re=/^((\(\d{3}\))|(\d{3}\-))?13[0-9]\d{8}?$|15[0-9]\d{8}?$|18[0-9]\d{8}?$/; 
	  if (re.test(str) != true) {
		return false;
	  }else{
		return true;
	}	
  }
&lt;/script&gt;
</pre>
<p>转载请注明转自:<a href="http://www.yunda51.com">运达's blog</a>  原文地址：<a href="http://www.yunda51.com/1295.html">http://www.yunda51.com/1295.html</a></p>
]]></content:encoded>
			<wfw:commentRss>https://www.yunda51.com/?feed=rss2&#038;p=1295</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
	</channel>
</rss>
