I'm new to PHP and Codeigniter. Trying to submit a form but the values are not being sent to the controller for some reason. If I don't use isset
function then I get a Undefined index error. What am I doing wrong?
HTML
<form action="<?= site_url('Buyer_Controller/view_cart') ?>" method= "post">
<div class="lalign">
<input type="checkbox" /> <span id="normal-p" name="coupon" value="sku_<?php echo html_escape($p_item2['CouponID']) ?>">;
</div>
<button type="submit">Add to Cart</button>
</form>
PHP
public function view_cart()
{
$this->load->model("cart_model", "cartm");
if (!empty($_SESSION['id']))
redirect('Buyer_Controller');
if(!isset($_POST['coupon'])) {
redirect('Buyer_Controller/test');
}
else{
$data= $_POST['coupon'];
}
}
更改为:
<input type="checkbox" id="normal-p" name="coupon" value="sku_<?php echo html_escape($p_item2['CouponID']) ?>">;
Your code is wrong
Errors
- Missing
name="coupon"
in<input>
- Missing
{}
inIF
statement- Missing
else
part of first IF condition
Improves
{}
in IF
empty
instead of isset
<input type="checkbox" /> # No Name attribute
<span id="normal-p" name="coupon" value="sku_<?php echo html_escape($p_item2['CouponID']) ?>">
It should be
In View
<input type="checkbox" id="normal-p" name="coupon" value="sku_<?php echo html_escape($p_item2['CouponID']) ?>">
In Controller
public function view_cart()
{
$this->load->model("cart_model", "cartm");
if (!empty($_SESSION['id'])) # missing {}
{
redirect('Buyer_Controller');
}
else # Missing
{
$post = $this->input->post('coupon');
if(!empty($post)) {
redirect('Buyer_Controller/test');
}
else{
$data = $post
}
}
}
Note : Make sure your site is working without
index.php
References
<input type="checkbox" name="coupon" value="sku_<?php echo html_escape($p_item2['CouponID']) ?>"/> <span id="normal-p" value="sku_<?php echo html_escape($p_item2['CouponID']) ?>">;
像这样改变它..
尝试
<input type="checkbox" name="coupon" value="sku_<?php echo html_escape($p_item2['CouponID']) ?>" /> <span id="normal-p" for="coupon"> <?echo "sku_".html_escape($p_item2['CouponID']);?></span>
Although the other answers and comments are correct - you need to put the name
attribute on your input
- that will not solve your problem completely. You do need to change that though.
The other problem is that unchecked checkboxes are not sent to the server by the browser.
So you always need to use something like isset()
or empty()
when handling checkboxes.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.