1

I'm trying to retrieve data from multiple table. So when the user clicks and select an item php file to retrieve the data from the corresponding table.

this is my first try on Ajax used w3 school code and trying to modify, I guess the way I'm using if condition is the problem? Before I try with the multiple table it worked.

My Script

 <script>
function showUser(str) {
if (str == "") {
    document.getElementById("txtHint").innerHTML = "Please Select type of users to view";
    return;
} else { 
    if (window.XMLHttpRequest) {
        // code for IE7+, Firefox, Chrome, Opera, Safari
        xmlhttp = new XMLHttpRequest();
    } else {
        // code for IE6, IE5
        xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
    }
    xmlhttp.onreadystatechange = function() {
        if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
            document.getElementById("txtHint").innerHTML = xmlhttp.responseText;
        }
    }
    xmlhttp.open("GET","get_users.php?q="+str,true);
    xmlhttp.send();
  }
}
</script>

My Form

<form>
<select name="users" onchange="showUser(this.value)">
<option value="">Select a person:</option>
<option value="1">Staff</option>
<option value="2">Admin</option>
<option value="3">Customers</option>
</select>
</form>

My PHP Page

<?php
$q = intval($_GET['q']);

$con = mysqli_connect('localhost','X','X','X');
if (!$con) {
die('Could not connect: ' . mysqli_error($con));
}

mysqli_select_db($con);

if ($q='Admin')
{

$sql="SELECT * FROM admin";
$result = mysqli_query($con,$sql);

echo "<table>
<tr>
<th>Email</th>
<th>Name</th>
<th>Mobile</th>
<th>Address</th>
<th>Password</th>
</tr>";
while($row = mysqli_fetch_array($result)) {
echo "<tr>";
echo "<td>" . $row['email'] . "</td>";
echo "<td>" . $row['name'] . "</td>";
echo "<td>" . $row['mobile'] . "</td>";
echo "<td>" . $row['address'] . "</td>";
echo "<td>" . $row['password'] . "</td>";
echo "</tr>";
}
echo "</table>";

else if ($q='Staff')
 {
echo "This is from Database B
}

else
{
echo "This is from database C";
}

mysqli_close($con);
?>
1
  • 1
    this is failing for 2 reasons if ($q='Admin') and this else if ($q='Staff') possibly 3 reasons. Commented Oct 24, 2015 at 0:59

1 Answer 1

1

You're using $q = intval($_GET['q']); with a string value for your assignments.

intval() "Get the integer value of a variable"

<option value="1">Staff</option>
<option value="2">Admin</option>
<option value="3">Customers</option>

and using an assignment rather than a comparison for both if ($q='Admin') and else if ($q='Staff') both being strings, as opposed to integers in your options.

which should read as and with 2 equal signs if ($q=='2') and else if ($q=='1') in order to match your select's numerical value options.

Or, change them accordingly with your options to read as Staff and Admin (and Customers) in the values, while removing the intval() from the GET array; the choice is yours.

You also don't need mysqli_select_db($con); since you've declared your 4 parameters in your initial connection and would fail for another reason; technically you didn't select a database for it.

You're also missing a quote and semi-colon in (a parse syntax error)

echo "This is from Database B

which should read as

echo "This is from Database B";

Footnotes:

The page which I believe you based yourself on, seems to be http://www.w3schools.com/php/php_ajax_database.asp

If so, then I suggest you follow their example completely and modify it slowly to fit your needs. I myself have used that demo (in the distant past) before with success.

Their example uses a WHERE clause also.

$sql="SELECT * FROM user WHERE id = '".$q."'";

Edit: Just for the record, there is a missing brace for this condition:

if ($q='Admin')
{
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks. Also I have missed } in if condition. :-)
@CJM You're welcome, glad to have been of help, cheers. Edit: Yeah, I just spotted that now. My bad. Thanks for letting me know. I made an additional edit for it.

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.