javascript – How do I display information from database on button click on a new page?-ThrowExceptions

Exception or error:

I’m looking for a suggestion on how to proceed. I currently have my application connected to an SQL database of tradesmen, and I have successfully been able to show search results on the same page within a table below the submit button. In this table, I have a small amount of information on each tradesmen returned, and at the end of the table row, there is a button with a value of ‘more info’. When clicked, the button takes the user to another page titled ‘info.php’.

I need to somehow have all of the information on that particular tradesmen generated on the info.php page when the user clicks that button. Since I need PHP for database queries and PHP doesn’t seem to have onClick events like JavaScript does, I’m unsure how to make this happen. How do I link the button to the particular tradesman that it is associated with so I can return the correct results? How do I query the database in this case?

My code for my landing page is below. I don’t need to be shown ALL of the code I would need to do this. I really just need to be pointed in the right direction. Thank you!

<body>
        <?php
            ini_set('display_errors', 1);
            ini_set('display_startup_errors', 1);
            error_reporting(E_ALL);
            include("functions.php");
        ?>
        <div class='nav'>
            <ul>
                <li><a href='#about' class='navLink'>About</a></li>
                <li><a href='#login' class='navLink'>Login</a></li>
                <li><a href='#contact' class='navLink'>Contact</a></li>
            </ul>
        </div>
        <div class='wrapper'>
            <h1>Welcome!</h1>
            <div class='form_container'>
                <form action='landing_page.php' method='post'>
                    <input type='text' name='trade' placeholder='What trade do you want to learn?'>
                    <p><button class='btn btn-success' name='submit' type='submit'>Find a mentor</button></p>
                </form>
           </div>
           <?php 
                if(isset($_POST['submit'])) {
                    $trade = $_POST['trade'];
                    $results = query_database($trade);
                    if($results) {
                        $num_results = count($results);
                        print "<table><tr><th>First</th><th>Last</th><th>City</th><th>State</th><th>Available</th></tr>";
                       for($i=0; $i<$num_results; $i++) {
                           for($j=0; $j<5; $j++) {
                              if($j==0) {
                                   print "<td>" . $results[$i][$j] . "</td>";
                               }
                               else if($j==4) {
                                   print "<td>" . $results[$i][$j] . "</td><td><input type='button' value='more info' class='btnClass' onClick='location.href=\"info.php\"'></td></tr>";
                               }
                               else {
                                   print "<td>" . $results[$i][$j] . "</td>";
                               }
                           }
                       }
                        print "</table>";
                    }
                }
            ?>

       </div>
    </body>
How to solve:

When you click your “More Info” button at the end of each row of your grid, you can save the info of the current row salesman into hidden variables on your page which you can transmit to your “info.php” page through the GET or POST methods.

Append the salesman ID to the name of the button for each row to make it unique to each salesman. When you click the relevant row button, save the information you need for the current salesman row into the hidden variables and submit using GET or POST to “info.php” as a form submit.

Example:
If you want to transmit the salesman ID and Name, you can have 2 hidden variables on the page as below.

<input type="hidden" id="saleman_id">
<input type="hidden" id="saleman_name">

For each button at the end of each row for each salesman in the salesman search grid, you can have code like this:

<input type="button" id="<salesman_id>" value="more info" onclick="moreinfo.php?id=XXX&name=YYYY">

You have to have code in moreinfo.php to handle the variables being sent. For the above, the variables are sent using GET but you can also use the POST method if you do not want to show the info being sent.

Leave a Reply

Your email address will not be published. Required fields are marked *