iceteea iceteea - 9 months ago 51
Javascript Question

Submit form and retrieve result using Dojo

Could anyone provide a simple example how to ajaxify a form with dojo and retrieve a result from a php script?


<form action="login.php" method="post">
<input type="text" name="user">
<input type="password" name="password">
<input type="button" value="login">


if($_POST["user"] == "test" && $_POST["password"] == "test") {
echo "youre logged in successfully [REDIRECT HERE]";
else {
echo "you failed epic";


or what?!

Answer Source

xhrPOST - for example

// Local var representing if the form has been sent at all
var hasBeenSent = false;
// Local var representing node to be updated
var messageNode = dojo.byId("messageNode");
// Using dojo.xhrPost, as the amount of data sent could be large
    // The URL of the request
    url: "submission.php",
    // No content property -- just send the entire form
    form: dojo.byId("contactForm"),
    // The success handler
    load: function(response) {
        messageNode.innerHTML = "Thank you for contacting us!";
    // The error handler
    error: function() {
        messageNode.innerHTML = "Your message could not be sent, please try again."
    // The complete handler
    handle: function() {
        hasBeenSent = true;