Не может получить значение из представления на кодеигниторе

Я работал над этим php-приложением codeigniter для своего класса, и у меня есть представление, где вы можете выбрать количество монет, и выбранное количество вернется к нулю.

мой контроллер:

public function add(){

$this->form_validation->set_rules('quantity', 'Quantity', 'required');

if(!$this->session->userdata('logged_in')){
redirect('users/login');
}

$inventorycoin = $this->uri->segment('3');
$inventoryquantity = $this->input->post('quantity');

$this->coin_model->add_coin($inventorycoin, $inventoryquantity);
//$this->session->set_flashdata('post_deleted', 'Post has been deleted');
redirect('coins');
}

моя модель:

public function add_coin($inventorycoin, $inventoryquantity){

$data = array (
'inventory_user' =>  $this->session->userdata('user_id'),
'inventory_quantity' => $inventoryquantity,
'inventory_coin' => $inventorycoin
);

return $this->db->insert('inventory', $data);
}

мой взгляд:

<?php echo form_open_multipart('coins/add', 'id="coins"'); ?>
<table width="100%">

<?php foreach($coins as $coin) : ?>
<tr>
<td><img  class="post-thumbnail" style="width: 200px; height: 200px" src="<?php echo site_url(); ?>assets/images/coins/<?php echo $coin['coin_image']; ?>"></td>
<td><?php echo $coin['coin_name']; ?></td>
<td><?php echo $coin['coin_country']; ?></td>
<td><?php echo $coin['coin_weight']; ?></td>
<td><input type="number" min="0" name="quantity" placeholder="Qt" style="width: 40px"></td>
<td><p><a class="btn btn-primary" href="<?php echo base_url(); ?>coins/add/<?php echo $coin['coin_id']; ?>">Add</a></p></td>
</tr>
<?php endforeach; ?>

</table>
</form>

В чем может быть проблема?

0

Решение

Вы не используете кнопку отправки. Так что эта часть не будет работать.

 $inventoryquantity = $this->input->post('quantity');

Вы зацикливаете input Так что попытайтесь изменить свою форму на это.

        <?php echo form_open_multipart('coins/add/', 'id="coins"'); ?>
<table width="100%">

<?php foreach($coins as $coin) : ?>
<tr>
<input name="inventorycoin[]" type="hidden" value = "<?=$coin['coin_id'];?>" >
<td><img  class="post-thumbnail" style="width: 200px; height: 200px" src="<?php echo site_url(); ?>assets/images/coins/<?php echo $coin['coin_image']; ?>"></td>
<td><?php echo $coin['coin_name']; ?></td>
<td><?php echo $coin['coin_country']; ?></td>
<td><?php echo $coin['coin_weight']; ?></td>
<td><input type="number" min="0" name="quantity[]" placeholder="Qt" style="width: 40px"></td>
<td>--</td>
</tr>
<?php endforeach; ?>

</table>
<button type="submit"> Add </button>
</form>

и ваша функция к этому

public function add(){

$this->form_validation->set_rules('quantity', 'Quantity', 'required');

if(!$this->session->userdata('logged_in')){
redirect('users/login');
}
$inventorycoin = $this->input->post('inventorycoin');

$inventoryquantity = $this->input->post('quantity');

echo "<pre>";print_r($inventorycoin);
echo "<pre>";print_r($inventoryquantity); // you need to loop this because it is an array

redirect('coins');
}
0

Другие решения

Вы можете изменить нижнюю строку в представлении:

<td><p><a class="btn btn-primary" href="<?php echo base_url(); ?>coins/add/<?php echo $coin['coin_id']; ?>">Add</a></p></td>

В это и попробуйте.

<td><p><a type="submit" class="btn btn-primary" href="<?php echo base_url('coins/add/'.$coin['coin_id']); ?>">Add</a></p></td>
0

По вопросам рекламы ammmcru@yandex.ru
Adblock
detector