Undermine2k
Undermine2k

Reputation: 1491

passing variables from views to controllers

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>

Upvotes: 1

Views: 129

Answers (1)

Ja͢ck
Ja͢ck

Reputation: 173542

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']);

Upvotes: 1

Related Questions