Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
967 views
in Technique[技术] by (71.8m points)

worklight adapters - echo posted json data in server php

The json sent to server php file as post request from ibm worklight Http adapter not accessible even after decoding:
Here are the codes:

Http adapter:

function storeRegistrationData(emailVal, passVal, fname1, gender, phone, userType, vehType) {
    var credentials = JSON.stringify({jemailVal: emailVal, jpassVal: passVal, jfname1: fname1, jgender: gender, jphone: phone, juserType : userType, jvehType : vehType});

    var input = {
        method : 'post',
        returnedContentType : 'json',
        path : "/carbikepooling/index.php",
        parameters: {credentials: credentials}
    };
    return WL.Server.invokeHttp(input);
}

Server php code:

$jsonObj = (isset($_POST['credentials']) ? $_POST['credentials'] : null);
        $credentials = json_decode($jsonObj);
        $email = $credentials->jemailVal;
        $pass  = $credentials->jpassVal;
        $uname = $credentials->jfname1;
        $gender = $credentials->jgender;
        $phone = $credentials->jphone;
        $usertype = $credentials->juserType;
        $vehtype = $credentials->jvehType;
        $boolean = false;
        $userId; $userTypeId;

        // sanitation, database calls, etc

        $connection = mysqli_connect("localhost","root","","carbikepooling");


                     if (mysqli_connect_errno($connection))
                     {
                      echo "Failed to connect to MySQL: " . mysqli_connect_error();
                     }

                    mysqli_query($connection,"insert into userInfo values(0,".$email.",'".$pass."','".$uname."','".$gender."','".$phone."')");

                        $result1 = mysqli_query($connection, "select u.userId from userInfo u order by u.userId desc limit 1");

                        if($result1){
                            $row1 = mysqli_fetch_row($result1);
                            $userId = $row1[0];
                            mysqli_query($connection,"insert into userType values(0,".$userId.",'".$usertype."')");
                            $result2 = mysqli_query($connection, "select t.userTypeId from userType t order by t.userTypeId desc limit 1");

                            if($result2){
                                    $row2 = mysqli_fetch_row($result2);
                                    $userTypeId = $row2[0];
                                    mysqli_query($connection, "insert into vehicleType values(0,".$userTypeId.",'".$vehtype."')");
                                    $boolean = true;
                                }
                        }

                    mysqli_close($connection);  

        $returnData[] = array(
            'boolean' => "$boolean"
        );

        echo json_encode($returnData);
?>

Javascript code to show return result from server php:

function storeFormData(emailVal, passVal, fname1, gender, phone, userType, vehType){

    var invocationData = {
            adapter : 'registerUser',
            procedure : 'storeRegistrationData',
            parameters : [emailVal, passVal, fname1, gender, phone, userType, vehType]
    };

    WL.Client.invokeProcedure(invocationData, {
        onSuccess : storeFormDataSuccess,
        onFailure : storeFormDataFailure,
    });
}

function storeFormDataSuccess(result){
    WL.Logger.debug('Data stored Successfully');
    alert(JSON.stringify(result.invocationResult));
}

function storeFormDataFailure(result){
    WL.Logger.error('Error in storing Data');
}

What happens is that when I used decoded json data in server php file in the insert statements, nothing happens. I tried echoed values of email, gender, etc. but nothing prints as if they contain no values. However, if I sent the decoded json values of email, gender etc. in the returnData array as they are used in insert statements, values successfully received by the app which made the request, i.e: shown those values in the alert in the js code.
Please solve this problem?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Hmm, that is strange. I copied and pasted your exact code and I was able to echo the variables that were passed from the Worklight adapter. Maybe the way you are trying to use the variables for your sql statements is wrong?

Can you change your mysql query to look something more like this?

mysqli_query($connection, "insert into vehicleType values(0, '$userTypeId','$vehtype')");

Notice how I've removed the concatenation '.' from the query and I now I just have the variables surrounded by single quotes.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share
...