Reputation: 23
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.
Upvotes: 0
Views: 194
Reputation: 431
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.
Upvotes: 1
Reputation: 11813
From the current content of your PHP file, you are echo
ing 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.
Upvotes: 0