简体   繁体   English

Javascript-使用AJAX发布表单并获取返回值

[英]Javascript - Posting a form with AJAX and getting return values

I have now got everything to post correctly but this script keeps loading it into a new page. 现在,我可以正确发布所有内容,但是此脚本不断将其加载到新页面中。 Is it something to do with the way my php file returns it? 这与我的php文件返回它的方式有关吗? "echo(json_encode($return_receipt));" “回声(json_encode($ return_receipt));”

    <script>        
    // Get XML HTTP Type
    function get_XmlHttp() {
        var xmlHttp = null;
            if(window.XMLHttpRequest) {
                xmlHttp = new XMLHttpRequest();
            }else if(window.ActiveXObject) {
                xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
            }
        return xmlHttp;
    }

    function ajaxSuccess () {
        alert(this.responseText);
    }

    function ajaxrequest(oFormElement,tagID) {
        //Get The Correct XMLHTTP Object
        var request = new XMLHttpRequest();             
        request.onload = ajaxSuccess;
        request.open(oFormElement.method, oFormElement.action, true);
        //request.setRequestHeader("Content-type","application/x-www-form-urlencoded");
        request.send(new FormData(oFormElement));           
    }
    </script>

you have two easy options.. 您有两个简单的选择。

1.you can use jquery ajax method... or 2. javascript form submission without loading the page by targetting the form to the hidden inline frame 1.您可以使用jquery ajax方法...或2.通过将表单定位到隐藏的嵌入式框架来提交表单而不用加载javascript

here i'm using it for image upload image name without hitting submit button and save the data to the database table without reloading the page .. edit it according to your convenience. 在这里,我将其用于图像上传图像名称,而无需单击提交按钮,并且无需重新加载页面即可将数据保存到数据库表中。.根据您的方便对其进行编辑。

function upload(img)//Javascript function
{
var types = /\.jpg|\.gif|\.jpeg/i;
var filename = img.value;
if (filename.search(types) == -1) {
alert("Select only images ( jpg or gif or jpeg format)");
img.form.reset();
return false;
}

img.form.action = 'upload-picture.php';
img.form.target = 'iframe';
img.form.submit();
return true;
}

// here is iframe on the same page //这是同一页上的iframe

<iframe name="iframe" id="u_iframe" style="display:none;"></iframe>


<input type="file" name="picture" id="picture" onchange="return upload(this);" />

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM