saurabh sharma saurabh sharma - 1 year ago 69
Javascript Question

pass the value to databse

I want to pass the value from the Ajax code to the database as this program is to show the details of the user, but there is an error in the code in passing the value. What can I do now?

function showUser() {
httpRequest = new XMLHttpRequest();

if (!httpRequest) {
alert('Giving up :( Cannot create an XMLHTTP instance');
return false;
var id = document.getElementById("id").value;
httpRequest.onreadystatechange = alertContents;"GET", "http://localhost/cart/guser.php?id=" + id + "&rand=" + , true);

function alertContents() {
if (httpRequest.readyState === XMLHttpRequest.DONE) {
if (httpRequest.status === 200) {
document.getElementById("txtHint").innerHTML = httpRequest.responseText;
var id = document.getElementById('id').value;


enter digit :
<input type="text" id="id" name="id" />
<br />
<input type='button' onclick='showUser(this.value)' value='select' />

<div id="txtHint"><b>Person info will be listed here...</b>

below code is for guser.php

<!DOCTYPE html>
table {
width: 100%;
border-collapse: collapse;

table, td, th {
border: 1px solid black;
padding: 5px;

th {text-align: left;}

$q = intval($_GET['q']);

$servername = "localhost";
$username = "root";
$password = "";
$dbname = "cart";

$conn = mysqli_connect($servername, $username, $password, $dbname);

if (mysqli_connect_errno())
echo "Failed to connect to MySQL: " . mysqli_connect_error();


$sql="SELECT * FROM user_details WHERE id = '".$q."'";
$result = mysqli_query($con,$sql);

echo "<table>
while($row = mysqli_fetch_array($result)) {
echo "<tr>";
echo "<td>" . $row['FirstName'] . "</td>";
echo "<td>" . $row['LastName'] . "</td>";
echo "<td>" . $row['email'] . "</td>";
echo "</tr>";
echo "</table>";

Answer Source

Try This:

Add in head:<script src=""></script>

you have to type person id in inputbox and user info in texthint div:

you will get id in guser.php of user from post and run query.and echo user info into guser.php file

<script type="text/javascript">
    function showUser($id){

        type: "POST",
        url: "http://localhost/cart/guser.php",
        success: function(data){

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download