I have the following code:
<form action="" method="POST">
<?php
$count = isset($_POST['count']) ? $_POST['count'] : 1;
if($count > 11) $count = 11;
?>
<table>
<!-- Keeps track of the current number of rows -->
<input type="hidden" name="count" value="<?php echo $count+1; ?>"/>
<?php for($i = 0; $i < $count; $i++):
// Loop through all rows gathering the data here, and then creating the fields below
$val0 = isset($_POST['field'][$i]['0']) ? $_POST['field'][$i]['0'] : '';
$val1 = isset($_POST['field'][$i]['1']) ? $_POST['field'][$i]['1'] : '';
$val2 = isset($_POST['field'][$i]['2']) ? $_POST['field'][$i]['2'] : '';
?>
<tr>
<td><input name="field[<?php echo $i; ?>][0]" value="<?php echo $val0; ?>"/></td>
<td><input name="field[<?php echo $i; ?>][1]" value="<?php echo $val1; ?>"/></td>
<td><input name="field[<?php echo $i; ?>][2]" value="<?php echo $val2; ?>"/></td>
</tr>
<?php endfor; ?>
</table>
<input type="submit" value="click me" />
How can I make the fields into a dropdowns and when you press submit echo out the dropdown as text instead as a dropdown?
First: Fill in the following:
Make this to action="mytestpage.php"
Give this a name attribue like: name="send"
Give these fields types like: type="Text"
If you want a dropdown menu use <select> in combination with <option>...
Here is a start to learn how:
http://www.echoecho.com/htmlforms11.htm
To print your result do this:
if(isset($_POST['send'])
{
print($_POST['youroptionname']);
}
I hope it helped you.
Related
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!
If i had enter all value in say four input boxes with name prod[] and when i click on the submit button, i want to show each values without loosing it if the array values are isset.
<?php for($i=1; $i<=8; $i++) { ?> <tr>
<td style="width:50% !important;">
<input type="text" class="form-control" name="PprodName[]" />
</td>
#QaisarSatti
thanks bro but i think i just got the logic. here is my code :
<?php
$j = 0;
for($i = 1; $i<=8; $i++) {
?>
<tr>
<td style="width:50% !important;">
<input type="text" class="form-control" id="Ppr<?php echo $i; ?>" name="PprodName[]" value="<?php echo isset( $_POST['PprodName'][$j] ) ? $_POST['PprodName'][$j] : ''; ?>" />
</td>
</tr>
<?php $j++; } ?>
mark this checked if you found it useful. thanks !
$name = $_POST['PprodName'];
foreach( $name as $v ) {
echo $v;
}
I'm tryng to save an array so I have the following code:
<?php
$sql = "SELECT * FROM scenarii where code_s='".mysql_real_escape_string($_POST['code_s'])."'";
$qry = mysql_query($sql) or die(__LINE__.mysql_error().$sql);
$i = -1; // index des enregistrements
?>
<table cellpadding="5" cellspacing="5">
<tr>
<td><strong>CODE SCENARIO</strong></td>
<td><strong>LIBELLE</strong></td>
<td><strong>ACTION</strong></td>
<td><strong>DESCRIPTION</strong></td>
<td><strong>DATE</strong></td>
</tr>
<form action="<?php echo (isset($_POST['go'])) ? 'go.php' : '#'; ?>" method="post">
<input type="hidden" name="liasse" value="<?php echo $_POST['liasse']; ?>"/>
<input type="hidden" name="n_doss" value="<?php echo $_POST['n_doss']; ?>"/>
<input type="hidden" name="qualite" value="<?php echo $_POST['qualite']; ?>"/>
<?php while($row = mysql_fetch_assoc($qry)): ?>
<tr>
<td><input name="data[<?php echo ++$i; ?>][code_s]" type="text" value="<?php echo $row['code_s'];?>" size="10"></td>
<td><input name="data[<?php echo $i; ?>][titre]" type="text" value="<?php echo $row['titre']; ?>" size="45"></td>
<td><input name="data[<?php echo $i; ?>][action]" type="text" value="<?php echo $row['action']; ?>" size="15"></td>
<td><input name="data[<?php echo $i; ?>][libelle]" type="text" value="<?php echo $row['libelle']; ?>" size="55"></td>
<td><input type="text" name="data[<?php echo $i; ?>][date]" value="<?php echo $get_date($row['jour']) ; ?>" size="12"></td>
</tr>
<?php endwhile; ?>
And in order to save this I have this code:
if (isset($_POST['liasse'])) {
$value = $_POST['data'] ;
foreach($value as $key => $array)
{
$sql = 'INSERT INTO agenda SET
liasse = "'.mysql_real_escape_string($_POST['liasse']).'",
code_s = "'.mysql_real_escape_string($array['code_s']).'",
date_action = "'.date('Y-m-d',strtotime($array['date'])).'",
libelle = "'.mysql_real_escape_string($array['titre']).'",
action = "'.mysql_real_escape_string($array['action']).'",
description = "'.mysql_real_escape_string($array['libelle']).'",
n_doss = "'.mysql_real_escape_string($_POST['n_doss']).'",
qualite = "'.mysql_real_escape_string($_POST['qualite']).'"
';
mysql_query($sql) or die(__LINE__.mysql_error().$sql);
}
But I'm really lost,
In fact I use a form for that, now I would like to submit all of this data but without any form, directly when I have the first while I would like to save it.
The thing is that I'm lost because I can not call any var like that data[][code_s].
So I do not know how to save this. I would like to save it in background, and not to display that something has been saved.
Receive all my Utmost Respect
kind regards,
SP.
Wrap the code od the lower code block into a function and hand over the value array as argument:
function storeValues ($data) {
foreach($data as $key => $val)
{
$catalog=sprintf("%s='%s'",$key,$val);
$sql = sprintf('INSERT INTO agenda SET %s', implode(',',$catalog));
mysql_query($sql) or die(__LINE__.mysql_error().$sql);
} // foreach
} // function storeValues
You call this function when you want to save the values. So after retrieving them from the database. You call it for each row you retrieve and hand over the values like that:
storeValues ($row);
This will store one row of values at a time. Obviously this can be optimized to use a multiple insert. But let's take one step after another...
I want to display array values followed by a checkbox inside a html table using a for loop :
<?php
for ($j = 0; $j < $data['user']['droit']['cnt']; $j++)
{
?>
<table>
<tr>
<td><?php echo $data['user']['droit'][$j]['menu_titre']; ?></td>
<?php
$checked = "";
if (is_array($data['user_droit']) && count($data['user_droit']) > 0 && in_array($data['user']['droit'][$j]['menu_code'], $data['user_droit']))
$checked = " checked ";
?>
<td><input type="checkbox" name="<?php echo $data['user']['droit'][$j]['menu_code']; ?>"
value="<?php echo $data['user']['droit'][$j]['menu_code']; ?>" <?php echo $checked ?> />
</td>
</tr>
</table>
<?php
}
?>
I don't know how to make the individual checkbox to be disabled if it is checked from the loop. How to achieve that ?
or set $checked = " checked disabled ";
<?php
for ($j = 0; $j < $data['user']['droit']['cnt']; $j++)
{
?>
<table>
<tr>
<td><?php echo $data['user']['droit'][$j]['menu_titre']; ?></td>
<?php
$checked = "";
if (is_array($data['user_droit']) && count($data['user_droit']) > 0 && in_array($data['user']['droit'][$j]['menu_code'], $data['user_droit']))
{
$checked = " checked disabled ";
}
?>
<td><input type="checkbox" name="<?php echo $data['user']['droit'][$j]['menu_code']; ?>"
value="<?php echo $data['user']['droit'][$j]['menu_code']; ?>" <?php echo $checked ?> />
</td>
</tr>
</table>
<?php
}
?>
I have the following code:
<form action="" method="POST">
<?php
$count = isset($_POST['count']) ? $_POST['count'] : 1;
if($count > 11) $count = 11;
?>
<table>
<!-- Keeps track of the current number of rows -->
<input type="hidden" name="count" value="<?php echo $count+1; ?>"/>
<?php for($i = 0; $i < $count; $i++):
// Loop through all rows gathering the data here, and then creating the fields below
$val0 = isset($_POST['field'][$count]['0']) ? $_POST['field'][$count]['0'] : '';
$val1 = isset($_POST['field'][$count]['1']) ? $_POST['field'][$count]['1'] : '';
$val2 = isset($_POST['field'][$count]['2']) ? $_POST['field'][$count]['2'] : '';
?>
<tr>
<td><input name="field[<?php echo $count; ?>][0]" value="<?php $val0; ?>"/></td>
<td><input name="field[<?php echo $count; ?>][1]" value="<?php $val1; ?>"/></td>
<td><input name="field[<?php echo $count; ?>][2]" value="<?php $val2; ?>"/></td>
</tr>
<?php endfor; ?>
</table>
<input type="submit" value="click me" />
</form>
The problem is when I press submit it add 3 other fields but it clears the other fields. How can I keep the content from the fields but make them uneditable?
You are not echoing your values and your $count should be $i, as you'll end up with same field names
<form action="" method="POST">
<?php
$count = isset($_POST['count']) ? $_POST['count'] : 1;
if($count > 11) $count = 11;
?>
<table>
<!-- Keeps track of the current number of rows -->
<input type="hidden" name="count" value="<?php echo $count+1; ?>"/>
<?php for($i = 0; $i < $count; $i++):
// Loop through all rows gathering the data here, and then creating the fields below
$val0 = isset($_POST['field'][$i]['0']) ? $_POST['field'][$i]['0'] : '';
$val1 = isset($_POST['field'][$i]['1']) ? $_POST['field'][$i]['1'] : '';
$val2 = isset($_POST['field'][$i]['2']) ? $_POST['field'][$i]['2'] : '';
?>
<tr>
<td><input name="field[<?php echo $i; ?>][0]" value="<?php echo $val0; ?>"/></td>
<td><input name="field[<?php echo $i; ?>][1]" value="<?php echo $val1; ?>"/></td>
<td><input name="field[<?php echo $i; ?>][2]" value="<?php echo $val2; ?>"/></td>
</tr>
<?php endfor; ?>
</table>
<input type="submit" value="click me" />
for($j=0;$j<3;$j++){
echo '<input type="hidden" name="field['.$i.'][0]" value="'.$_POST[field][$i][0].'" />';
}