hope you are all doing well. I'm not too sure on how I worded the title so I'm sorry for that.
I have this code:
<form method="POST" action="checkoutManager.php" name="submitOrder">
<?php
if(isset($_SESSION["cart_item"])) {
$item_total = 0;
$total;
foreach ($_SESSION["cart_item"] as $item) {
$item_total += (($item["price"]-$item["discount"])*$item["quantity"]);
$total = $total + $item_total;
?>
<tr>
<td class="product-name">
<?php echo $item["name"]; ?> <strong class="product-qty"> × <?php echo $item["quantity"]; ?></strong>
</td>
<td class="product-total">
<span class="amount"><?php echo "$".$item_total; ?></span>
</td>
</tr>
<?php
$item_total = 0;
}
}
?>
<input type="submit" name="btnSubmitOrder" value="Submit Order">
</form>
How would I go about submitting all the $_SESSION items in using form tag. I have tried submitting as an array but failed. All help appreciated. Thanks in advance
why don't you create hidden elements like
<input type="hidden" name="total" value="<?php echo $total; ?>">
and anything you need use hidden input field and you can access them after the submit button is clicked by using $_POST in your checkoutManager.php
UPDATE
simply you can process all the fields in checkoutManager.php using session, you dont need any form to be submitted. create a link to checkoutManager.php and do all the calculations there
Try below code.
In this code I have added one hideen count field.
By using count u can use for loop after submit.
I display only one input for name but u can add multiple inputs inside for loop of form.
I also concated $i variables, so all input names are different as name1, name2,... U can use it on submit as $_POST['name'.$i] after submit.
<form method="POST" action="checkoutManager.php" name="submitOrder">
<?php
<input type="hidden" name="count" value="<?php echo count($_SESSION["cart_item"]); ?>">
$i=0;
foreach ($_SESSION["cart_item"] as $item)
{
?>
<input type="text" name="input_name<?php echo $i; ?>" value="<?php echo $item['name']; ?>" />
<?php
$i++;
}
?>
<input type="submit" name="btnSubmitOrder" value="Submit Order">
</form>
You need to add input fields in foreach loop for all your items that are coming in $_SESSION["cart_item"]. After submission, you need to use foreach again for inserting all your items in your temp table one by one.
Update your code like below:
<form method="POST" action="checkoutManager.php" name="submitOrder">
<?php
if(isset($_SESSION["cart_item"])) {
$item_total = 0;
$total = 0;
foreach ($_SESSION["cart_item"] as $item) {
$item_total +=
(($item["price"]-$item["discount"])*$item["quantity"]);
$total = $total + $item_total;
?>
<input type="hidden" name="price[]" value="<?php echo $item["price"]; ?>">
<input type="hidden" name="name[]" value="<?php echo $item["name"]; ?>">
<input type="hidden" name="discount[]" value="<?php echo
$item["discount"]; ?>">
<input type="hidden" name="quantity[]" value="<?php echo
$item["quantity"]; ?>">
<tr>
<td class="product-name">
<?php echo $item["name"]; ?> <strong class="product-qty"> × <?php
echo $item["quantity"]; ?></strong>
</td>
<td class="product-total">
<span class="amount"><?php echo "$".$item_total; ?></span>
</td>
</tr>
<?php
$item_total = 0;
}
}
?>
<input type="submit" name="btnSubmitOrder" value="Submit Order">
</form>
And you can access all your input fields after submission in the below way.
foreach($_POST['name'] as $key => $val){
echo $_POST['name'][$key];
echo $_POST['price'][$key];
echo $_POST['discount'][$key];
echo $_POST['quantity'][$key];
}
Hope it helps!
Related
I am trying to update quantity as i fill the quantity field and click on update button. It works but it shows the form fields of each item in the loop and works for the last entry update only. Something like this.
/EcommerceClient/index.php?page=cart&action=update&id=3&name=%09%0D%0ACool+T-shirt&color=blue&size=XL&quantity=4&id=4&name=HBD+T-Shirt&color=yellow&size=XL&quantity=900
In the above link it should only get the information associated with id=3 only because i tried to update the quantity of id=3.
Here is my code. Any suggestions or help will be highly appreciated.
Code
if(isset($_GET['action']) && $_GET['action']=="update"){
$id= intval($_GET['id']);
$size = $_GET['size'];
$color = $_GET['color'];
$qty = $_GET['quantity'];
$index = $id." ".$color. " ".$size;
if( isset($_SESSION['cart'][$index]) && isset($_SESSION['cart'][$index]['color']) && $_SESSION['cart'][$index]['color'] == $color && isset($_SESSION['cart'][$index]['size']) && $_SESSION['cart'][$index]['size'] == $size){
$_SESSION['cart'][$index]['quantity']=$qty;
print_r($_SESSION['cart'][$index]);//It just shows me the last item array.
}
}
?>
<form class="product" method="get" action="index.php">
<table>
<input type="hidden" name="page" value="cart">
<input type="hidden" name="action" value="update">
<?php
if(isset($_SESSION['cart'])){
foreach($_SESSION['cart'] as $id => $value){
?>
<tr>
<input type="hidden" name="id" value="<?php echo $value['id'] ?>">
<input type="hidden" name="name" value="<?php echo $value['name'] ?>">
<input type="hidden" name="color" value="<?php echo $value['color'] ?>">
<input type="hidden" name="size" value="<?php echo $value['size'] ?>">
<td><?php echo $value['id'] ?></td>
<td><?php echo $value['name']?></td>
<td><?php echo $value['price']?> </td>
<td><?php echo $value['quantity']?><input type="text" name="quantity" value="<?php echo $value['quantity'] ?>"></td>
<td><?php echo $value['color'];?> </td>
<td><?php echo $value['size']; ?></td>
<td><?php echo "$" .$value['price']*$value['quantity']. ".00"; ?>
</tr>
<?php
}
}
?>
<tr><td><button type="submit">Update</button></td></tr>
</table>
</form>
You can update the quantity by not posting or getting all the cart product. Either you can use simple JS to update the field value or use SESSION variable array to update data as #Marc B mentioned.
this is my html code:
<input name="dependencies[]"
and in php i do this:
$dependencies = $_POST['dependencies'];
and when I do this:
print_r($dependencies);
I can see the values like this:
Array ( [0] => [1] => )
My question
I want to add each value from that array to another array:
I didn't know how to do that
I tried:
foreach ($dependencies as $number){
echo $number;
}
but nothing has been printed
Update
this is the html
<input name="dependencies[]" value="<?php $question->id; ?>" type="checkbox" <?php if($db->does_question_depend_question($questionID, $question->id) == 0){}else{echo "checked";} ?> />
and I can see the check boxes checked or not when I run the page
Update2
the whole code
<form action="../DB/addDependencies.php" method="post">
<input type="hidden" name="questionID" />
<table>
<tr>
<th>Porgugese Name</th>
<th>Englisn Name</th>
<th>Dependent</th>
</tr>
<?php
foreach ($questions as $question) {
?>
<tr>
<td>
<?php echo $question->ptName; ?>
</td>
<td>
<?php echo $question->enName; ?>
</td>
<td>
<input name="dependencies[]" value="<?php $question->id; ?>" type="checkbox" <?php if($db->does_question_depend_question($questionID, $question->id) == 0){}else{echo "checked";} ?> />
</td>
</tr>
<?php
}
?>
</table>
<input type="submit" value="Save" name="submit" />
</form>
You need to change your HTML to this:
<input name="dependencies[<?php $question->id; ?>]" type="checkbox" <?php if($db->does_question_depend_question($questionID, $question->id) == 0){}else{echo "checked";} ?> />
Then to test if a checkbox if checked you just need to do something like
function isQuestionChecked($question_id) {
return isset($_POST['dependencies']) && isset($_POST['dependencies'][$question_id]);
}
try to use
foreach ($dependencies as $key=>$val){
var_dump($key);
var_dump ($val);
}
instead of
foreach ($dependencies as $number){
echo $number;
}
;-) hope that will bring some ideas to your mind in this case
I have a table to display Addon items from database.
The user must check the checkbox for the first two items namely: gps and babyseat.
So the deposit and priceperday are captured inside add_item[] of the checkbox.
But the third item which is Driver, there's no deposit or priceperday but price per hour.
So I gave out two radio buttons namely: one for per8thhour rate and the other one for per16thhour rate.
The problem is it captures both rate but I only want either one value of the radio button to be recorded.
Here's the table:
<table border="1" style="border-collapse: collapse;margin-top: 20px;">
<tr>
<th>AddOns</th>
<th>Deposit</th>
<th>PricePerDay</th>
<th>Price per 8th Hour</th>
<th>Price per 16th Hour</th>
<th></th>
</tr>
<?php
while($row_addon=mysql_fetch_array($result_addon))
{
$add_on_id=$row_addon['addOns_id'];
$add_on=$row_addon['addOns'];
$deposit=$row_addon['Deposit'];
$ppd=$row_addon['PricePerDay'];
$pp8=$row_addon['PricePer8thHour'];
$pp16=$row_addon['PricePer16thHour'];
$status=$row_addon['status'];
?>
<tr>
<td><input type="hidden" name="add_name" value=""><?php echo $add_on;?></td>
<td><input type="hidden" name="add_on"><?php echo $deposit;?></td>
<?php
if(isset($add_on)&&($add_on!=='Driver'))
{
?>
<td><input type="hidden" name="add_on"><?php echo $ppd;?></td>
<td><input type="hidden" name="add_on"><?php echo $pp8;?></td>
<td><input type="hidden" name="add_on"><?php echo $pp16;?></td>
<?php
}
else
{
?>
<td><input type="hidden" name="add_on"><?php echo $ppd;?></td>
<td><input type="radio" name="add_on"><?php echo $pp8;?></td>
<td><input type="radio" name="add_on"><?php echo $pp16;?></td>
<?php
}
?>
<td><input type="checkbox" name="add_item[]" value="<?php echo $add_on_id;?>"/></td>
</tr>
<?php
}
?>
<input type="hidden" name="submit_add">
<tr><td colspan="6"></td></tr>
</table>
I'm recording the value like this:
if(isset($_POST['submit_add']))
{
$add_item=$_POST['add_item'];
echo $add_item_1=implode(',',$add_item);
}
How do I make it possible to catch only the value selected by user for the item driver.
At the same time group them all under one name which is addon so that I can store them as one group in session later?
FULL FORM:
<form action="" method="POST">
<?php
//display pickup location
mysql_select_db($database_bumi_conn, $bumi_conn);
$q="SELECT * FROM tbl_pickup_location WHERE pickup_id='$location'";
$r=mysql_query($q);
$row_show= mysql_fetch_array($r);
?>
<input type="hidden" name="lo" value="<?php echo $location_name=$row_show['pickup_location'];?>">
<?php
echo 'Pickup Location :'.$location_name=$row_show['pickup_location'].'<br/>';
//display dropoff location
mysql_select_db($database_bumi_conn, $bumi_conn);
$q_1="SELECT * FROM tbl_dropoff WHERE dropoff_id='$d_location'";
$r_1=mysql_query($q_1);
$row_show_1= mysql_fetch_array($r_1);
?>
<input type="hidden" name="d_lo" value="<?php echo $location_name_1=$row_show_1['dropoff_location'];?>">
<?php
echo 'Return Location : '.$location_name_1=$row_show_1['dropoff_location'].'<br/>';
?>
<input type="hidden" name="val_1" value="<?php echo $date_value; ?>"/>
<?php
echo 'Pickup date : '.$date_value.'<br/>';
?>
<input type="hidden" name="val_2" value="<?php echo $date_value_2; ?>"/>
<?php
echo 'Return date : '.$date_value_2.'<br/>';
//$days=0;
$days=$diff->format("%a Days");
echo 'Total Rental for : '.$days.'<br/>';
//echo"hdssssssssssssfviodrhfvuhgudfhghdfhijswdjiahsdhsndjfhzsnhio";
$total_days=(int)$days;
?>
<input type="hidden" name="t_days" value="<?php echo $total_days; ?>"/>
<?php
include'calculation.php';
?>
<input type="hidden" name="sum" value="<?php echo $sum;?>">
<?php
mysql_select_db($database_bumi_conn, $bumi_conn);
$q_addon="SELECT * FROM tbl_addons";
$result_addon= mysql_query($q_addon)or die(mysql_error());
echo "<hr width=540>";
?>
<table border="1" style="border-collapse: collapse;margin-top: 20px;">
<tr><th>AddOns</th><th>Deposit</th><th>PricePerDay</th><th>Price per 8th Hour</th><th>Price per 16th Hour</th><th></th></tr>
<?php
while($row_addon=mysql_fetch_array($result_addon))
{
$add_on_id=$row_addon['addOns_id'];
$add_on=$row_addon['addOns'];
$deposit=$row_addon['Deposit'];
$ppd=$row_addon['PricePerDay'];
$pp8=$row_addon['PricePer8thHour'];
$pp16=$row_addon['PricePer16thHour'];
$status=$row_addon['status'];
?>
<tr>
<td><input type="hidden" name="add_name" value=""><?php echo $add_on;?></td>
<td><input type="hidden" name="add_item[]"><?php echo $deposit;?></td>
<td><input type="hidden" name="add_item[]"><?php echo $ppd;?></td>
<td><input type="radio" name="add_item[]" value="eight"><?php echo $pp8;?></td>
<td><input type="radio" name="add_item[]" value="six"><?php echo $pp16;?></td>
<td><input type="checkbox" name="add_item[]" value="<?php echo $add_on_id;?>"/></td>
</tr>
<?php
}
?>
<input type="hidden" name="submit_add">
<tr><td colspan="6"></td></tr>
</table>
<input type="image" src="submit.png" name="submit" value="submit" onclick="return confirm('Do you really want to proceed to checkout?');"href="checkout.php" style="padding-left:20px;" title="Add to cart"/>
</form>
To select one radio button at once, you have to give the all radio button's name same.
Otherwise you have to use javascript/jquery to deselect others when one is selected.
if you give all radio button name same, then when you post it will just post the value of the radio which is selected. You can get it by $_POST['radio_name'] .
Here it is $_POST['add_item'] .
If you are interested to use jquery, you can do that in this way
<script>
$(document).ready(function() {
$('input:radio').click(function(event) {
$('input:radio').not(this).attr('checked', false);
$(this).attr('checked', true);
});
});
</script>
Im getting crazy with this problem.
I try to use ajax to send data from a form in a way that I have not to reload all webpage (my webpage contain some queries for generate index menu that produce traffic); I create sendForm method, which handle form datas and pass it to php page to process.
This is the js code:
function sendForm(){
var http = false;
http = new XMLHttpRequest();
var ser = $(":input").serialize();
http.open("POST", "http://localhost/redir.php" ,true);
http.onreadystatechange= handleResponse();
http.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
http.setRequestHeader("Content-length", ser.length);
http.setRequestHeader("Connection", "close");
http.send(ser);
return true;
}
My form data contain a variable numbers of inputs because there's some php code inside a for cycle.
<form name="form" id="form" class="table table-hover" method="post">
<?php while(.....)) { ?>
<tr style="line-height:40px">
<td><?php echo $home;?></td>
<td><?php echo $vis;?></td>
<td><input type="text" name="goal_h[]" class="input-mini"
<?php if ($sq[16] == "2"){ ?>
readonly value="<?php echo $goal_h;?>"
<?php }else if ($sq[16] == "3"){
?> readonly value="-"
<?php } else echo "value=\"-\"";
?> id="goal_h<?php echo $i?>[]" maxlength="2" size="2" />
<input type="hidden" name="id_sub" value="<?php echo $id_sub;?>" />
<input type="hidden" name="id" id="id" value="<?php echo $id_d;?>" />
<input type="hidden" name="data_gg" value="<?php echo $data_gg;?>" />
<input type="hidden" name="n_gg" value="<?php echo $n_gg;?>" />
<input type="hidden" name="id_p_home[]" id="idphome<?php echo $i?>" value="<?php echo $tmp_id_p;?>" />
<input type="hidden" name="id_p_vis[]" id="idpvis<?php echo $i?>" value="<?php echo $sq[10];?>" />
</td>
<td>
<input type="text" name="goal_v[]" class="input-mini" <?php if ($sq[16] == "2"){ ?> readonly value="<?php echo $goal_v;?>" <?php }else if ($sq[16] == "3"){ ?> readonly value="-" <?php } else echo "value=\"-\""; ?> id="goal_v<?php echo $i?>[]" maxlength="2" </td>
<td><input type="checkbox" <?php if ($sq[16] == "2"){ ?> checked <?php } ?> id="sosp<?PHP echo $i?>" name="sosp[]" value="<?PHP echo $i?>" onclick="cambiaStato(<?php echo $i?>);"/></td>
<td><input type="checkbox" <?php if ($sq[16] == "3"){ ?> checked <?php } ?> id="nd<?PHP echo $i?>" name="nd[]" value="<?PHP echo $i?>" onclick="cambiaStato(<?php echo $i?>);"/>
</td>
</tr>
<tr style="line-height:40px">
<td><input type="submit" id="submit" name="submit" value="AGGIORNA IL PUNTEGGIO" onclick="sendForm()" /></td>
<td colspan="5"></td>
</tr>
<?php } ?>
</form>
this code works if I don't use with ajax and simply use an action=name_of_file.php, but when I try to use sendForm() method, in redir.php file I lose $_POST variables.
$home_id_p=$_POST['id_p_home'];
$vis_id_p=$_POST['id_p_vis'];
$goal_home=$_POST['goal_h'];
$goal_vis= $_POST['goal_v'];
$sosp= (isset($_POST['sosp'])) ? $_POST['sosp'] : "99";
$nd= (isset($_POST['nd'])) ? $_POST['nd'] : "99";
and then I need to loop on $home_id_p.
I tried also wihout serialize(), and also with serializeArray() :
var ser = $(":input").serializeArray();
but with this I'm not able to send it, because I get an error "invalid argument" in
http.send(ser)
Any idea?
Thanks
I am trying to create a function where users can check a message to delete. Obviously if more than one message is checked, they will all be deleted.
I have the following code
<form action="process.php" method="post">
<input type="hidden" name="deleteMessages" />
<?php
while($row=mysql_fetch_assoc($q))
{
if($row['to_viewed'] == 0)
{
?>
<tr>
<td><input type="hidden" name="check_box_1" value="0" />
<input type="checkbox" name="check_box_1" value="1" />
</td>
<td><b><a href='<?php echo $_SERVER['PHP_SELF']; ?>?p=view&mid=<?php echo $row['id']; ?>'><?php echo $row['title'] ?></a></b></td>
<td><b><a href='<?php echo $_SERVER['PHP_SELF']; ?>?p=view&mid=<?php echo $row['id']; ?>'><?php echo $row['from']; ?></a></b></td>
<td><b><?php echo $row['created']; ?></b></td>
</tr>
<?php
}
else if($row['to_viewed'] == 1)
{
?>
<tr>
<td><input type="hidden" name="check_box_1" value="0" />
<input type="checkbox" name="check_box_1" value="1" />
</td>
<td><a href='<?php echo $_SERVER['PHP_SELF']; ?>?p=view&mid=<?php echo $row['id']; ?>'><?php echo $row['title'] ?></a></td>
<td><a href='<?php echo $_SERVER['PHP_SELF']; ?>?p=view&mid=<?php echo $row['id']; ?>'><?php echo $row['from']; ?></a></td>
<td><?php echo $row['created']; ?></td>
</tr>
<?php
}
}
?>
<input type="submit" value="Delete All" />
</form>
I want to pass the checkbox through and if the value is 1, process it and delete it.
But how would I achieve this with mulitple messages no matter if there is one message or ten?
Thanks
In your form put:
<input type="checkbox" name="check_box_delete[]" value="<?php echo $row['id']; ?>" />
Then to process:
if(isset($_POST['check_box_delete']))
{
foreach($_POST['check_box_delete'] as $id)
{
// Delete $id
}
}
You can let php store mutliple values in an array by naming the input fields with an array designator suffix [].
For example, all checkboxes named checkbox[] will then be stored in $_POST['checkbox'][].
Note that this may not be applicable to checkboxes as their $_POST value only exists if they were checked.
<input type="checkbox"
name="mid_to_delete[]"
value="some message id"
id="mid_to_delete_some_message_id">
<label for="mid_to_delete_some_message_id">Delete "Some subject"</label>
and then
<?php
foreach ($_POST['mid_to_delete'] as $mid) {
delete_some_message_id($mid);
}
?>
Since only successful controls are submitted, and unchecked checkboxes are not successful, all the values you get will be ones that have been selected for deletion. (Obviously you still need to perform auth/authz to make sure that the messages being deleted are ones the user is allowed to delete)