i have now searched the web for hours and i still cant figure it out.
So iam now asking you guys:
Okay, i have ha website, and a MS SQL server, (loalhost), and I want this function to work:
First: I run a query to print out all costumers in my database (in a table):`
$searchq = $_POST['search'];
$resultArray = array();
$sql = "SELECT * FROM Costumers WHERE firstname LIKE '%".$searchq."%' ";
$stmt = sqlsrv_query( $conn, $sql);
if( $stmt == false){
die( print_r(sqlsrv_errors(), tue) );
}
Then I print the result in a table:
echo "<table border='1'>";
echo "<tr><th>CustomerID</th><th>Firstname</th><th>Lastname</th><th>Tlf</th><th>Email</th><th>Productnr</th><th>Address</th></tr>";
while($row = sqlsrv_fetch_array( $stmt, SQLSRV_FETCH_BOTH))
{
$resultArray[] = $row;
echo "<tr>";
echo "<td>" . $row[0] . "</td>";
echo "<td>" . $row[1] . "</td>";
echo "<td>" . $row[2] . "</td>";
echo "<td>" . $row[3] . "</td>";
echo "<td>" . $row[4] . "</td>";
echo "<td>" . $row[5] . "</td>";
echo "<td>" . $row[6] . "</td>";
echo "</tr>";
}
echo "</table>";
I have made an array list so that I can use the variable "row[x]" later in the code.. don't know if this is nessary but..
Second: I now want this rows to be clickable, so if u click on a costumer from the table, you wil now run a new query:
$sql = "SELECT * FROM Products WHERE Costumers = '".$resultArray[0]['']."' ";
$stmt = sqlsrv_query( $conn, $sql);
if( $stmt == false){
die( print_r(sqlsrv_errors(), tue) );
}
echo "<table border='1'>";
echo "<tr><th>CustomerID</th><th>Firstname</th><th>Lastname</th><th>Tlf</th><th>Email</th><th>Productnr</th><th>Address</th></tr>";
while( $row = sqlsrv_fetch_array( $stmt, SQLSRV_FETCH_NUMERIC) )
{
echo "<tr>";
echo "<td>" . $row[0] . "</td>";
echo "<td>" . $row[1] . "</td>";
echo "<td>" . $row[2] . "</td>";
echo "<td>" . $row[3] . "</td>";
echo "</tr>";
}
echo "</table>";
It should now print out all products that contains the same CosuterID as the one I clicked.
I have figured out that I need to use som AJAX, but I just can't get it to work.. Is here somone out there who can help me? I really need this to work!
Thanks :)
Set onClick function on td Here also include Latest Jquery file
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
<?php
echo "<table border='1'>";
echo "<tr><th>CustomerID</th><th>Firstname</th><th>Lastname</th><th>Tlf</th><th>Email</th><th>Productnr</th><th>Address</th></tr>";
while($row = sqlsrv_fetch_array( $stmt, SQLSRV_FETCH_BOTH))
{
$resultArray[] = $row;
echo "<tr>";
echo "<td onClick='lodaproduct(" . $row[0] . ")'>" . $row[0] . "</td>";
echo "<td>" . $row[1] . "</td>";
echo "<td>" . $row[2] . "</td>";
echo "<td>" . $row[3] . "</td>";
echo "<td>" . $row[4] . "</td>";
echo "<td>" . $row[5] . "</td>";
echo "<td>" . $row[6] . "</td>";
echo "</tr>";
}
//Set another row column to show the out put from ajax.php page
echo "<tr><td id='costumersdata'></td></tr>";
echo "</table>";
?>
<script>
function lodaproduct(Costumers){
$.post("ajax.php?Costumers="+Costumers,
function(data){
$("#costumersdata").html(data);
});
}
</script>
On your ajax.php page
<?php
$sql = "SELECT * FROM Products WHERE Costumers = '".$_POST['Costumers']."' ";
$stmt = sqlsrv_query( $conn, $sql);
if( $stmt == false){
die( print_r(sqlsrv_errors(), tue) );
}
echo "<table border='1'>";
echo "<tr><th>CustomerID</th><th>Firstname</th><th>Lastname</th><th>Tlf</th><th>Email</th><th>Productnr</th><th>Address</th></tr>";
while( $row = sqlsrv_fetch_array( $stmt, SQLSRV_FETCH_NUMERIC) )
{
echo "<tr>";
echo "<td>" . $row[0] . "</td>";
echo "<td>" . $row[1] . "</td>";
echo "<td>" . $row[2] . "</td>";
echo "<td>" . $row[3] . "</td>";
echo "</tr>";
}
echo "</table>";
?>