Tuesday, July 31, 2012

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


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();


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() {


    public void onResponseReceived(Request request, Response response) {



    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



 } });

and this is my php script:


$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 



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


// Mysql_num_row is counting table row


echo $count;

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


// Register $myusername, $mypassword and redirect to file "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.

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