So I was looking at some other posts on this site and came across the code below (EDIT of my code) which works perfectly apart from when I submit I get an error code of: Notice: Undefined index: id in /customers/0/2/e/richardbrown.name/httpd.www/debt/payment_process.php on line 17 for each result.
<form method="post" action="payment_process.php">
<table border="0">';
$stmt = $db->query("SELECT * FROM debt_accounts LEFT JOIN debt_companies ON accounts_company=companies_id WHERE accounts_amount > 0 ORDER BY accounts_company ASC");
while($row = $stmt->fetch(PDO::FETCH_ASSOC)){
echo'<tr>
<td width="200" align="left"><input type="text" name="payment[][id]" value="'.$row['accounts_id'].'" /></td>
<td width="100" align="right"><input type="text" name="payment[][amount]" value="25.00" /></td>
</tr>';
}
echo'</table><br /> <br /><input type="submit" /></form>
if ( isset( $_POST['payment'] ) )
{
echo '<table>';
foreach ( $_POST['payment'] as $diam )
{
echo '<tr>';
echo ' <td>', $diam['id'], '</td>';
echo ' <td>', $diam['amount'], '</td>';
echo '</tr>';
}
echo '</table>';
}
Change this line from your form element:
name="payment_amount"
to be an array:
name="payment_amount[]"
Then, modify your foreach to read as:
foreach ($_POST['payment_amount'] as $value) {
echo $value . "<br>";
}
You were using the wrong POST array => name="payment_id[]"
To add them all up, should you want to do that, use:
foreach ($_POST['payment_amount'] as $value) {
echo $value . "<br>";
$total += $value;
}
echo $total;
or
$total = array_sum($_POST['payment_amount']);
your solution is :
name="payment_id[]" in your form
and server side (payment_process.php)
you use a foreach loop with the $_POST['payment_id'][].
Related
I'm trying to develop a small "To Do List" application. The data for the app is stored in a database, and it needs to perform all CRUD operations. As it is right now, Select, Insert, and Delete work just fine. I'm stuck on updating though. The index.php page is shown below:
<?php
session_start();
require_once 'connect.php';
if (isset($_POST['DeleteTask'])) {
$sqldelete = "DELETE FROM Tasks WHERE dbTaskID = :bvTaskID";
$stmtdelete = $db->prepare($sqldelete);
$stmtdelete->bindValue(':bvTaskID', $_POST['taskID']);
$stmtdelete->execute();
echo "<div>Task successfully deleted</div>";
}
if (isset($_POST['theSubmit'])){
echo '<p>New task added</p>';
$formfield['ffTaskName'] = trim($_POST['taskName']);
$formfield['ffTaskDue'] = trim($_POST['taskDue']);
if(empty($formfield['ffTaskName'])){$errormsg .= "<p>Task field is empty.</p>";}
if(empty($formfield['ffTaskDue'])){$errormsg .= "<p>Deadline field is empty.</p>";}
if ($errormsg != "") {
echo "<div class='error'><p>Please fill out all fields before submitting.</p>";
echo $errormsg;
echo "</div>";
} else {
try {
$sqlinsert = 'INSERT INTO Tasks (dbTaskName, dbTaskDue, dbTaskDone)
VALUES (:bvTaskName, :bvTaskDue, :bvTaskDone)';
$stmtinsert = $db->prepare($sqlinsert);
$stmtinsert->bindValue(':bvTaskName', $formfield['ffTaskName']);
$stmtinsert->bindValue(':bvTaskDue', $formfield['ffTaskDue']);
$stmtinsert->bindValue(':bvTaskDone', 0);
$stmtinsert->execute();
echo "<div><p>There are no errors. Thank you.</p></div>";
} catch(PDOException $e){
echo 'ERROR!!!' .$e->getMessage();
exit();
}
}
}
$sqlselect = "SELECT * from Tasks";
$result = $db->prepare($sqlselect);
$result->execute();
?>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>To Do Application</title>
</head>
<body>
<h1><u>To-Do List</u></h1>
<table border>
<tr>
<th>Task</th>
<th>Deadline</th>
<th>Status</th>
<th>Complete</th>
<th>Edit</th>
<th>Delete</th>
</tr>
<?php
while ($row = $result->fetch()) {
if ($row['dbTaskDone'] == 0) {
$status = "Unfinished";
} else {
$status = "Finished";
}
echo '<tr><td>' . $row['dbTaskName']
. '</td><td>' . $row['dbTaskDue']
. '</td><td>' . $status;
/*if ($status == "Unfinished"){
echo '</td><td>';
echo '<form action="'. $_SERVER['PHP_SELF'] . '" method="post">';
echo '<input type="hidden" name="taskID" value"' . $row['dbTaskID'] . '">';
echo '<input type="submit" name="CompleteTask" value="Complete Task">';
echo '</form>';
}*/
echo '</td><td>';
echo '<form action="updateTask.php" method="post">';
echo '<input type="hidden" name="taskID" value="' . $row['dbTaskID'] . '">';
echo '<input type="submit" name="EditTask" id="EditTask" value="Edit Task">';
echo '</form></td><td>';
echo '<form action="'. $_SERVER['PHP_SELF'] . '" method="post">';
echo '<input type="hidden" name="taskID" value="' . $row['dbTaskID'] . '">';
echo '<input type="submit" name="DeleteTask" value="Delete Task">';
echo '</td></tr>';
}
?>
</table>
<form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>" name="toDoForm">
<fieldset><legend>New Task</legend>
<table>
<tr>
<th>Task</th>
<td><input type="text" name="taskName" id="taskName"
value="<?php echo $formfield['ffTaskName']; ?>"></td>
</tr><tr>
<th>Deadline</th>
<td><input type="text" name="taskDue" id="taskDue"
value="<?php echo $formfield['ffTaskDue']; ?>"></td>
</tr>
</table>
<input type="submit" name = "theSubmit" value="Add Task">
</fieldset>
</form>
</body>
</html>
Each record displays an "Edit" button that grabs the PK from the "Tasks" table and sends it to the updateTask.php page:
<?php
require_once 'connect.php';
$errormsg = "";
if (isset($_POST['EditTask']) ) {
$formfield['ffTaskID'] = $_POST['taskID'];
$sqlselect = "SELECT * FROM Tasks WHERE dbTaskId = :bvTaskID";
$result = $db->prepare($sqlselect);
$result->bindValue(':bvTaskID', $formfield['ffTaskID']);
$result->execute();
$row = $result->fetch();
if( isset($_POST['theEdit']) )
{
$formfield['ffTaskID'] = $_POST['taskID'];
$formfield['ffTaskName'] = trim($_POST['taskName']);
$formfield['ffTaskDue'] = trim($_POST['taskDue']);
if(empty($formfield['ffTaskName'])){$errormsg .= "<p>Task field is empty.</p>";}
if(empty($formfield['ffTaskDue'])){$errormsg .= "<p>Deadline field is empty.</p>";}
if ($errormsg != "") {
echo "<div class='error'><p>Please fill out all fields before submitting.</p>";
echo $errormsg;
echo "</div>";
} else {
try
{
$sqlUpdate = "UPDATE Tasks SET dbTaskName = :bvTaskName,
dbTaskDue = :bvTaskDue
WHERE dbTaskID = :bvTaskID";
$stmtUpdate = $db->prepare($sqlUpdate);
$stmtUpdate->bindvalue(':bvTaskName', $formfield['ffTaskName']);
$stmtUpdate->bindvalue(':bvTaskDue', $formfield['ffTaskDue']);
$stmtUpdate->bindvalue(':bvTaskID', $formfield['ffTaskID']);
$stmtUpdate->execute();
}
catch(PDOException $e)
{
echo 'ERROR!!!' .$e->getMessage();
exit();
}
}
}
}
?>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>To Do Application</title>
</head>
<body>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post" name="editForm">
<fieldset><legend>Edit Task</legend>
<table>
<tr>
<th>Task</th>
<td><input type="text" name="taskName" id="taskName"
value="<?php echo $row['dbTaskName'];?>" ></td>
</tr><tr>
<th>Deadline</th>
<td><input type="text" name="taskDue" id="taskDue"
value="<?php echo $row['dbTaskDue']; ?>"></td>
</tr>
<tr>
<th>Submit Changes</th>
<input type="hidden" name="taskID" value="<?php echo $_formfield['ffTaskID']; ?>">
<td><input type="submit" name="theEdit" value="Submit Changes">
</table>
</fieldset>
</form>
</body>
</html>
The Name and Deadline fields populate appropriately based on the PK value passed from the last page. However, whenever I press the "Submit Changes" button, the update doesn't seem to execute. The page just refreshes and I see the table data remains unchanged in the database.
Solved the problem!
There were several issues that I discovered.
1.) In updateTask.php, I had the second if-statement nested within the first one. So it was running the update query as the page loaded, with no change to the data. So the 'theEdit' button did nothing since since it required the previous if statement's condition to run.
2.) The formfield 'ffTaskID' at the bottom of the form on updateTask.php to be passed on the 'theEdit' button press was typed incorrectly.
$_formfield
..should have been..
$formfield
At this point, the update query functions properly.
3.) The issue with the 'Edit' buttons has been fixed. Though I honestly can't say for certain how it was fixed. It may have been linked with the first part of the problem. So when that was fixed, so was this.
Either way, all seems to be functioning as it should. Thanks again to everyone who commented and helped.
This is my code, where a user can add dynamic rows by clicking add more link.
But when I print
print_r($this->input->post('name1'));
in controller, it prints only first value but I want all values of array.
<form action="test.php" method="post">
<table rules="all" style="background:#fff;">
<tr>
<td style="font-size:14px;" >Name</td>
<td style="font-size:14px;">Email</td>
<td style="font-size:14px;">Mobile</td>
<td><span style="font:normal 12px agency, arial; color:blue; text-decoration:underline; cursor:pointer;" onclick="addMoreRows(this.form);">Add More</span>
</td>
</tr>
<tr id="rowId">
<td><input name="name1[]" type="text" value="" size="17%"/></td>
<td><input name="age1[]" type="text" value="" /></td>
<td><input name="relation1[]" type="text" value="" /></td>
</tr>
</table>
<div id="addedRows"></div>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<script type="text/javascript">
var rowCount = 1;
function addMoreRows(frm) {
rowCount ++;
var recRow = '<p id="rowCount'+rowCount+'"><tr><td><input name="name1[]" type="text" size="17%" maxlength="120" /></td><td><input name="age1[]" type="text" maxlength="120" style="margin: 4px 5px 0 5px;"/></td><td><input name="relation1[]" type="text" maxlength="120" style="margin: 4px 10px 0 0px;"/></td></tr> Delete</p>';
jQuery('#addedRows').append(recRow);
}
function removeRow(removeNum) {
jQuery('#rowCount'+removeNum).remove();
}
</script>
</form>
Try saving it to another variable and printing it like this:
$name1 = $this->input->post('name1');
print_r($name1);
You can loop through all the POST item, so you print out all the POST values. Also, I suggest incremented the 'name' atttribute as well, so you will have name1, name2, name 3 etc, or just remove the number because you are using an array.
If you want to increment the 'name' attribute
To loop:
$counter = 0;
foreach ($this->input->post('name1') as $key => $value)
{
$counter++;
// $value = name1
echo $this->input->post('age". $counter ."');
echo $this->input->post('relation". $counter ."');
}
With array
To loop:
foreach ($this->input->post('name1') as $key => $value)
{
echo $value;
$this->input->post('age1');
$this->input->post('relation1');
}
I havent tested it myself
Change your view page:
put <?php echo form_open_multipart('test/add_member'); ?> instead of
<form action="test.php" method="post">
In Controller: Dynamic input array data can handle as follows, this may help you..thanks!
public function add_member()
{
if (!$this->ion_auth->logged_in()) {
redirect('auth/login');
}
$itemCount = count($this->input->post('name'));
$itemValues=0;
$query = "INSERT INTO You_table_name(name, age, relation) VALUES ";
$queryValue = "";
for($i=0;$i<$itemCount;$i++) {
$name = $this->input->post('name');
$age = $this->input->post('age');
$relation = $this->input->post('relation');
if(!empty($name[$i])) {
$itemValues++;
if($queryValue!="") {
$queryValue .= ",";
}
$queryValue .= "('" . $name[$i] . "', '" . $age[$i] . "', '" . $relation[$i] . "')";
}
}
$sql = $query.$queryValue;
if($itemValues!=0) {
if (!$this->db->query($sql)) {
// echo "FALSE";
}else {
// echo "TRUE";
}
}
}
thank you for your answer meanwhile i got this code for inserting the multiple data, and it really works but my problem is it adds the fisrt column then after finished then it executes the second column i want them to excecute insert f1 then f2 then f1 then f2 again:
just imagine i have 4 input with same name f1 and f2
Inserting multiple entries into table from the same form please refer on this post
Your are missing names in all of your form fields
/*PHP code where the form submit, and repeat the same for other fields like descriptionField*/
<?php
for($i = 0; $i <= count ( $_POST ['dateField'] ); $i ++) {
// Do what ever you want with data
var_dump ( $_POST ['dateField'] [$i] );
}
?>
HTML Code, names added
<tr class="item-row">
<td class="item-name"><div class="delete-wpr">
<textarea>Date</textarea>
<a class="delete" href="javascript:;" title="Remove row">X</a>
</div></td>
<td class="description"><textarea name="descritpionField[]">Description</textarea></td>
<td><textarea name="dateField[]" style="text-align: center;"
class="asd">0</textarea></td>
<td><textarea name="dateField[]" style="text-align: center;"
class="qty">0</textarea></td>
<td><textarea name="dateField[]" style="text-align: center;"
class="cost">0</textarea></td>
<td style="text-align: center;"><span class="price">0</span></td>
</tr>
If i understand correctly
If this fields in form, then set 'name' attr to fields like name="user[]"
When u'll submit the form, all values of "user[]" fields will in array. Just do print_r() and you will see, what i try to explain
Also u can do it in javascript. . .
Just grab all values in array. . .
User "Noor" already post example for you
Please try this code,
<?php
echo "<pre>";
if(isset($_POST['submit'])){
print_r($_POST['data']);//RESULT WILL GET AN ARRAY
}
?>
<form action="" method="post">
<table>
<?php
//$items = // DATA FROM DB
//$items_count = count($items);
$items_count = 5;
for($i=0;$i<$items_count;$i++) {
?>
<tr class="item-row-<?php echo $i;?>">
<td class="item-name"><div class="delete-wpr"><textarea name="data[<?php echo $i;?>]['date']">Date</textarea><a class="delete" href="javascript:;" title="Remove row">X</a></div></td>
<td class="description"><textarea name="data[<?php echo $i;?>]['description']">Description</textarea></td>
<td><textarea name="data[<?php echo $i;?>]['age']" style="text-align:center;" class="asd">0</textarea></td>
<td><textarea name="data[<?php echo $i;?>]['dob']" style="text-align:center;" class="qty">0</textarea></td>
<td><textarea name="data[<?php echo $i;?>]['status']" style="text-align:center;" class="cost">0</textarea></td>
<td style="text-align:center;" ><span class="price">0</span></td>
</tr>
<?php } ?>
<table>
<input type="submit" class="button" name="submit" value="submit" />
</form>
Answer fo the post (Trouble with $_POST [duplicate])
I don't know if I understand your concern.
I think you are trying to create a quiz. And so the user must validate several attempts. your problem is that you can not accumulate the different answers following a table. so here is a solution.
<?php
$good_answers = array(
"easy1" => array("4","3","5","2","6","9","7","8","1" ),
"easy2" => array("6","8","2","5","7","1","4","9","3" ),
"easy3" => array("1","9","7","8","3","4","5","6","2" ),
"easy4" => array("8","2","6","1","9","5","3","4","7" ),
"easy5" => array("3","7","4","6","8","2","9","1","5" ),
"easy6" => array("9","5","1","7","4","3","6","2","8" ),
"easy7" => array("5","1","9","3","2","6","8","7","4" ),
"easy8" => array("2","4","8","9","5","7","1","3","6" ),
"easy9" => array("7","6","3","4","1","8","2","5","9" )
);
if(isset($_POST['row'])){
$easy = false;
$client_responses = $_POST['row']; // EX: [" "," "," " ,"2","6"," " ,"7"," " ,"1"]
$old = json_decode($_POST['old']);
$old[] = $client_responses;
// Or make array_push($old,$client_responses); if you prefere
foreach ($good_answers as $easy => $responses) {
if($client_responses === $responses){
$easy = $responses;
break;
}
}
// generating table for HTML of client responses
echo '<table>';
// saving old responses
echo '<input type="hidden" value="'. json_encode($old) .'" name="old">';
foreach ($old as $number => $row) {
echo '<tr id="row'. $number .'">';
for ($i=0; $i < count($row); $i++) {
echo '<td class="cellTop">';
echo '<input type="text" maxlength="1" name="row" value="'. $row[$i].'"/>';
echo '</td>';
}
echo '</tr>';
}
echo '</table>';
}
I am using php to insert into Microsoft SQL table and the below is my code:
$server = "**/**,1433";
$connectionInfo = array( "Database"=>"***", "UID"=>"**", "PWD"=>"******" );
$conn = sqlsrv_connect( $server, $connectionInfo );
if( $conn === false ) {
die( print_r( sqlsrv_errors(), true));
}
if(!empty($_POST['check_list'])) {
foreach($_POST['check_list'] as $check) {
$sql = "SELECT Item.HQID, Item.ItemLookupCode, Item.Description, Item.ExtendedDescription, Item.SalePrice, Item.Price, Item.CategoryID FROM Item WHERE Item.HQID = '$check'";
$stmt = sqlsrv_query( $conn, $sql );
if( $stmt === false) {
die( print_r( sqlsrv_errors(), true) );
}
while( $row = sqlsrv_fetch_array( $stmt, SQLSRV_FETCH_ASSOC) ) {
$html_table = '<table border="1" cellspacing="0" cellpadding="2">';
$html_table .= '<form method="post" action="upload.php"><tr><td width="350" align ="center">' .$row['Description']. '</td><td width="350" align ="center">' .$row['ExtendedDescription']. '</td><td width="130" align="center">'
.$row['ItemLookupCode']. '<td width="100" align="center">' .$row['SalePrice']. '</td><td width="100" align="center">' .$row['Price']. '</td><td width="200" align="center"><input
type="text" size="24" name="stitle"></td><td width="100" align="center"><input type="text" size="8" name="wprice"></td><td width="120" align="center"><input type="text" size="10" name="scategory"></td><td width="220" align="center"><input
type="text" size="28" name="sbody"></td></tr>';
$html_table .= '</table>';
echo $html_table;
}
}
}
}
?>
<input type="submit" value="Upload this page" ></form>
I have an <input> with name="stitle" and I want the PHP code to take values from each <input> but currently it picks up the value just from the first <input>. How do I fix this?
Your post is horrible formatted, but I think you're looking for the array notation.
You can make something like that:
<input type="text" size="24" name="stitle[]">
The $_POST array field stitle is an array with all values after submitting the form.
OK, a simple example of the form, ignoring all extra syntactical mark-up
<pre>
<?php print_r($_POST); ?>
</pre>
<form method="POST" action="">
<input type="text" size="24" name="stitle[]" />
<input type="text" size="24" name="stitle[]" />
<input type="text" size="24" name="stitle[]" />
</form>
Now, anything you enter into those three text boxes will all be returned, and be accessible in the "$_POST" variable.
If you do not want to use the array style inputs [] and you have control over the qty of fields then you can also do the following:
for($f=0; $f < 5; $f++){
echo '<input type="text" name="foo_'.$f.'">';
}
/*-----------server-side------------*/
for($f=0; $f < 5; $f++){
if(isset($_POST['foo_'.$f])){
//do something
}
}
You can use this simple code for multiple simple input
$stitle=$_POST['stitle'];
for($j=0;$j<count($stitle);$j++)
{
echo $stitle[$j]."<br>";
}
I have a form such as the below:
<form name="basketSelectionForm" action="processBasket.php" method="POST">
<div id="tabs-1">
<table cellpadding="10" cellspacing="10" width="inherit">
<tr>
<td><img alt="itemNameb" src="images/itemName.jpg" width="70px" height="70px"/></td>
<td>Qty. <input value="0" id="itemName" name="basket[itemName]" type="text" style="width:40px;"/> </td>
<td><img alt="itemName" src="images/itemName.jpg" width="70px" height="70px"/></td>
<td>Qty. <input value="0" id="itemName" name="basket[itemName]" type="text" style="width:40px;"/></td>
Now when I go to second page to look at the entries of the array, I do this:
<?php
$itemsBasket = array( );
$itemsBasket = $_POST['basket'];
echo "<h1>The Items Are...</h1><br>";
//print_r($itemsBasket);
foreach ($itemsBasket as $value)
{
if($value > 0){
echo $value . "<br>";
}
}
?>
This will print the value at the indexes of the array...but I need to store the name of the index so lets say the item is chocolate and value of 12. I want to extract that index name from array to store it in variable and then assign value to that variable...
Any way I can do that? right now I get only the value while iterating...
Thanks for help and sorry if question isn't clear I will help explain better if so...
UPDATE: this is the unexpected output....
whitethoab: Array woolthoab: 22 shemag: 22 undershirt: 1 serwalthoab:
22 socks: 12
and this is the definition of the element showing as two dimensional array...
<td><img alt="White Thoab" src="images/whitethoub.jpg" width="70px" height="70px"/></td>
<td>Qty. <input value="0" id="whitethoab" name="basket[whitethoab]" type="text" style="width:40px;"/> </td>
Something like:
<form name="basketSelectionForm" action="processBasket.php" method="POST">
<div id="tabs-1">
<table cellpadding="10" cellspacing="10" width="inherit">
<tr>
<td><img alt="itemNameb" src="images/itemName.jpg" width="70px" height="70px"/></td>
<td>Qty. <input value="12" id="itemName" name="basket[chocolate]" type="text" style="width:40px;"/> </td>
<td><img alt="itemName" src="images/itemName.jpg" width="70px" height="70px"/></td>
<td>Qty. <input value="9" id="itemName" name="basket[onions]" type="text" style="width:40px;"/></td>
...and...
<?php
echo "<h1>The Items Are...</h1><br>";
foreach ($_POST['basket'] as $name => $value)
{
if($value > 0){
echo $name . ": " . $value . "<br>";
}
}
/* Output:
chocolate: 12
onions: 9
*/
?>
?
Not sure, but i think you want this
foreach ($itemsBasket as $key => $value)
{
if($value > 0){
echo $key. "<br>\n"
echo $value . "<br>\n";
}
}