1

unable to get contnt from daabase when we select a value from database.Please help me regarding this.

drop.php

I Have a page as drop.php which contains the following code.

<html>
<head>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
<script>
function showUser(id) {
    //get the selected value

    //make the ajax call
    $.ajax({
        url: 'getuser.php',
        type: 'GET',
        data: {option : id},
        success: function(data) {
           document.getElementById('txtHint').innerHTML=data;
        }
    });
}
</script>
</head>
<body>
<?php
include("database.php");
include("session.php");
$query = "SELECT * FROM invoicetable WHERE username='$_SESSION[username]'";
$result = mysql_query($query);
echo'<select name="users" onchange="showUser(this.value)">';
echo '<option value="">'.'--- Select ---'.'</option>';
while($row = mysql_fetch_assoc( $result )) { 
        echo '<option value="'.$row['id'].'">' . $row['name'] . '</option>';   
}
echo '</select>';?>
<br>
<div id="txtHint"><b></b></div>

</body>
</html>

getuser.php

<?php
include("database.php");
include("session.php");
$sql="SELECT address FROM invoicetable WHERE username='$_SESSION[username]'";

$result = mysql_query($sql);

echo "<table border='1' style='width:500'>
<tr>
<th>Address</th>

</tr>";

$row = mysql_fetch_array($result); 
  echo "<tr>";
  echo "<td>" . $row['address'] . "</td>";

  echo "</tr>";

echo "</table>";
?>

unable to get contnt from daabase when we select a value from database.Please help me regarding this. Thanks in advance

3 Answers 3

1

Try this code in getuser.php

$userid = $_request['option'];
$sql="SELECT address FROM invoicetable WHERE Id='$userid'";
Sign up to request clarification or add additional context in comments.

1 Comment

i have two fields with same username.username stored in session
0

If you have two fields with same username.For resolve this issue you have to make id column in table is primary key.So that id filed will be different for both the users that have same username.

$id = $_REQUEST['option'];

$sql="SELECT address FROM invoicetable WHERE username='$_SESSION[username]' AND id=$id";

Try this it will work.

Comments

0

A simple mistake in this is in getuser.php file you are not taking the userId from ajax call rather than you are using SESSION_DATA which remain unchanged.

you can just take userId as $id = $_GET['option'];

and change query to

  $sql="SELECT address FROM invoicetable WHERE id=$id";

1 Comment

i have two fields with same username.username stored in session

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.