The Slim Framework support forum has moved to http://discourse.slimframework.com. This Tender forum is no longer maintained or monitored.

POSTING DATA TO SLIM FRAMEWORK USING JQUERY FROM A JQUERY MOBILE APP

elvisabwa's Avatar

elvisabwa

23 Aug, 2015 03:30 PM

Hi, my code works right when testing on the localhost, but it doesn't work with the framework when I place it to my subdomain. What might be the problem. Below is the jquery and slimframework post codes

$(document).on('pagecontainershow', function (e, ui) {
            var request;
            $('#signupform').submit(function(event){
                if(required($('#firstname').val()) && required($('#lastname').val()) && required($('#gender').val()) && required($('#emailaddress').val()) && required($('#phonenumber').val()) && required($('#password').val()) && required($('#confirmpassword').val()) && required($('#country').val())) {
                    if(isEmail($('#emailaddress').val())) {
                       if(isPhone($('#phonenumber').val())) {
                        if(checkPasswords($('#password').val(), $('#confirmpassword').val())) {
                                if(request){
                                    request.abort();
                                }
                                var $form = $(this);
                                var $inputs = $form.find("input,select,button,textarea");
                                var encodedData = JSON.stringify({
                                    "firstname": $('#firstname').val(),
                                    "lastname": $('#lastname').val(),
                                    "gender": $('#gender').val(),
                                    "emailaddress": $('#emailaddress').val(),
                                    "phonenumber": $('#phonenumber').val(),
                                    "password": $('#password').val(),
                                    "country": $('#country').val()
                                });
                                $inputs.prop("disabled", true);
                                request = $.ajax({
                                    url: "http://mobile.opuula.com/signup",
                                    type: "post",
                                    contentType: 'application/json',
                                    data: encodedData
                                });
                                request.done(function(response, textStatus, jqXHR){
                                    if(response == 'success') {
                                        alert("Sign Up Successful! Now, sign in.");
                                        window.location.href = "signin.html";
                                    } else {
                                        alert(response);
                                    }
                                });
                                request.fail(function(jqXHR, textStatus, errorThrown){
                                    console.error("The following error occurred: " + textStatus, errorThrown);
                                });
                                request.always(function(){
                                    $inputs.prop("disabled", false);
                                    $inputs.val('');
                                    request.abort();
                                });
                                event.preventDefault();
                            } else {
                                alert('Sign Up Failed! The passwords don\'t match. Please, try again');
                            }
                        } else {
                            alert('Sign Up Failed! The phone number should be exactly 9 digits and numeric. Please, try again.');
                        }
                    } else {
                        alert('Sign Up Failed! The email address is invalid. Please, try again.');
                    }
                } else {
                    alert('Sign Up Failed! All fields are required. Please, try again.');
                }
            });
        });

Above is the jquery code I use to submit data to my server. And below is the slim framework php codes

// Validate existance worker email
function validateEmail($emailaddress)
{
    $sql = "SELECT * FROM oa_workers_tb WHERE emailaddress = :emailaddress";
    try {
        $db = connectDb();
        $stmt = $db->prepare($sql);
        $stmt->bindParam("emailaddress", $emailaddress);
        $stmt->execute();
        $validate = $stmt->fetchObject();
        $db = null;
        if($validate){
            return true;
        } else {
            return false;
        }
    } catch(PDOException $e) {
        echo $e->getMessage(); 
    }
}

// Validate existance worker phone
function validatePhone($phonenumber)
{
    $sql = "SELECT * FROM oa_workers_tb WHERE phonenumber = :phonenumber";
    try {
        $db = connectDb();
        $stmt = $db->prepare($sql);
        $stmt->bindParam("phonenumber", $phonenumber);
        $stmt->execute();
        $validate = $stmt->fetchObject();
        $db = null;
        if($validate){
            return true;
        } else {
            return false;
        }
    } catch(PDOException $e) {
        echo $e->getMessage(); 
    }
}

// Insert a new worker
function postWorkers()
{
    $request = \Slim\Slim::getInstance()->request();
    $worker = json_decode($request->getBody());
    if(!validatePhone($worker->phonenumber)) {
        if(!validateEmail($worker->emailaddress)) {
            $sql = "INSERT INTO oa_workers_tb(firstname, lastname, gender, emailaddress, phonenumber, password, country, regdate) VALUES (:firstname, :lastname, :gender, :emailaddress, :phonenumber, :password, :country, :regdate)";
            try {
                $db = connectDb();
                $stmt = $db->prepare($sql);  
                $stmt->bindParam("firstname", $worker->firstname);
                $stmt->bindParam("lastname", $worker->lastname);
                $stmt->bindParam("gender", $worker->gender);
                $stmt->bindParam("emailaddress", $worker->emailaddress);
                $stmt->bindParam("phonenumber", $worker->phonenumber);
                $stmt->bindParam("password", md5($worker->password));
                $stmt->bindParam("country", $worker->country);
                $stmt->bindParam("regdate", time());
                $stmt->execute();
                $db = null;
                echo "success";
            } catch(PDOException $e) {
                echo $e->getMessage(); 
            }
        } else {
            echo "Sign Up Failed! The provided email address exists. Please, try again.";
        }
    } else {
        echo "Sign Up Failed! The provided phone number exists. Please, try again.";
    }
}

The code below works well on my app, which I use to get a list of countries from my database. But the post I don't know what the issue is. Pls help.

Reply to this discussion

Internal reply

Formatting help / Preview (switch to plain text) No formatting (switch to Markdown)

Attaching KB article:

»

Attached Files

You can attach files up to 10MB

If you don't have an account yet, we need to confirm you're human and not a machine trying to post spam.

Keyboard shortcuts

Generic

? Show this help
ESC Blurs the current field

Comment Form

r Focus the comment reply box
^ + ↩ Submit the comment

You can use Command ⌘ instead of Control ^ on Mac