找回密码
 注册

QQ登录

只需一步,快速开始

查看: 311|回复: 1

process PHP without leaving page FUNCTION onsubmit=“loadXMLdoc()”

[复制链接]
发表于 2012-6-12 08:16:17 | 显示全部楼层 |阅读模式
have a form:
    <form  id="form" onsubmit="loadXMLDoc(); return false;">
    <label>Email:</label>
    <input type="email" name="email" id="email" class="iput" placeholder="jane@doe.com" reguired="required"/>
    <input type="button" id="nbut" onclick="loadXMLDoc()" value="NEXT">
    </form>

The return false prevents the default event handler (form submission) from executing.

call and process the javascript:

function loadXMLDoc() {

var xmlhttp;
if (window.XMLHttpRequest)   {
    // code for IE7+, Firefox, Chrome, Opera, Safari   
    xmlhttp=new XMLHttpRequest();
} else {
   // code for IE6, IE5   
   xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");

}
xmlhttp.onreadystatechange=function()   {   
if (xmlhttp.readyState==4 && xmlhttp.status==200)
     {      
       document.getElementById('response').innerHTML=xmlhttp.responseText;
       document.getElementById('step1').style.display = "none";
       document.getElementById('step2').style.display = "block";
     }   
}

var em = document.getElementById('email');
var em1 = em.value;

xmlhttp.open("GET","bin/process.php?email="+em1,true);
xmlhttp.send();
}




 楼主| 发表于 2012-6-12 08:22:45 | 显示全部楼层
本帖最后由 demo 于 2012-6-13 00:24 编辑

making an IRC type thing to run in a browser window. Ethe way I submit the new message:

send values via AJAX
set action="#" in your form tag this way it wont navigate away...

another way is to use a hidden iframe.               

do something like this:

        Code:
// JavaScript/AJAX

function ajax_setup()
{
    var xmlhttp;
    var contents = document.getElementById("some_id").value;
   
    if (window.XMLHttpRequest)
        xmlhttp=new XMLHttpRequest();
    else
        xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");

    xmlhttp.onreadystatechange=function()
    {
        if(xmlhttp.readyState == 4)
        {
            document.getElementById("text").innerHTML += xmlhttp.responseText;
        }
    }
    xmlhttp.open("GET", "your_php_file.php?your_get_id=" + contents, true);
    xmlhttp.send();
}

Then instead of calling PHP file from the button, call this JavaScript function instead, which calls the PHP file within it. In your PHP file, you would simply echo the return value of the database retrieval somewhere in the source, which would then be handled by the onreadystatechange function and the value would be placed in a text-based element like a <div> or <input type="text" />.               

to use the function as suggested, you need to use that button also and use onclick event handler to call the ajax javascript function               

您需要登录后才可以回帖 登录 | 注册

本版积分规则

手机版|小黑屋|BC Morning Website ( Best Deal Inc. 001 )

GMT-8, 2026-6-11 01:57 , Processed in 0.013440 second(s), 16 queries .

Supported by Weloment Group X3.5

© 2008-2026 Best Deal Online

快速回复 返回顶部 返回列表