火枪手 發表於 2019-9-15 16:02:00

前端html,后端php,数据库mysql 之间的交互

<p>前端向后端发送数据:ajax方法</p>
<p>  &nbsp;get/post方法:</p>
<div class="cnblogs_code">
<p>&lt;script&gt;</p>
<p> document.onclick = function(){<br>      ajax({<br>            // type:"get",               //发送方式,可选,默认get<br>            url:"http://localhost/ajax/php/index.php",   //要请求的地址,必选<br>            success:function(res){         //请求成功之后的函数,必选<br>                console.log(res)<br>            },<br>            data:{                      //要发送的数据,可选,默认不发<br>                user:"admin",<br>                pass:13123121123<br>            },<br>            // error:function(res){         //请求失败之后的函数,可选,默认不处理<br>            //   console.log(res)<br>            // },<br>            // timeout:10                  //请求超时的时间,可选,默认2000<br>      })<br>    }</p>
<p>    function ajax(options){<br>      // 1.处理默认参数<br>      var {type,url,success,error,data,timeout} = options;<br>      type = type || "get";<br>      data = data || {};<br>      timeout = timeout || 2000;</p>
<p>&nbsp;</p>
<p>      // 2.解析要发送的数据<br>      var str = "";<br>      for(var i in data){<br>            str += `${i}=${data}&amp;`;<br>      }</p>
<pre></pre>
<p> // 3.根据方式,决定是否处理url<br>      if(type == "get"){<br>            var d = new Date();<br>            url = url + "?" + str + "__qft=" + d.getTime();<br>      }</p>
<pre></pre>
<p> // 4.开启ajax<br>      var xhr = new XMLHttpRequest();<br>      // 注意:open中的方式<br>      xhr.open(type,url,true);<br>      xhr.onreadystatechange = function(){<br>            if(xhr.readyState == 4 &amp;&amp; xhr.status == 200){<br>                // 5.执行成功之前,先判断是否传入<br>                success &amp;&amp; success(xhr.responseText);<br>                // 成功之后,不应有失败<br>                error = null;<br>            }else if(xhr.readyState == 4 &amp;&amp; xhr.status != 200){<br>                // 6.执行失败之前,先判断是否传入<br>                error &amp;&amp; error(xhr.status);<br>                // 失败之后,不应有成功<br>                success = null;<br>                // 且失败不应多次执行<br>                error = null;<br>            }<br>      }</p>
<pre></pre>
<p> // 7.如果请求超时,执行失败<br>      setTimeout(() =&gt; {<br>            error &amp;&amp; error("timeout");<br>            // 失败之后,不应有成功<br>            success = null;<br>      }, timeout);</p>
<pre></pre>
<p> // 8.最后根据type的方式,决定send的发送内容和格式<br>      if(type == "post"){<br>            xhr.setRequestHeader("Content-type","application/x-www-form-urlencoded");<br>            xhr.send(str)<br>      }else{<br>            xhr.send()<br>      }<br>    }</p>

</div>
<p>后台接受数据并返回到前端:php方法</p>
<div class="cnblogs_code">
<pre>&lt;?<span style="color: rgba(0, 0, 0, 1)">php

    $u </span>= @$_REQUEST[<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">user</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">];
    $p </span>= @$_REQUEST[<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">pass</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">];

    echo </span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">这是前端的ajax发送的数据,现在再还给前端:</span><span style="color: rgba(128, 0, 0, 1)">"</span>.$u.<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">-------</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">.$p;

</span>?&gt;</pre>
</div>
<p>在PHP中使用MySQL:</p>
<p>  注意运行的环境,可以在本地服务器上运行</p>
<div class="cnblogs_code">
<pre>&lt;?<span style="color: rgba(0, 0, 0, 1)">php
    </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 1.登录mysql,选择数据库</span>
    $link = @new mysqli(<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">localhost:3306</span><span style="color: rgba(128, 0, 0, 1)">"</span>,<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">root</span><span style="color: rgba(128, 0, 0, 1)">"</span>,<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">root</span><span style="color: rgba(128, 0, 0, 1)">"</span>,<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">test</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">);
    </span><span style="color: rgba(0, 0, 255, 1)">if</span>($link-&gt;<span style="color: rgba(0, 0, 0, 1)">connect_error){
      echo $link</span>-&gt;<span style="color: rgba(0, 0, 0, 1)">connect_error;
    }

    </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 2.$link-&gt;query()向mysql发送命令
    </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 增
    </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> $q = "INSERT stu (name,sex,age) VALUES('admin','1',16)";
    </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> $res = $link-&gt;query($q);
    </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> if($res){
    </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">   echo "insert ok";
    </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> }else{
    </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">   echo "insert no ok";
    </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> }

    </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 删
    </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> $q = "DELETE FROM stu WHERE id=6";
    </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> $res = $link-&gt;query($q);
    </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> if($res){
    </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">   echo "delete ok";
    </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> }else{
    </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">   echo "delete no ok";
    </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> }

    </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 改
    </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> $q = "UPDATE stu SET name='root' WHERE id=6";
    </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> $res = $link-&gt;query($q);
    </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> if($res){
    </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">   echo "update ok";
    </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> }else{
    </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">   echo "update no ok";
    </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> }

    </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 查</span>
    $q = <span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">SELECT * FROM stu</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">;
    $res </span>= $link-&gt;<span style="color: rgba(0, 0, 0, 1)">query($q);
    </span><span style="color: rgba(0, 0, 255, 1)">if</span><span style="color: rgba(0, 0, 0, 1)">($res){
      echo </span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">select ok</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">;
    }</span><span style="color: rgba(0, 0, 255, 1)">else</span><span style="color: rgba(0, 0, 0, 1)">{
      echo </span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">select no ok</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">;
    }
</span>?&gt;</pre>
</div>
<p>&nbsp;</p><br><br>
来源:https://www.cnblogs.com/hupeng1996/p/11510495.html
頁: [1]
查看完整版本: 前端html,后端php,数据库mysql 之间的交互