skip to Main Content

I have created on table with a PHP while loop right from the phpmyadmin database fine on page 1 (cpu.php). However, I am not able to send just the name variable ( $row[‘name’] ) from one selected row to another page, page 2 (cpumore.php). I have tried using sessions and the closest I have come is the code given that outputs the name of my last listed cpu from the cpu.php page to the cpumore.php page. I hope someone can help me pass variables from a selected row to the other table on page 2 (cpumore.php). Thank you so much!

PAGE 1 CPU.PHP

<table id="myTable">

<thead>
    <tr>
      <th onclick="sortTable(0)">Name</th>
      <th onclick="sortTable(1)">Socket</th>
      <th onclick="sortTable(2)">Speed</th>
      <th>Price</th>
      <th></th>
     </tr>
</thead>

<?php
    session_start();
    if(isset($_POST['search'])) {
        $valueToSearch = $_POST['valueToSearch'];
        // search in all table columns
        // using concat mysql function
        $query = "SELECT name, socket, speed, price FROM `cpu` WHERE 
        CONCAT(`name`, `socket`, `speed`, `price`) LIKE 
        '%".$valueToSearch."%'";
        $search_result = filterTable($query);
    } else {
        $query = "SELECT name, socket, speed, price FROM `cpu`";
        $search_result = filterTable($query);
    }

// function to connect and execute the query
function filterTable($query)
{
    $connect = mysqli_connect("localhost", "root", "", "brodbuilds");
    $filter_Result = mysqli_query($connect, $query);
    return $filter_Result;
}
?>
<?php while($row = mysqli_fetch_array($search_result)):?>
     <tr>
     <form action="cpumore.php" method="post">
        <td><?php echo $row['name']; $_SESSION['cpuName']=$row['name'];?</td>
        <td><?php echo $row['socket'];?></td>
        <td><?php echo $row['speed'];?></td>
        <td><?php echo $row['price'];?></td>
        <td><input type="submit" name=submit id="cpuname" value="Add"/> 
        </div</td>
      </form>
      </tr>
<?php endwhile;?>
</table>

PAGE 2 CPUMORE.PHP

<table id="myTable">
 <thead>
 <tr>
  <th onclick="sortTable(0)">Name</th>
  <th></th>
 </tr>
</thead>
                <tr>
                    <td> <?php session_start(); echo $_SESSION['cpuName'];?> </td>
                </tr>
</table>

I would like to be able to click on a button that send the name field from one row to another page.

2

Answers


  1. You missed ‘>’ Try now

    <td><?php echo $row['name']; $_SESSION['cpuName']=$row['name'];?></td>
    
    Login or Signup to reply.
  2. I’m not too sure if I get the question, the best is to have a session manager, or alternatively on more add a variable as page?varname=value on page to use:

    $Var = $_Get['varname'];
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search