Maison  >  Article  >  php教程  >  php无刷新更新config文件

php无刷新更新config文件

WBOY
WBOYoriginal
2016-06-06 19:47:171096parcourir

本用例包含3个文件:index.php、config.php、update_process.php 1、index.php 主要功能:负责显示config.php中的数据信息,并可供修改。 包含技术: 1)$.ajax 2)data = eval('({'+data.trim(',')+'})'); json格式字符串转换成json对象 源码: 1 html 2 he

本用例包含3个文件:index.php、config.php、update_process.php

1、index.php

     主要功能:负责显示config.php中的数据信息,并可供修改。

     包含技术:

         1)$.ajax

    2)data = eval('({'+data.trim(',')+'})'); json格式字符串转换成json对象

   源码:

<span> 1</span> 
<span> 2</span> 
<span> 3</span>     <title>config 文件更新</title>
<span> 4</span>     <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<span> 5</span>     <meta http-equiv="Content-Language" content="zh-CN">
<span> 6</span>     <script type="text/javascript" src="http://files.cnblogs.com/Zjmainstay/jquery-1.6.2.min.js"></script>
<span> 7</span> 
<span> 8</span> 
<span> 9</span> 
<span>10</span> <span>php
</span><span>11</span>     <span>header</span>("Content-type: text/html; charset=utf-8"<span>);
</span><span>12</span> 
<span>13</span>     <span>//</span><span>获取config.php数据</span>
<span>14</span>     <span>ob_start</span><span>();
</span><span>15</span>     <span>include</span>('config.php'<span>);
</span><span>16</span>     <span>$configData</span> = <span>ob_get_contents</span><span>();
</span><span>17</span>     <span>ob_clean</span><span>();
</span><span>18</span>     <span>eval</span>(<span>$configData</span>); <span>//</span><span>执行config.php中的变量</span>
<span>19</span> 
<span>20</span>     <span>foreach</span>(<span>$config</span> <span>as</span> <span>$key</span> => <span>$value</span><span>){
</span><span>21</span>         <span>echo</span> "key:<input type="text" value="{<span>$key</span>}" disabled>  value:<input class="input" id="{<span>$key</span>}" type="text" value="{<span>$value</span>}"><br>"<span>;
</span><span>22</span> <span>    }
</span><span>23</span>     <span>echo</span> '<input type="button" value="保存" id="Save">'<span>;
</span><span>24</span>     <span>echo</span> '<div id="feedback"></div>'<span>;
</span><span>25</span> ?>
<span>26</span> <script type="text/javascript">
<span>27 $(document).ready(<span>function<span>(){
<span>28     $("#Save").click(<span>function<span>(){
<span>29         <span>var inputs = $(".input"<span>);
<span>30         <span>var data = ''<span>;
<span>31         $.<span>each(inputs,<span>function(i,<span>input){
<span>32             <span>//<span>拼接json格式字符串
<span>33             data += '"' + $(input).attr('id') + '":"' + $(input).val() + '",'<span>;
<span>34 <span>        });
<span>35         <span>//<span>字符串转换成json
<span>36         data = <span>eval('({'+data.<span>trim(',')+'})'<span>);
<span>37         $.<span>ajax({
<span>38             url:'update_process.php',
<span>39             type:'POST',
<span>40             data:data,
<span>41             success:<span>function<span>(result){
<span>42                 $("#feedback").hide().<span>empty().append(result).toggle(1000<span>);
<span>43                 setTimeout('$("#feedback").toggle(1000)',2000<span>);
<span>44             },
<span>45             error:<span>function<span>(msg){
<span>46                 alert('Error!'+<span>msg);
<span>47 <span>            }
<span>48 <span>        });
<span>49 <span>    });
<span>50 <span>});
<span>51 </script>
<span>52</span> 
<span>53</span> 

 

2、config.php

   数据文件。

   数据内容:

<span>1</span> <span>$config</span>['a_a']='a1'<span>;
</span><span>2</span> <span>$config</span>['a_b']='a2'<span>;
</span><span>3</span> <span>$config</span>['a_c']='a3';


3、update_process.php

  主要功能:负责接收ajax上传数据并更新config.php,反馈更新成功与否信息。

  源码: 

<span> 1</span> <span>php
</span><span> 2</span>     <span>if</span>(<span>empty</span>(<span>$_POST</span><span>)) {
</span><span> 3</span>         <span>echo</span> 'Post empty data!'<span>;
</span><span> 4</span> <span>    }
</span><span> 5</span>     
<span> 6</span>     <span>ob_start</span><span>();
</span><span> 7</span>     <span>include</span>('config.php'<span>);
</span><span> 8</span>     <span>$configData</span> = <span>ob_get_contents</span><span>();
</span><span> 9</span>     <span>ob_clean</span><span>();
</span><span>10</span>     <span>eval</span>(<span>$configData</span><span>);
</span><span>11</span>     
<span>12</span>     <span>$data</span> = ''<span>;
</span><span>13</span>     <span>foreach</span>(<span>$config</span> <span>as</span> <span>$key</span> => <span>$value</span><span>){
</span><span>14</span>         <span>//</span><span>以config.php中的变量为标准,POST值存在则更新,否则保留config.php中的变量值。</span>
<span>15</span>         <span>if</span>(<span>isset</span>(<span>$_POST</span>[<span>$key</span>])) <span>$data</span> .= "\$config['{<span>$key</span>}']='{<span>$_POST</span>[<span>$key</span>]}';\n"<span>;
</span><span>16</span>         <span>else</span> <span>$data</span> .= "\$config['{<span>$key</span>}']='{<span>$value</span>}';\n"<span>;
</span><span>17</span> <span>    }
</span><span>18</span>     
<span>19</span>     <span>file_put_contents</span>('config.php',<span>$data</span><span>);
</span><span>20</span>     <span>echo</span> 'Success Updated!'<span>;
</span><span>21</span> <span>//</span><span>End_php</span>

 软件包下载:config文件无刷新更新源码

演示地址:config文件无刷新更新演示

Déclaration:
Le contenu de cet article est volontairement contribué par les internautes et les droits d'auteur appartiennent à l'auteur original. Ce site n'assume aucune responsabilité légale correspondante. Si vous trouvez un contenu suspecté de plagiat ou de contrefaçon, veuillez contacter admin@php.cn