2

Basically, I want to retrieve a certain product after clicking on an element. I'm using AJAX to pass the variable, and PHP to display the SQL query. I will use the product id on a WHERE statement, to retrieve and display the correct product.

Here is part of my code so far:

<html>
<head>
    <title>Left Frame</title>
<link href='http://fonts.googleapis.com/css?family=Indie+Flower' rel='stylesheet' type='text/css'>
<link href="stylesheets/main.css" rel="stylesheet" type="text/css">
<script src="javascripts/jquery-1.11.2.js">
</script>
</head>

<body>
<div class="container">

    <div id="bottomHalf">
        <img id="blank" src="assets/bottom_half.png" style= "z-index: 5" >
        <img src="assets/frosen_food.png"
        usemap="#Map2"
        border="0"
        id="frozen"
        style="z-index: 0;
        visibility:hidden;" >

        <map name="Map2">
            <area shape="rect" coords="7,95,126,146" alt="Hamburger Patties" href="#" id="hamburgerPatties">

     </div>
</div>
<script language="javascript">

  $("#hamburgerPatties").click(function(){
         var hamburgerPatties = "1002";
      $.ajax({
          type:"GET",
          url: "topRightFrame.php",
          data: "variable1=" + encodeURIComponent(hamburgerPatties),
          success: function(){
          //display something if it succeeds
          alert( hamburgerPatties );
      }
      });
  });

</script>
</body>
</html>

Part of my PHP code:

<?php

$product_id =(int)$_GET['variable1'];
$servername = "************";
$username = "************";
$password = "*************";
$dbname = "poti";
$tableName = "products";

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}

$sql = "SELECT * FROM $tableName ";
$result = $conn->query($sql);

// Display all products on the database
if ($result->num_rows > 0) {
    // output data of each row
    while($row = $result->fetch_assoc()) {
        echo "Product ID: " . $row["product_id"]. " - Product Name: " . $row["product_name"]. " Unit Price:" . $row["unit_price"]. "<br>";
    }
} else {
    echo "0 results";
}

    // Display product I clicked on the other frame
    if (isset($GET['variable1'])) {

    $sql = "SELECT * FROM $tableName WHERE product_id = " . $_GET['variable1'];
    $result = $conn->query($sql);

        if ($result) {
        echo '<table>';

            while ($row = $result->fetch_assoc())
            {
            echo '<tr>';
            echo '<td>', "Product ID: " . $row["product_id"]. " - Product Name: " . $row["product_name"]. " Unit Price:" . $row["unit_price"]. "<br>";
            echo '</tr>';
            }
        echo '</table>';
        }
}
$conn->close();
?>

I'm able to display all the products. But starting from the ifsset statement, the code no long works. I get no error message or anything. How can I solve this? I'm pretty new to PHP.

EDIT: Ok, I managed to get the product I want when I hard code the product id. Now I need to get this variable using javascript.

2 Answers 2

1

You have syntax errors in your if statements.

When you're setting if, you should enclose the statement in braces:

instead of if something : do if(something): and then end it with endif; (when using colons :)

// Display product I clicked on the other frame
if (isset($GET['variable1'])):
    $query = "SELECT * FROM $tableName WHERE product_id = " . $_GET['variable1'];
    $result = mysqli_query($conn, $query);
    if ($result):
        echo '<table>';
        while ($row = $result->fetch_assoc()) {
            echo '<tr>';
            echo '<td>', $row['product_id'], '</td>'; // and others
            echo '</tr>';
        }
        echo '</table>';
    endif;
endif;

Or, use {} braces instead of colons :

// Display product I clicked on the other frame
if (isset($GET['variable1'])){
    $query = "SELECT * FROM $tableName WHERE product_id = " . $_GET['variable1'];
    $result = mysqli_query($conn, $query);
    if ($result){
        echo '<table>';
        while ($row = $result->fetch_assoc()) {
            echo '<tr>';
            echo '<td>', $row['product_id'], '</td>'; // and others
            echo '</tr>';
        }
        echo '</table>';
    }
}
Sign up to request clarification or add additional context in comments.

5 Comments

Changed those and I few other things and still no go.
You didn't read my answer carefully. You still have if isset($GET['variable1']) { . You should enclose isset with () braces too, like if (isset($GET['variable1'])) {
Sorry, I didn't notice that. Now the code is running, and it displays all the data, but nothing after clicking the product element.
I don't see any product element in your code. And what is the PHP in your question, is it part of a topRightFrame.php file? Does console says anything?
Updated the code, the product element in this case is the hamburgerPatties. Yep, is the topRIghtFrame.php file. No errors on my console.
0

You have syntax error in if block. use {} for if block also use echo instead of pure html tag(table). This error prevents successful state of ajax request. also don't forget to close table.

if ($result)
{
 echo '<table>';

 while ($row = $result->fetch_assoc()) 
   {
    echo '<tr>';
    echo '<td>', $row['product_id'], '</td><td>'; // and others
    echo '</tr>';

  }
 echo '</table>';
}

and the ajax code to display php result instead of test alert would be this:

<script language="javascript">

  $("#hamburgerPatties").click(function(){
         var hamburgerPatties = "1002";
      $.ajax({
          type:"GET",
          url: "topRightFrame.php",
          data: "variable1=" + encodeURIComponent(hamburgerPatties),
          success: function(data){
          alert(data);
      }
      });
  });

</script>

9 Comments

what happens if you open php page directly when passing variable1 as query string?
I manage to retrieve the product I want when I hard code the ID. Edited the working code. Now I need to be able to retrieve this value using ajax, instead of manually inputting it.
OK! I have updated the answer including jquery to show data instead of fake alert.
When I do that, I get an alert displaying the source code of my PHP page. Is it the output I should expect? If I add echo $product_id, I get the correct product in this alert. But still, nothing happens at the PHP page, it stays static, and the SQL query doesn't run.
what if you convert variable1 to number to make sure there is no data type problem in your sql query. I mean : where productId=".intval($_GET['variable1'])
|

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.