0

I have a html form whose data is fetched by a PHP file. Inorder to further manipulate the data, I want it to be sent back to a java file. I'm a newbie. Any help will go a long way.

HTML Code:

<html>
<head>
 <title>Console App</title>
</head>
<body>
 <div class="container-fluid">
                <div class="row">
                    <div class="col-xs-4 col-md-offset-4 vert-offset-top-8">
                        <img class="img-responsive text-center" src="img/logo.png">
                        <br/>
                        <div class="well well-lg" id="well">
                            <form class="form-horizontal" action="ServLogin" method="POST">
                                <div class="form-group">
                                    <label for="inputEmail3" class="col-sm-3 control-label">Username</label>
                                    <div class="col-sm-9">
                                        <input type="text" class="form-control" name="v_username" placeholder="Username">
                                    </div>
                                </div>
                                <div class="form-group">
                                    <label for="inputPassword3" class="col-sm-3 control-label">Password</label>
                                    <div class="col-sm-9">
                                        <input type="password" class="form-control" name="v_password" placeholder="Password">
                                    </div>
                                </div>
                                <button type="submit" class="btn btn-primary btn-block" name="submit" value="submit">Sign in</button>
                            </form>
                        </div>
                    </div>
                </div>
            </div>
</body>
</html>

PHP Code:

<?php
$uname = $_POST["v_username"];
$pwd = $_POST["v_password"];
$submit = $_POST["submit"];

$conn = mysqli_connect("localhost","root","","vconsole");
$array = array('username' => $uname,'password' => $pwd );
if($conn){
    if($submit)
        echo json_encode($array);
}
else{
    echo '<h1> failed </h1>';
}?>

How to fetch the encoded json array in java ? I'm currently working in XAMPP.

4
  • 1
    Have you tried implementing the code in Java at all? It sounds like you're asking someone to just do it for you. Commented Apr 8, 2016 at 4:37
  • @codegooner I suppose you meant javascript instead Commented Apr 8, 2016 at 4:44
  • Maybe this helps stackoverflow.com/questions/6213509/send-json-post-using-php Using the solution provided you can invoke a URL. Responding to that POST request, you can implement your java code in a JSP, a servlet o whatever. Commented Apr 8, 2016 at 4:44
  • RubioRic Sorry my mistake Commented Apr 8, 2016 at 4:49

2 Answers 2

1

What you will require is to make an AJAX request with jQuery.

You can refer to this example to learn how to implement the corresponding javascript with your HTML.

After sending the request, you will receive your response in JSON and you can parse your data accordingly.

P/s: This works with javascript and you may also manipulate your data extensively and remove your dependency with Java for your web program.

Sign up to request clarification or add additional context in comments.

Comments

0

From the current content of your PHP file, you are echoing JSON back to whoever sent that data to PHP file. Since you already have that data in your MySql, why not simply read it out from there? Just use MySql JDBC connector or some ORM (Hibernate) if you prefer and get the data in your Java application.

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

Connection conn = null;
try {
    conn = DriverManager.getConnection("jdbc:mysql://localhost/vconsole?user=root&password=");

    // Query your DB

} catch (SQLException ex) {
    // ex.getMessage(), ex.getSQLState(), ex.getErrorCode());
}

Google will help with the rest.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.