Tuesday, July 31, 2012

How to send data from gwt TextBox to PhP server via JSON

Hi,

I'm trying to send login datato php script, when click on submit button. I want to send data as Post for php script, via JSON.
This is my code on java file (piece of the form):

final TextBox mioUsername = new TextBox();

final PasswordTextBox miaPassword = new PasswordTextBox();

Button accedi = new Button();


//BUTTON SUBMIT

accedi.addClickHandler(new ClickHandler() {

publicvoid onClick(ClickEvent event) {

        RequestBuilder request = new RequestBuilder(RequestBuilder.POST,URL.encode("checklogin.php"));

       

   String name = mioUsername.getText();

String pssw = miaPassword.getText();

        JSONObject jsonValue = new JSONObject();

        jsonValue.put("myusername", new JSONString(name));

        jsonValue.put("mypassword", new JSONString(pssw));

        request.setHeader("Content-Type", "application/json");

        try {

request.sendRequest(jsonValue.toString(),new RequestCallback() {

    @Override

    public void onResponseReceived(Request request, Response response) {


    }


    @Override

    public void onError(Request request, Throwable exception) {

        //displayError("Couldn't retrieve JSON");

    }

});

} catch (com.google.gwt.http.client.RequestException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

 } });


and this is my php script:


<?php


$host="localhost"; // Host name 

$username="usersuper"; // Mysql username 

$password="passw"; // Mysql password 

$db_name="Pippo"; // Database name 

$tbl_name="Users"; // Table name 


// Connect to server and select databse.

mysql_connect("$host", "$username", "$password")or die("cannot connect"); 

mysql_select_db("$db_name")or die("cannot select DB");


// username and password sent from form 

$myusername=$_POST['myusername'];

$mypassword=$_POST['mypassword'];


$sql="SELECT * FROM $tbl_name WHERE Nome='$myusername' and Cognome='$mypassword'";

$result=mysql_query($sql);


// Mysql_num_row is counting table row

$count=mysql_num_rows($result);

echo $count;

// If result matched $myusername and $mypassword, table row must be 1 row

if($count==1){


// Register $myusername, $mypassword and redirect to file "login_success.php"

session_register("myusername");

session_register("mypassword"); 

header("location:login_success.php");

}

else {

echo "Wrong Username or Password";

}

?>
 
I don't know how must implement onResponseReceived method, to ensure that everything runs.

I need help.
Thanks


--
You received this message because you are subscribed to the Google Groups "Google Web Toolkit" group.
To view this discussion on the web visit https://groups.google.com/d/msg/google-web-toolkit/-/1Mxt4PRXndEJ.
To post to this group, send email to google-web-toolkit@googlegroups.com.
To unsubscribe from this group, send email to google-web-toolkit+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/google-web-toolkit?hl=en.

No comments:

Post a Comment