1

I'm going to try to explain this as best I can. I'm returning an array of results from my controller. I wish to send over the primary key of the selected row to another controller that will manage inserting the record into the Database when a user clicks the "add employee" button. My primary key is MOVIE_ID. It's currently returning an array of two MOVIE_ID {12341, 31351} like so. My questions are:

  1. How do I associate each movie ID uniquely with that corresponding rows' button?

  2. Can I pass a variable to a controller through the action="" attribute , do I need JS?

enter image description here

            <fieldset id= "results">

                <?php if (isset($results ) And count($results)) : ?>
                    <table id="box-table-a" summary="Employee Sheet">
    <thead>
        <tr>
            <th scope="col">Employee</th>
            <th scope="col">Salary</th>
            <th scope="col">Bonus</th>
            <th scope="col">Supervisor</th>
            <th scope="col">Action</th>
        </tr>


            <?php foreach ($results as $result) : ?>
                            <tr>
//Primary key <?php echo $result['MOVIE_ID!'];?>
<td><span class="Employee_name"><?php echo $result['Employee']; ?> </span></td>         
<td><span class="Salary"><?php echo $result['Salary']; ?> </span> </td>
<td><span class="Bonus"><?php echo $result['Bonus']; ?> </span> </td>
<td><span class="Supervisor_name"><?php echo $result['Supervisor']; ?> </span></td>
<td><input type="submit" value="add employee" > </td>

    </tr>

    <?php endforeach; ?>
        <?php endif; ?>
    </thead>
        <tbody>
    </fieldset>

1 Answer 1

1

One trick is to pass the id via the submit button, like so:

<input name="submit[12341]" type="submit" value="add employee" />

When clicked it would send submit[12341]=add+employee in the POST data (assuming you have wrapped everything inside a <form> tag). The other buttons will not be submitted, only the one that's clicked.

In PHP you can retrieve the id like so:

$id = key($_POST['submit']);
Sign up to request clarification or add additional context in comments.

2 Comments

The problem is, that data can be changed so I can't really hard-code in every ID for the movie it just needs to grab it in the order it was returned from the controller. So I would like to send for row 1, something like "Movie_ID[1]" or something... if you follow me?
@Undermine2k right, so you write ... name="submit[<?php echo $result['MOVIE_ID!'] ?>]" ...

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.