简体   繁体   中英

unable to send ajax request on php

I am using sending an ajax request on php. In form method im using the following code.

rohit1.php

<form action="index1.php" onsubmit="callfunc()">
<span id="raj"></span>
<input type="submit" value="submit"></div>

</FORM>

in javascript im using: callfun.js

function callfunc()
{

var http =new XMLHttpRequest(); 
    var name=prompt('enter name of the form');
    var name="rohit";
    var url = "index1.php";
    var s = document.getElementById('raj').innerHTML;
    alert(http);
    http.open("POST", "index1.php?name="+name, true);

    http.onreadystatechange = function() 
    {
        if(http.readyState == 4 && http.status == 200) 
        {


        }




    }

    http.send();
}

when i am using this function. then on submit this is redirecting on index.php?label= instead of index.php?name= what should I do for this???

Just try to add return false at the end of callfunc() function. It will prevent the form's default event and will not act as a standard form.

<form action="index1.php" onsubmit="callfunc()">
    <span id="raj"></span>
    <input type="submit" value="submit"></div>
</form>

and the appropriate javascript will look like this:

function callfunc() {
    var name = "some name here"; // get this name from whereever you want
    window.location.href = "index1.php?name="+name;
    return false;
}

this should work for you

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