English 中文(简体)
我如何利用一个提交国启动非洲复兴开发银行的职能?
原标题:How do I use a submit button to activate an AJAX function?

我的网页上有以下表格和javascript功能。 这是我用来检验我是否愿意做的事情的 du。

我试图做的是,有一种表格向服务器发送了美国宇宙航空研究开发机构的请求,以便服务器能够更新数据库,而该网页本身继续沿着预定的道路前进。 我处在一个紧张的时期,因此,不幸的是,我没有时间重整整页,以更好地支持这一点。 我的问题是<代码>xml。 物体似乎没有适当返回。 我拿到4个读写的国家,但有一个<>代码>status0?

我的守则如下:

ajax.php

<html>
<head>
<script type="text/javascript">
function test(){
    var xmlhttp = new XMLHttpRequest();

    xmlhttp.onreadystatechange=function(){
        document.getElementById("new").innerHTML+="<b>"+xmlhttp.readyState+"</b> "+xmlhttp.status;

        if (xmlhttp.readyState==4 && xmlhttp.status==200){
            document.getElementById("hello").innerHTML=xmlhttp.responseText;
        }
    }

    xmlhttp.open("GET","答复:php",true);
    xmlhttp.send();
}
</script>
</head>

<body>
<?php
if ($_POST){
  echo $_POST[ hello ];
}
?>

<form action="ajax.php" method="post">
<input type="text" name="hello" />
<input type="submit" value="submit" onclick="test();" />
</form>

<div id="hello"></div>
<h3>debug</h3>
<div id="new"></div>
</body>
</html>

答复:php

<?php
echo "Hello there";
?>

http://www.ohchr.org。

Please note that I do not want to prevent the default behavior. In fact, the forn must be submitted as usual. I simply want to add an AJAX request to the process.

最佳回答

您可以提出非洲宇宙航空研究开发机构的请求,并允许表格同时提交。 当页重载时(如提交表格时)就取消了AJAX号的不完整要求。 你们要么不得不而不是提交表格,要么等到您的AJAX呼吁完成后才提交表格。 如果你想走第二条路,你可以修改你的法典如下:

<html>
<head>
<script type="text/javascript">
function test(){
    var xmlhttp = new XMLHttpRequest();

    xmlhttp.onreadystatechange=function(){
        document.getElementById("new").innerHTML+="<b>"+xmlhttp.readyState+"</b> "+xmlhttp.status;

        if (xmlhttp.readyState==4 && xmlhttp.status==200){
            document.getElementById("hello").innerHTML=xmlhttp.responseText;
            **document.getElementById("ajaxform").submit();**
        }
    }

    xmlhttp.open("GET","response.php",true);
    xmlhttp.send();
}
</script>
</head>

<body>
<?php
if ($_POST){
  echo $_POST[ hello ];
}
?>

<form action="ajax.php" method="post" **id="ajaxform"**>
<input type="text" name="hello" />
<input type="submit" value="submit" onclick="test();**return false;**" />
</form>

<div id="hello"></div>
<h3>debug</h3>
<div id="new"></div>
</body>
</html>

改动/增补的编号为**

请注意,我在这里不喜欢一些做法,特别是使用<条码>onsubmit等,即超文本标记的属性,以随附 Java稿处理器。

问题回答

我知道,这 t不直接回答你的问题,但如果你被困在一段时间内,我就建议,只是利用 j来处理AJax。

http://api.jquery.com

如果需要的话,我可以列举一些法典的例子。

一旦提交纽顿,浏览器就会向服务器提交数据,新页面将装上。 你可以约束提交表格的活动,如果是的话,会发出亚克斯呼吁,但你将有两个问题。

  1. The browser may redirect before all ajax data is sent
  2. You have no idea if the ajax call was successful or not

我将尝试用隐藏的投入,用同样的形式发送你通过麻省发送的数据。 如果这两条电话都针对不同的导游,则试图:

var ajaxSent = false;
$( #myForm ).submit(function(e){
    if (!ajaxSent){
        e.preventDefault();
        $.ajax({
            url: "ajaxUrl",
            // data, method, etc
            success: function(){
                ajaxSent = true;
                $( #myForm ).trigger( submit );
            }
    }
    // else submit the form
});




相关问题
CSS working only in Firefox

I am trying to create a search text-field like on the Apple website. The HTML looks like this: <div class="frm-search"> <div> <input class="btn" type="image" src="http://www....

image changed but appears the same in browser

I m writing a php script to crop an image. The script overwrites the old image with the new one, but when I reload the page (which is supposed to pickup the new image) I still see the old one. ...

Firefox background image horizontal centering oddity

I am building some basic HTML code for a CMS. One of the page-related options in the CMS is "background image" and "stretch page width / height to background image width / height." so that with large ...

Separator line in ASP.NET

I d like to add a simple separator line in an aspx web form. Does anyone know how? It sounds easy enough, but still I can t manage to find how to do it.. 10x!

热门标签