0

Long time viewer, first time asking a question. College student new to PHP so hopefully I do this right.

I have a webpage in PHP which connects to a MYSQL db and reads back the data to the page. As the data is being read in, it populates an array. Each item that is being pulled from the db is set to a button.

Ideally, I only want to fill the array with the item that is clicked. Is it possible what I'm trying to do or is there a better alternative. I have seen that elements such as buttons are client side whereas PHP is server which (I think) indicates that I would have to refresh the page.

Thanks in advance for any help and patience!

<?php
    $servername = "localhost";
    $username = "root";
    $password = "";
    $dbname = "project_db";

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

    $sql = "SELECT ingredient_id, ingredient_name FROM ingredients"; 
    $result = $conn->query($sql);

$array = array(); //used to hold each ingredient as a variable to pass to php script
array_unshift($array,""); 
unset($array[0]); //our first ingredient_id is set to 1 so I've started my array at 1 to allow for that
$_SESSION['arr'] = $array;

if ($result->num_rows > 0) 
{
    // output data of each row
    while($row = $result->fetch_assoc()) 
    {
    echo "<br /><button id='$row[ingredient_id]' name='button' value='button' class='btn-info btn-lg' onClick='return addItem($row[ingredient_id])'>$row[ingredient_name]</button><br />";
                array_push($_SESSION['arr'],$row['ingredient_name']);
    }
} 
else 
{
    echo "0 results";
}

    $conn->close();
?>
4
  • You can create ajax query for transmision data on client to server with out reload page, sorry for my english. Commented Feb 24, 2016 at 12:22
  • What is your question? What is wrong with current code? Commented Feb 24, 2016 at 12:22
  • Thanks Naumov, I will give that a try and let you know. Commented Feb 24, 2016 at 14:37
  • purplepsycho - I am trying to only save certain items to an array so that I can use anything that is in the array across multiple web pages. I still need to read in everything that's in the database though. Commented Feb 24, 2016 at 14:39

1 Answer 1

1

Use javascript to get the data from the DB using an AJAX call. For the button click part, you can use jquery's on.click() function

Sign up to request clarification or add additional context in comments.

4 Comments

Am I correct in saying that I would be able to fill a js array then? Will the other pages of my website be able to see that js array?
If I understand you correctly, then once you get the data in javascript, you can dynamically create html code and add it to a pre-specified <div> in your view or html. This way, every time something new comes as the response, your page will be modified accordingly.
Sorry @brokensax I wasn't clear. I want to use the contents of the javascript array on another page of my website (something equivalent to a session variable in PHP).
Yes, you can. Check this link for more information : stackoverflow.com/questions/21065193/…

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.