Question

I created a table with search functionality that displays information from my database. I have made so on the last 2 columns of each row has an edit and delete link. The problem is that the when I click edit it won't direct me to the form I created and linked it to, As for the delete link it won't delete at all. I am going to include the code for the view, if you need me to show anymore code just ask and I'll do so.

<table  align="center">
<tr>
    <th>Voter #</th>
    <th>First Name</th>
    <th>Last Name</th>
    <th>Middle</th>
    <th>Home #</th>
    <th>Street</th>
    <th>Apt</th>
    <th>Zip</th>
    <th>DOB</th>
    <th>District</th>
    <th>Edit</th>
    <th>Delete</th>

</tr>
<?php 
foreach($query as $voter){
    $voter_id = $voter['voterNum'];
    ?>
<tr align="center">
    <td><?php echo $voter['voterNum'] ?></td>
    <td><?php echo $voter['firstName'] ?></td>
    <td><?php echo $voter['lastName'] ?></td>
    <td><?php echo $voter['midInitial'] ?></td>
    <td><?php echo $voter['homeNum'] ?></td>
    <td><?php echo $voter['street'] ?></td>
    <td><?php echo $voter['apt'] ?></td>
    <td><?php echo $voter['zip'] ?></td>
    <td><?php echo $voter['dob'] ?></td>
    <td><?php echo $voter['district'] ?></td>
    <td><a href= "<?php echo base_url("reg/edit_voter/");?><?php echo $voter_id ?>">Edit</a></td>
    <td><?php echo anchor('reg/delete_voter'.$voter_id, 'Delete', 
            array('onClick' => "return confirm('Are you sure you want to delete?')"));
    ?>
    </td>
</tr>
<?php
}
?>
</table>

UPDATE Controller

public function search(){
    $search_term = array(
    'firstName' => $this->input->post('firstName'),
    'lastName' => $this->input->post('lastName'),
    'street' => $this->input->post('street'),
    'dob' => $this->input->post('dob')
        );


    $data['query'] = $this->reg_model->search_voters($search_term);


    $this->load->view("reg_header");
    $this->load->view("reg_nav");
    $this->load->view("reg_search", $data); 
    }
    function edit_voter($voter_id) {

    $voter = $this->reg_model->get_voter($voter_id);

    $this->data['title'] = 'Edit Voter';

    $this->load->library("form_validation");

    $this->form_validation->set_rules("firstName", "First Name", "required");
    $this->form_validation->set_rules("lastName", "Last Name", "required");
    $this->form_validation->set_rules("homeNum", "Home Number", "required");
    $this->form_validation->set_rules("street", "Street", "required");
    $this->form_validation->set_rules("zip", "Zip Code", "required");
    $this->form_validation->set_rules("dob", "Date of Birth", 'trim|required|valid_date[d/m/y,/]');
    $this->form_validation->set_rules("district", "District", "required");

    if (isset($_POST) && !empty($_POST))
    {       
        $data = array(
            'firstName' => $this->input->post('firstName'),
            'lastName' => $this->input->post('lastName'),
            'midInitial' => $this->input->post('midInitial'),
            'homeNum' => $this->input->post('homeNum'),
            'street' => $this->input->post('street'),
            'apt' => $this->input->post('apt'),
            'zip' => $this->input->post('zip'),
            'dob' => $this->input->post('dob'),
            'district' => $this->input->post('district')
        );

        if ($this->form_validation->run() === true)
        {
            $this->reg_model->update_voter($voter_id, $data);

            $this->session->set_flashdata('message', "<p>voter updated successfully.</p>");

            redirect(base_url().'reg/search/'.$voter_id);
        }           
    }

    $this->data['message'] = (validation_errors() ? validation_errors() : $this->session->flashdata('message'));

    $this->data['voter'] = $voter;


    $this->data['firstName'] = array(
        'name'      => 'firstName',
        'id'        => 'firstName',
        'type'      => 'text',
        'value'     => $this->form_validation->set_value('firstName', $voter['firstName'])
    );

    $this->data['lastName'] = array(
        'name'      => 'lastName',
        'id'        => 'lastName',
        'type'      => 'text',
        'value'     => $this->form_validation->set_value('lastName', $voter['lastName'])
    );

    $this->data['midInitial'] = array(
        'name'      => 'midInitial',
        'id'        => 'midInitial',
        'type'      => 'text',
        'value'     => $this->form_validation->set_value('midInitial', $voter['firstName'])
        );

    $this->data['homeNum'] = array(
        'name'      => 'homeNum',
        'id'        => 'homeNum',
        'type'      => 'text',
        'value'     => $this->form_validation->set_value('homeNum', $voter['homeNum'])
        );


    $this->data['street'] = array(
        'name'      => 'street',
        'id'        => 'street',
        'type'      => 'text',
        'value'     => $this->form_validation->set_value('street', $voter['street'])
    );

    $this->data['apt'] = array(
        'name'      => 'apt',
        'id'        => 'apt',
        'type'      => 'text',
        'value'     => $this->form_validation->set_value('apt', $voter['apt'])
    );

    $this->data['zip'] = array(
        'name'      => 'zip',
        'id'        => 'zip',
        'type'      => 'text',
        'value'     => $this->form_validation->set_value('zip', $voter['zip'])
    );

    $this->data['dob'] = array(
        'name'      => 'dob',
        'id'        => 'dob',
        'type'      => 'text',
        'value'     => $this->form_validation->set_value('dob', $voter['dob'])
    );

    $this->data['district'] = array(
        'name'      => 'district',
        'id'        => 'district',
        'type'      => 'text',
        'value'     => $this->form_validation->set_value('district', $voter['district'])
    );


    $this->load->view('edit_voter_form', $this->data);
}    

    function delete_voter($voter_id) {
    $this->reg_model->del_voter($voter_id);

    $this->session->set_flashdata('message', '<p>Product were successfully deleted!</p>');

    redirect(base_url('reg/search'));
}
Was it helpful?

Solution

Why mix base_url() and anchor()? Just use anchor().

<td><?php echo anchor("reg/edit_voter/{$voter_id}", 'Edit') ?></td>


<td><?php echo anchor('reg/delete_voter/'.$voter_id, 'Delete', 
            array('onClick' => "return confirm('Are you sure you want to delete?')"));
    ?>
</td>

I think your delete link is missing a trailing slash.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top