I am building a Codeigniter shopping cart. On the cart details page I have a form input field allowing the user to type in the quantity required of a product, and a submit button to post the information to the update function.
When there is just one item in the cart, when updating the quantity everything works as it should. However, when there is more than one item, changing the quantity of an item and clicking submit results in a ‘Undefined Offset 1: error on the following code in the Model (specifically the two lines within the array) :
function validate_update_cart()
{
$total = $this->cart->total_items();
$item = $this->input->post('rowid');
$qty = $this->input->post('qty');
for($i=0;$i < $total;$i++)
{
$data = array(
'rowid' => $item[$i],
'qty' => $qty[$i]
);
$this->cart->update($data);
}
}
This is the View code to which the above refers:
<form action="<?php echo base_url(); ?>home/update" method="post">
<div><input type="hidden" name="rowid[]" value="<?php echo $item['rowid']; ?>"/></div>
<div><input type="text" name="qty[]" value="<?php echo $item['qty']; ?>" maxlength="2" class="chg-qty"/></div>
<div><input type="submit" value="update" class="update-quantity"/></div>
</form>
And this is the Controller:
function update()
{
$this->products_model->validate_update_cart();
redirect('cart');
}
Please can anyone explain why this is happening?
Many thanks,
Matt
instead of
for($i=0;$i < $total;$i++)
use this
for($i=0;$i < count($item);$i++)
I had the same issue; I'm pretty sure the issue is in the shopping cart view
section. The hidden field isn't inside the foreach{} statement - that's why
you can edit the quantity when you have one product in your shopping cart, but
when you add another product can't edit the product. Here's a chunk of code
that worked for me.
<?php if ($this->cart->total_items()!=0) :?>
<div id="cart">
<?php if ($cart=$this->cart->contents()) :?>
<table>
<caption>Shopping Cart</caption>
<thead>
<th>Item Name</th>
<th>Option</th>
<th>Price</th>
<th>Qty</th>
<th></th>
</thead>
<?php foreach ($cart as $item): ?>
<tr>
<td><?=$item['name'];?></td>
<td>
<?php
if ($this->cart->has_options($item['rowid'])) {
foreach ($this->cart->product_options($item['rowid']) as $option => $value) {
echo $option.": <em> ".$value." </em>";
};
};
?>
</td>
<td>$<?=$item['subtotal'];?></td>
<?=form_open('index.php/shop/update_cart'); ?>
<td>
<?=form_input(array('name' => 'qty[]', 'value' => $item['qty'], 'maxlength' => '2', 'size' => '2')); ?>
</td>
<td class="remove"><?=anchor('index.php/shop/delete/'.$item['rowid'],'X','class="remove"');?></td>
<td> <?=form_hidden('rowid[]', $item['rowid']); ?></td>
</tr>
<?php endforeach;?>
<tr class="total">
<td colspan="2"> <strong>Total</strong> </td>
<td>$<?=$this->cart->total();?></td>
</tr>
<tr>
<td><?php echo form_submit('submit', 'Update your Cart'); ?></td>
<!-- When you want to empty your cart using ajax, add 'class="empty"' as a third parameter. -->
<td><?=anchor('index.php/shop/empty_cart', 'Empty Cart', 'class="empty"');?></td>
<?=form_close();?>
</tr>
</table>
<?php endif;?>
</div>
<?php endif;?>
I believe that your problem is that you need to have
<form action="<?php echo base_url(); ?>home/update" method="post">
<div><input type="hidden" name="rowid[]" value="<?php echo $item['rowid']; ?>"/></div>
<div><input type="hidden" name="rowid[]" value="<?php echo $item['rowid']; ?>"/></div>
<div><input type="text" name="qty[]" value="<?php echo $item['qty']; ?>" maxlength="2" class="chg-qty"/></div>
<div><input type="text" name="qty[]" value="<?php echo $item['qty']; ?>" maxlength="2" class="chg-qty"/></div>
<div><input type="submit" value="update" class="update-quantity"/></div>
</form>
Namely, the 2 entries for the rowid and qty.
This link provides examples of using both standard and associative arrays with HTML inputs.
EDIT based on OP feedback:
This was the example I was referring too:
<label><input type="checkbox" name="choice[]" value="1"/> 1</label>
<label><input type="checkbox" name="choice[]" value="2"/> 2</label>
<!-- etc... -->
// meanwhile, on the server...
$choice = $this->input->post('choice');
print_r($choice); // Array ( [0] => 1 [1] => 2 );
Another example:
<form method="post" action="">
<input maxlength="30" name="friend[]" size="30" type="text" />
<input maxlength="30" name="friend[]" size="30" type="text" />
<input maxlength="30" name="friend[]" size="30" type="text" />
<input type="submit" value="Submit" />
</form>
// ***** Server-Side PHP: *****
// Loop through the friend array
foreach ($_POST['friend'] as $value) {
if ($value) { echo $value."<br />"; }
}
Notice where the examples are using an input with the same "blah[]" for each value they expect to come back in the array. In your code, you have one rowid[] and one qty[] input in your view. For a single element this will work b/c you have one element defined in the array. when you have 2 items and you apparently are updating the total items variable to represent the correct number of items but then loop through trying to access the second element (i.e. 1) in each array which does not exist which is why you're getting "Undefined Offset 1" error.
Related
Here i am facing a problem with the while loop.. (i think) unable to store my all retrieve the data from Mysql table to my html form.
the first row is only getting posted and the rest of the rows are not posting due to same name element getting repeated from the while loop,
Here the $_POST['mechanic_name']; one time i'm using.. any problem with this..
because this is not in a while loop, ore if you think any other problems with the code below pls advice
<?php
include("db_conection.php");
$view_users_query="select * from mechanic";//select query for viewing users.
$run=mysqli_query($dbcon,$view_users_query);//here run the sql query.
while($row=mysqli_fetch_array($run))//while look to fetch the result and store in a array $row.
{
$mechanic_ID=$row[0];
$mechanic_name=$row[1];
?>
<tr>
<td>
<input name="mechanic_ID" type="text" value="<?php echo $mechanic_ID; ?>">
</td>
<td>
<input name="mechanic_name" type="text" value="<?php echo $mechanic_name; ?>">
</td>
</tr>
<!--* For save php script*-->
<?php
include("db_conection.php");//make connection here
if(isset($_POST['register']))
{
$mechanic_ID=$_POST['mechanic_ID'];//here getting result from the post array after submitting the form.
$mechanic_name=$_POST['mechanic_name'];//same
$month=$_POST['month'];//same
if($mechanic_name=='')
{
//javascript use for input checking
echo"<script>alert('Please enter the name')</script>";
exit();//this use if first is not work then other will not show
}
//insert the user into the database.
$insert_schedule="insert into schedule (mechanic_ID,mechanic_Name,) VALUE ('$mechanic_ID','$mechanic_name'[enter image description here][1])";
if(mysqli_query($dbcon,$insert_schedule))
{
echo"<script>window.open('index.html','_self')</script>";
}
}
plz help me...!`
<div>
<form role="form" method="post" action="schedule.php">
<table class="table table table-inverse table-hover">
<fieldset>
<div>
<input class="form-control" placeholder="Username" name="month" type="Month" value="January">
</div>
<thead class="thead-inverse">
<tr>
<th>Mechanic Id</th>
<th>Mechanic Name</th>
<th>Woking Day Selection</th>
<th>Delete User</th>
</tr>
</thead>
<?php
include("db_conection.php");
$view_users_query="select * from mechanic";//select query for viewing users.
$run=mysqli_query($dbcon,$view_users_query);//here run the sql query.
while($row=mysqli_fetch_array($run))//while look to fetch the result and store in a array $row.
{
$mechanic_ID=$row[0];
$mechanic_name=$row[1];
?>
<tr>
<td>
<input name="mechanic_ID" type="text" value="<?php echo $mechanic_ID; ?>">
</td>
<td>
<input name="mechanic_name" type="text" value="<?php echo $mechanic_name; ?>">
</td>
<td>
<div class="weekDays-selector">
<input type="checkbox" name="Sun" id="weekday-sun" class="weekday" />
<label for="weekday-sun">S</label>
<input type="checkbox" name="Mon" id="weekday-mon" class="weekday" />
<label for="weekday-mon">M</label>
<input type="checkbox" name="Tue" id="weekday-tue" class="weekday" />
<label for="weekday-tue">T</label>
<input type="checkbox" name="Wed" id="weekday-wed" class="weekday" />
<label for="weekday-wed">W</label>
<input type="checkbox" name="Thu" id="weekday-thu" class="weekday" />
<label for="weekday-thu">T</label>
<input type="checkbox" name="Fri" id="weekday-fri" class="weekday" />
<label for="weekday-fri">F</label>
<input type="checkbox" name="Sat" id="weekday-sat" class="weekday" />
<label for="weekday-sat">S</label>
</div>
</td>
<td>
<!--button-->
<input class="btn btn-lg btn-success btn-block" type="submit" value="register" name="register" >
</td>
</tr>
</fieldset>
<?php } ?>
</table>
</form>
</div>
<?php
include("db_conection.php");//make connection here
if(isset($_POST['register']))
{
$mechanic_ID=$_POST['mechanic_ID'];//here getting result from the post array after submitting the form.
$mechanic_name=$_POST['mechanic_name'];//same
$month=$_POST['month'];//same
$Sun=$_POST['Sun'];//same
$Mon=$_POST['Mon'];//same
$Tue=$_POST['Tue'];//same
$Wed=$_POST['Wed'];//same
$Thu=$_POST['Thu'];//same
$Fri=$_POST['Fri'];//same
$Sat=$_POST['Sat'];//same
if($mechanic_name=='')
{
//javascript use for input checking
echo"<script>alert('Please enter the name')</script>";
exit();//this use if first is not work then other will not show
}
//insert the user into the database.
$insert_schedule="insert into schedule (mechanic_ID,mechanic_Name,month,Sun,Mon,Tue,Wed,Thu,Fri,Sat) VALUE ('$mechanic_ID','$mechanic_name','$month','$Sun','$Mon','$Tue','$Wed','$Thu','$Fri','$Sat')";
if(mysqli_query($dbcon,$insert_schedule))
{
echo"<script>window.open('index.html','_self')</script>";
}
}
?>
ok
your text input have same static name
you shuld give it an array name if you want to save it like :
<input type="text" name="mechanic_ID[]" value="first_one" />
<input type="text" name="mechanic_ID[]" value="second_one" />
and when get data
$mechanic_ids = $_POST['mechanic_ID'];
// this give you data in array ('first_one','second_one')
can resume
foreach($mechanic_ids as $mechanic_id){
// do some thing in this one
}
here good example about arrays form html and php
here is the things is mechanic table whole data (mechsanic_ID , Mechanic_name) able to retrieve, it's showing all the data; because of while loop, But when register it the very first row only able to posted and the rest of the rows are not postinginterface look like this.
I have a form page and when I submit this form all inputs are posting successfully except this one:
<input id="TC" class="form-control" name="kullanici_id" type="text" onchange="edit()"
<?php if($this->data['kullanici_id']){echo 'readonly';} ?>
value="<?php echo $this->data['kullanici_id']?>">
But why?
-This is my .phtml file:
<html>
<head>
</head>
<body>
<form enctype="multipart/form-data" action="/admin/kaydet" method="post" onSubmit="javascript: beforeSubmit();">
<?php if(strlen($this->data['id'])):?>
<input type="hidden" name="id" value="<?php echo $this->data['id']?>">
<?php endif;?>
<font color="green"><h3>DÜZENLE</h3></font>
<img src="/foto/<?php echo $this->data['fotograf']?>" height="110" width="110" align="left" />
<table class="table">
<tr>
<td>T.C. Kimlik No.:</td>
<td><input id="TC" class="form-control" name="kullanici_id" type="text" onchange="edit()" <?php if($this->data['kullanici_id']){echo 'readonly';} ?> value="<?php echo $this->data['kullanici_id']?>"></td>
</tr>
<?php if(!strlen($this->data['id'])):?>
<tr>
<td>Parola:</td>
<td><input id="password2" class="form-control" type="password" name="parola" value="<?php echo $this->data['parola']?>"></td>
</tr>
<tr>
<td>Parola Tekrar:</td>
<td><input onchange="passwordCheck(this.value)" class="form-control" type="password" name="parola" value="<?php echo $this->data['parola']?>"></td>
</tr>
<?php endif; ?>
</table>
<td><button type="submit" class="btn btn-success btn-sm glyphicon glyphicon-floppy-disk">KAYDET</button> </td>
</form>
</body>
</html>
If I have an id; page looks like an edit member page, if I haven't; page will add a new member. In id="TC" input, if I edit a member, this input shouldn't change, so I add a readonly to solve this. But when I submit, input does not post.
Sorry about my bad English :D
Reason your field is not being submitted it is because its set to readonly.
Reason for this is 'If user cannot change the field there is no point of submitting it as value will always remain the same'.
One way to mitigate this behavior is to add hidden field with same name
<input type="hidden" name="kullanici_id" value="<?php echo $this->data['kullanici_id']?>"> />
<input id="TC" class="form-control" name="kullanici_id" type="text" onchange="edit()" <?php if($this->data['kullanici_id']){echo 'readonly';} ?> value="<?php echo $this->data['kullanici_id']?>" />
I need to make a task where it compares an array value with an inputted text value.
For the array, the code is this:
<div class="wpsc-quantity-discounts">
<table>
<thead>
<tr>
<th class="qty" colspan="2">Quantity:</th>
<th class="curr"><span class="hidden">Currency:<span></th>
<th class="price">Price:</th>
</tr>
</thead>
<tbody>
<tr>
<td class="remove"></td>
<td class="qty">
<input type="text" size="5" value="500"/*this value*/ name="table_rate_price[quantity][]" />
+ </td>
<td class="curr">USD $</td>
<td><input class="newCurrPrice text" value="0.48" name="table_rate_price[table_price][]" /></td>
It already contains the variables I need. I need to compare the first column with an inputted text from my single product page.
<?php if(wpsc_has_multi_adding()): ?>
<fieldset><legend style="float:left;"><?php _e('Quantity', 'wpsc'); ?>: </legend>
<div class="wpsc_quantity_update">
<input type="text" id="wpsc_quantity_update_<?php echo wpsc_the_product_id(); ?>" name="wpsc_quantity_update" size="2" value="500"/*This value*/ />
<input type="hidden" name="key" value="<?php echo wpsc_the_cart_item_key(); ?>"/>
<input type="hidden" name="wpsc_update_quantity" value="true" />
</div><!--close wpsc_quantity_update-->
</fieldset>
I need to make an if statement where if the inputted text is less than the first column's array, it will return false. If anybody has a suggestion that'd be awesome. I'm still kind of a noob in php so be easy :p. Thanks.
This is the code that I have now.
<fieldset><legend style="float:left;"><?php _e('Quantity', 'wpsc'); ?>: </legend>
<div class="wpsc_quantity_update">
<input type="text" id="wpsc_quantity_update_<?php echo wpsc_the_product_id(); ?>" name="wpsc_quantity_update" size="2" value="500" />
<input type="hidden" name="key" value="<?php echo wpsc_the_cart_item_key(); ?>"/>
</div><!--close wpsc_quantity_update-->
<php? if ( table_rate_price[1][0] > wpsc_quantity_update(value)): />
<return <input type="hidden" name="wpsc_update_quantity" value="false" />
<?php else: ?>\
return <input type="hidden" name="wpsc_update_quantity" value="true" />;
</fieldset>
Check documentation for in_array I believe that's what you are looking for.
Here is a snippet to get you started:
<?php
$os = array("Mac", "NT", "Irix", "Linux");
if (in_array("Irix", $os)) {
echo "Got Irix";
}
if (in_array("mac", $os)) {
echo "Got mac";
}
?>
Looks like table_rate_price[1][] should be table_rate_price[1][0]? Can't tell without seeing how that array looks, do a print_r($table_rate_price) and show us the results.
I would cast value to an int or float before the comparison since all post data will be String. I'm assuming wpsc_quantity_update() returns the $_POST['wpsc_quantity_update'] value.
floatval() * decimals like 0.48, 3.14, ...
intval * whole numbers 1, 2, 3 ...
if ( table_rate_price[1][0] < floatval(wpsc_quantity_update())) {
return false;
}
return true;
Okay, so I have a PHP array pulling from a mysql table. The array is generated based on the items in a table where items are frequently added and deleted. I have a button next to the item name, "Submit." I want the button to identify with the item that is in the same index. It will then pass the item submitted item to a new table.
<form class="omb_loginForm" action="inc/contribute_item.php" autocomplete="off" method="POST">
<?php
$item_array;
$index = 0;
$index_2 = 1;
$r = "r";
$b="b";
foreach ($item_array as $id_array){ ?>
<tr id="<?php echo $r.$index_2; ?>">
<td><?php echo $item_array[$index] ?></td>
<td> <?php echo $quantity_array[$index]; ?></td>
<td> <?php echo $price_array[$index];
$selectedItem = $item_array[$index]; ?>
<input type='hidden' name='hidden' value='<?php $selectedItem ?>'>
<input type='submit' name='submit' value"submit">
</form> </td>
<?php $index++;
$index_2++; ?>
</tr>
Here is the PHP:
if ($_POST['submit']) {
$connect = new mysqli(DB_HOST, DB_USER, DB_PASS, DB_NAME);
$user_contrib = $_SESSION['first_name'];
$selected = $selectedItem;
$connect->query("UPDATE `items` SET `purchased_by` = '$user_contrib' WHERE `name` = '$selected'");
}
You're on the right track, just make sure that your opening and closing html tags are properly aligned.
Using one form per row
If you want to transmit the selected value via an hidden input, make sure, that each of these inputs is inside its own form together with the corresponding submit button:
<!-- row 1: -->
<form action="inc/contribute_item.php" method="post">
<input type="hidden" name="myValue" value="1"/>
<input type="submit" value="submit"/>
</form>
<!-- row 2: -->
<form action="inc/contribute_item.php">
<input type="hidden" name="myValue" value="2"/>
<input type="submit" value="submit"/>
</form>
Then in PHP access the selected value by using $_POST['myValue'].
Don't nest form tags. And don't put form tags between your table, tr, td tags. Close them in the same order you open them.
To be more specific, this is how your loop could look like:
<!-- don't start your form here -->
<table>
<?php foreach(...) { ?>
<tr>
<td>...</td>
<td>
<form action="inc/contribute_item.php" method="post">
<input type="hidden" name="myValue" value="<?= $index ?>"/>
<input type="submit" value="submit"/>
</form>
</td>
</tr>
<?php } ?>
</table>
Using radio buttons or check boxes
Yet another option would be to use <input type="radio" ... /> elements in each row. This way you could use just one global form:
<form action="inc/contribute_item.php" method="post">
<table>
<?php foreach(...) { ?>
<tr>
<td>...</td>
<td>
<input type="radio" name="myValue" value="<?= $index ?>"/>
</td>
</tr>
<?php } ?>
</table>
<input type="submit" value="submit"/>
</form>
I have a page with some radio buttons and form. When I select one radio button I need its id to be set to $_POST['id'] varible so when I submit the form I can check which radio button was selected and act accordingly. I know I can't do it directly so tried using
onclick="document.getElementById("id").value="'.$row['id'].'"
on radio button but it didn't work.
$d="select * from {$_c['dbprefix']}card where active='1' order by s_sent desc, s_order asc limit 0,{$_c['filtr']['step']}";
$r=mysql_query($d);
if (mysql_num_rows($r)>0) {
echo '<div class="cards">';
echo '<form method="POST">';
$i=0;
while ($row=mysql_fetch_array($r)):
echo '<div class="item">';
echo '<input id="'.$row['id'].'" type="radio" name="id" class="detail">'.$_l['detailtitle'].'</input>';
echo '<label for="'.$row['id'].'" class="itemin" style="height:'.$_c['card']['sy'].'px"><img src="pub/cards/'.$row['id'].'s.jpg" /></lable>';
echo '</div>';
$i++;
if ($i%3==0) echo '<div class="cl-left"></div>';
endwhile;
echo '<div class="cl-left"></div>';
echo '</div>';
}
?>
<div class="dvasl">
<div class="sl1">
<fieldset>
<legend>Saatja andmed</legend>
<table class="info">
<colgroup><col width="12%"><col></colgroup>
<tr>
<th>Nimi:</th>
<td><input type="text" name="item[from_name]" value="<?php echo $ap['set']['from_name']; ?>" class="inpedit"></td>
</tr>
<tr>
<th>Email:</th>
<td><input type="text" name="item[from_email]" value="<?php echo $ap['set']['from_email']; ?>" class="inpedit"></td>
</tr>
</table>
</fieldset>
<fieldset>
<legend>Saaja andmed</legend>
<table class="info">
<colgroup><col width="12%"><col></colgroup>
<tr>
<th>Nimi:</th>
<td><input type="text" name="item[to_name]" value="<?php echo $ap['set']['to_name']; ?>" class="inpedit"></td>
</tr>
<tr>
<th>Email:</th>
<td><input type="text" name="item[to_email]" value="<?php echo $ap['set']['to_email']; ?>" class="inpedit"></td>
</tr>
</table>
</fieldset>
</div>
<div class="sl2">
<fieldset>
<legend>E-kaardi tekst</legend>
<table class="info">
<tr>
<td><textarea name="item[text]" cols="20" rows="8" class="inpedit2"></textarea></td>
</tr>
</table>
</fieldset>
</div>
<div class="cl-left"></div>
</div>
<div class="buttons">
<div class="t2"><input type="submit" value="Vaata kaardi eelvaadet" name="button[nahled]" class="unbutt1"></div>
</div>
<input type="hidden" name="id" value="<?php echo $ap['card']['id']; ?>">
<input type="hidden" name="action" value="itemadd">
</form>
<?php
$k=floor(($i-1)/3);
if ($k<2) echo '<div class="spacer-'.(2-$k).'"></div>';
?>
Its better if you can use a hidden field in the form and update its value with the id of the selected radio button and when the form will be posted you can get its value and more over this after once you changed the value of the hidden field you can access it with javascript also with id attibute and can get it in the post by name attribute
add a hidden field named
<input type="hidden" id="checked_radio" name="checked_radio" value="" />
and then change the value with javascript on click of button
<input type="radio" id="foo" name="foo" onclick="document.getElementById('checked_radio').value=this.value;" />
The value of the selected radio button gets posted, and the name is used to group radio buttons together. (Only one radio button with the specified name can be selected.) Instead of trying to post the value of the id attribute you should add a value attribute and set that. You could set it to the same value as the id attribute, or remove the id attribute completely.
When I select one radio button I need its id to be set to $_POST['id'] variable
You are changing the value, not the id, to change the id use:
onclick="document.getElementById("id").id="<?php echo $row['id'] ?>";
Anyway, I think you should use the name attribute for this.
change this line
echo '<input id="'.$row['id'].'" type="radio" name="id" class="detail">'.$_l['detailtitle'].'</input>';
to
echo '<input id="'.$row['id'].'" type="radio" value="'.$row['id'].'" name="id" class="detail">'.$_l['detailtitle'].'</input>';
when your form submit,you will get the selected id as $_POST['id']