Skip to content
Advertisement

PHP MVC not sending value to controller

I have been making a MVC site, I am having trouble sending the row id from my form to my controller.

The code that I am using for my form gets the row ID for each db entry and assigns it to a hidden value. When the form is submitted it sends the parameters to the controller (should send $uid) but the uid isn’t making it to the controller.

Form Code (buttons.php)

<?php
    $itemsDAO = new ItemsDAO();
    $result = $itemsDAO->getItems();
    foreach ($result as $row) {
        $uid = $row['id'];
?>
    <form action="index.php" method="post">
    <fieldset>
    <input id='action' type='hidden' name='action' value='deleteItem' />
    <p>
    <div class="form-group">
        <div class="controls">
            <input type="hidden" id="fId" name="fId" value="<?php echo  $uid; ?>">
            <input type="submit" class="btn btn-success" value="Delete">
        </div>
    </div>
    </p>
    </fieldset>
    </form>
<?php } ?>

Controller function

function deleteItem($parameters) {
    $id=$parameters["fId"];

    if ($this->model->deleteItem( $id )) {
        $this->model->hasDeleteFailed = false;
        $this->model->setDeleteItemConfirmation();
        return (true);
    }
    else
        $this->model->deleteItemError ( DELETE_ITEM_ERROR_STR );
}

View.php – where I am showing the list of db items and the buttons.php

$this->model->prepareItemList();
$buttons = file_get_contents( "templates/buttons.php");
$HTMLItemList = "";
foreach ( $this->model->itemList as $row ) 
        $HTMLItemList .= "<li><strong>" . $row ["title"] . ": </strong>" . $row ["price"] . "<blockquote>" .$row ["description"] .  " " . $buttons ."</blockquote></li>";

Advertisement

Answer

Okay,

  1. step by step the $uid has to first make it into the form elements value attribute. Check the html source code to make sure this is actually happening.

  2. place var_dump($_POST) exit; in your controller to find what is actually being recieved if anything at all.

  3. check to make sure your result array element actual has a value and not an empty string value or NULL.

Hmm S.O. code formatting bad.

   // turn on error reporting for dev to view empty or missing  variable errors
   ini_set('error_reporting', E_ALL);
   $result=$itemsDAO->getItems();
   foreach ($result as $row) {
       ($row['id'] != ''? $uid = $row['id'] : $uid ='no id found');
       // debug result
       echo '<pre>' . print_r($row,1) .'</pre>';
   }
User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement