0

I'm having some trouble working out what's going on with my code.

I have a form containing some editable fields. There are also three radio buttons

  • active (db value 1)
  • inactive (db value 2)
  • not found (db value 3)

The editable form opens with the radio button pre-selected based on the database value. For example, if the database value for status = 2, the inactive radio button is selected by default.

Sometimes the form opens and none of the radio buttons are selected.

For example;

  • I open Item 2 for editing (the URL being '/inventory/edit-data.php?edit_id=2').
  • The radio button is set as 'Active', fine.
  • I click 'Inactive' and submit, the form does submit the data and the page refreshes with a 'Success message', fine.
  • I stay on the page, select 'Not Found' and submit again I receive the following error;

Notice: Undefined index: status in C:\xampp\htdocs...\edit-data.php on line 26

I have highlighted line 26 in my code below. I'd appreciate any help or suggestions.

My (minified) code is below;

edit-data.php

if (isset($_POST['btn-update'])) {
    $itemId = $_GET['edit_id'];
    $status = $_POST['status']; // <---- line 26

    if ($crud->update($itemId, $status)) {
        $msg = "Record was updated successfully";
    } else {
        $msg = " While updating record!";
    }
}

<form method='post'>

  <table class='table table-bordered'>

    <tr>
      <td>ID</td>
      <td>
        <input type='text' name='itemId' class='form-control ' value="<?php echo $itemId; ?>" required disabled>
      </td>
    </tr>

    <tr>
      <td>Notes</td>
      <td>
        <input type='text' name='notes' class='form-control' value="<?php echo $notes; ?>" required>
      </td>
    </tr>

    <tr>
      <td>Status</td>
      <td>
          <div class="btn-group" data-toggle="buttons">
            <label class="btn btn-default <?php if ($status == '1' ) echo 'active btn-success' ; ?>">
              <input type="radio" name="status" id="option1" value="1" autocomplete="off"> Active
            </label>
            <label class="btn btn-default <?php if ($status == '2' ) echo 'active btn-warning' ; ?>">
              <input type="radio" name="status" id="option2" value="2" autocomplete="off"> Inactive
            </label>
            <label class="btn btn-default <?php if ($status == '3') echo 'active btn-danger' ; ?>">
              <input type="radio" name="status" id="option3" value="3" autocomplete="off"> Not Found
            </label>
          </div>
      </td>
    </tr>

    <tr>
      <td colspan="2">
        <button type="submit" class="btn btn-primary" name="btn-update">
          <span class="glyphicon glyphicon-edit"></span> Update this Record
        </button>
        <a href="index.php" class="btn btn-large btn-danger"><i class="glyphicon glyphicon-backward"></i> &nbsp; Cancel</a>
      </td>
    </tr>

  </table>
</form>

//The (working) **javascript** to add/remove radio button classes is;
<script>
$(document).ready(function() {
  $('#option1 ,#option2, #option3').change(function() {
    $('#option1').parent().removeClass('btn-success').addClass("btn-default")
    $('#option2').parent().removeClass('btn-warning').addClass("btn-default")
    $('#option3').parent().removeClass('btn-danger').addClass("btn-default");

    if ($(this).attr('id') == 'option1')
      $(this).parent().removeClass("btn-default").addClass("btn-success");
    else if ($(this).attr('id') == 'option2')
      $(this).parent().removeClass("btn-default").addClass("btn-warning");
    else
      $(this).parent().removeClass("btn-default").addClass("btn-danger");
  });
});    
</script>

crud.php

class crud {
    public function update($itemId, $status) {
            try {
                $stmt = $this->db->prepare("UPDATE items SET 
                    itemId=:itemId,
                    status=:status
                 WHERE itemId=:itemId ");
                $stmt->bindparam(":itemId", $itemId);
                $stmt->bindparam(":status", $status);
                $stmt->execute();
                return true;
            } catch (PDOException $e) {
                echo $e->getMessage();
                return false;
            }
        }
}
jonboy
  • 2,729
  • 6
  • 37
  • 77
  • try `$itemId = $_POST['edit_id'];`on line 25 and let me know the result, your form is post, not get – Billy Feb 12 '16 at 16:35
  • Thanks @Billy but I'm getting the `edit_id` from the URL. Your suggestion just produces another 'Notice: Undefined index: edit_id' error – jonboy Feb 12 '16 at 16:38
  • 1
    If you visit the form for the first time(e.g. the form hasn't been submitted), $_post[status'] will be blank. You need to retrieve status from the database and replace $_POST['status'] with whatever value your database retrieval script returns. – ShoeLace1291 Feb 12 '16 at 18:25
  • Thanks @ShoeLace1291 that's exactly what's happening! So do I need to write another query on my edit-item page in order to retrieve the status? I'd be really grateful if you could write an answer. Sounds just like what I need! – jonboy Feb 12 '16 at 18:52

0 Answers0