简体   繁体   中英

$POST not sending checkbox values, PHP Undefined index error

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

  1. Missing name="coupon" in <input>
  2. Missing {} in IF statement
  3. Missing else part of first IF condition

Improves

  1. Fix the name field
  2. Added {} in IF
  3. Used 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

  1. HTML checked Attribute - W3Schools.com
<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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM