javascript - Why onreadystatechange fails when trying to POST data to php script? -



javascript - Why onreadystatechange fails when trying to POST data to php script? -

i have next javascript code using ajax send input info database form in notebook.php when submit button clicked. function called addnote() should handle ajax operation posting info add.php.

for reason doesn't work. onreadystate function seems fail , response text add.php never received variable res. have no thought why. there missing code or what?

here contents of files notebook.js, notebook.php.

notebook.js:

function addnote() { ... xhr.onreadystatechange = function () { if (xhr.readystate == 4 && xhr.status == 200) { // compelete & ok var res = xhr.responsetext; } } var notetext = document.getelementbyid("notetext").value; xhr.open("post", "add.php", true); xhr.setrequestheader("content-type", "application/x-www-form-urlencoded"); xhr.send("notetext=" + notetext); } function main() { $("#submitbtn").click(function() { addnote(); }); } $(document).ready(main);

notebook.php:

... <body> <h1>notebook</h1> <div class="input"> <form> <input type="text" name="notetext" id="notetext"> <input type="submit" value="add" id="submitbtn"> </form> </div> <ul> <?php // add together stuff list ?> </ul> </body> </html>

you not cancel click of submit button form submits page.

$("#submitbtn").click(function(evt) { evt.preventdefault(); //cancel form submission cancelling click action. addnote(); });

javascript php jquery ajax xmlhttprequest

Comments

Popular posts from this blog

Delphi change the assembly code of a running process -

json - Hibernate and Jackson (java.lang.IllegalStateException: Cannot call sendError() after the response has been committed) -

C++ 11 "class" keyword -