Home >Backend Development >PHP Tutorial >PHP implements partial refresh through iframe iframe refreshes iframe src to obtain elements in iframe
reg.html
There is an iframe tag in the reg.html page, which uses the iframe to request php to verify whether the user name is available.
<code><span><!DOCTYPE html></span><span><<span>html</span>></span><span><<span>head</span>></span><span><<span>meta</span><span>charset</span>=<span>"utf-8"</span>></span><span><<span>title</span>></span> iframe局部刷新<span></<span>title</span>></span><span></<span>head</span>></span><span><<span>body</span>></span><span><<span>form</span><span>method</span>=<span>"post"</span><span>action</span>=<span>""</span>></span> 用户名:<span><<span>input</span><span>type</span>=<span>"text"</span><span>id</span>=<span>"uname"</span><span>name</span>=<span>'username'</span> /></span><span><<span>span</span><span>id</span>=<span>"msg"</span><span>style</span>=<span>"color:red;"</span>></span><span></<span>span</span>></span><span></<span>form</span>></span><span><<span>iframe</span><span>border</span>=<span>'0'</span><span>width</span>=<span>'0'</span><span>height</span>=<span>'0'</span><span>id</span>=<span>'myiframe'</span> ></span><span></<span>iframe</span>></span><span><<span>script</span><span>type</span>=<span>"text/javascript"</span>></span><span><span>//当用户名输入框失去焦点时让iframe请求check_reg.php</span> document.getElementById(<span>'uname'</span>).onblur = <span><span>function</span><span>()</span>{</span><span>if</span>(<span>this</span>.value != <span>''</span>) { document.getElementById(<span>'myiframe'</span>).src = <span>'check_reg.php?username='</span>+ <span>this</span>.value; } } </span><span></<span>script</span>></span><span></<span>body</span>></span><span></<span>html</span>></span></code>
check_reg.php
<code><span><span><?php</span>header(<span>"content-type:text/html;charset=utf-8"</span>); <span>$username</span> = <span>$_GET</span>[<span>'username'</span>]; <span>if</span>(<span>$username</span> == <span>'admin'</span>) { <span>$msg</span> = <span>'对不起,用户名已被使用!'</span>; }<span>else</span> { <span>$msg</span> = <span>'恭喜!该用户名可以使用。'</span>; } <span>echo</span><span><<<STR <script> window.parent.document.getElementById('msg').innerHTML = "$msg"; </script> STR;</span><span>?></span></span></code>').addClass('pre-numbering').hide(); $(this).addClass('has-numbering').parent().append($numbering); for (i = 1; i ').text(i)); }; $numbering.fadeIn(1700); }); });
The above introduces the partial refresh of PHP through iframe, including the content of iframe. I hope it will be helpful to friends who are interested in PHP tutorials.