简体   繁体   中英

Ajax post opens a new window after submitting

I'm trying to submit a form and get a response using Ajax, but when i submit the form a new window is opened with the values i typed
The function works and do what it should, it's just the new window that is the problem
Here is the HTML code :

<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <link href="chat_style.css" rel="stylesheet" type="text/css">
</head>
<body>
<div id = "refresh" class="refresh"></div>
<form method="POST" action="save.php" name="chat_send"  onsubmit="sendChatData(); return false;">
    <input id="sender" name="sender" type="hidden" value ="<?php echo $sender ?>">
    <?php echo "$sender:" ?> <input name="texta" type="text" id="texta"/>
    <input name="submit" type="submit" value="Send" />
</form>

And the JS code :

function sendChatData() {

    var xmlHttpReq = false;
    var self = this;
    if (window.XMLHttpRequest) {
        self.xmlHttpReq = new XMLHttpRequest();
    }
    else if (window.ActiveXObject) {
        self.xmlHttpReq = new ActiveXObject("Microsoft.XMLHTTP");
    }

    self.xmlHttpReq.open('POST', 'save.php' , true);
    self.xmlHttpReq.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
    self.xmlHttpReq.onreadystatechange = function() {
        document.getElementById("texta").innerHTML = "";
        if (self.xmlHttpReq.readyState == 4) {
            var x = self.xmlHttpReq.responseText;
        }
    }
    self.xmlHttpReq.send(getquerystring());
}

function getquerystring() {
    var qstr = "message=";
    try {
        qstr += document.getElementById("texta").value;
        window.open(qstr);
    } catch (e) {
        // empty...
    }
    return qstr;
}

In your code, you have a call to window.open . It does that - opens a new (browser) window!

function getquerystring() {
    var qstr = "message=";
    try {
        qstr += document.getElementById("texta").value;
        window.open(qstr);   // <-- Does exactly that - opens a new window!
    } catch (e) {
        // empty...
    }
    return qstr;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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