I am trying to pass a variable to Controller, but can't. The form I am submitting works fine, it shows everything. But when I try to delete a row, then the problem is that I can't submit it, so that my action would look like /Products/delete/2.
See my code below:
<tbody>
<?php
for($i = 0; $i < count($all); $i++){?>
<tr>
<form action="/Products/delete/<?= $i ?>" method="post">
<td><input name="name" value="<?php echo $all[$i]['name'] ?>" readonly></td>
<td><input name="price" value="<?php echo $all[$i]['price'] ?>" readonly></td>
<td><input name="qty" value="<?php echo $all[$i]['qty'] ?>" readonly></td>
<td><input type="submit" value="Delete"></td>
</form>
</tr>
<?php
}
?>
</tbody>
Thank you!
Related
I am sending a form data via ajax call to a php script. I am serializing the data in ajax and on the php script I want to loop through that data to extract the values.
This is my ajax call
$("#submitAttendance").click(function(){
var data = $('form#attendanceForm').serialize();
$.ajax({
url: 'save-attendance.php',
method: 'post',
data: {formData: data},
success: function(data){
console.log(data);
alert(data);
}
});
});
and in the attendance.php I am doing
print_r(($_POST['formData']));//prints the entire serialize data
when I do this
parse_str($_POST['formData'], $searcharray);
print_r(($searcharray));//prints only last user and all radio buttons
I want to extract values so I can save it in db.
This is my form
<form action="" id="attendanceForm">
<?php
if(mysqli_num_rows($result)>0){
while($row = $result->fetch_assoc()){ ?>
<tr>
<input type="hidden" value="<?php echo($row['id']);?>">
<td><input type="text" name="name" value="<?php echo $row['fullname'];?>" readonly></td>
<td><input type="text" name="email" value="<?php echo $row['email'];?>" readonly</td>
<td><input type="text" name="class" value="<?php echo $row['class'];?>" readonly</td>
<td><input type="radio" value="present" name="<?php echo($row['id']); ?>" checked></td>
<td><input type="radio" value="absent" name="<?php echo($row['id']); ?>"></td>
</tr>
<?php }
}
?>
<input id="submitAttendance" type="button" class="btn btn-success" value="Submit Attendance" name="submitAttendance">
</form>
You need to rename your items to be able to post arrays (that is call them "whatever" + "[]" and loop over them in PHP), e.g.:
HTML:
<form action="" id="attendanceForm">
<?php
if(mysqli_num_rows($result)>0){
while($row = $result->fetch_assoc()){ ?>
<tr>
<input type="hidden" value="<?php echo($row['id']);?>">
<td><input type="text" name="name[]" value="<?php echo $row['fullname'];?>" readonly></td>
<td><input type="text" name="email[]" value="<?php echo $row['email'];?>" readonly</td>
<td><input type="text" name="class[]" value="<?php echo $row['class'];?>" readonly</td>
<td><input type="radio" value="present" name="<?php echo($row['id']); ?>" checked></td>
<td><input type="radio" value="absent" name="<?php echo($row['id']); ?>"></td>
</tr>
<?php }
}
?>
<input id="submitAttendance" type="button" class="btn btn-success" value="Submit Attendance" name="submitAttendance">
</form>
Later in PHP:
foreach ($_POST["formData"]["name"] as $name)
echo "Wow, $name is a really pretty name!";
Additionally, I am not sure what present and absent are meant to do and why they should have the same name (an id). You are already posting the id as an hidden field, why should it be done twice? One overrides the other one (as the names have to be unique).
In addition to #Jan answer I did following to get the complete data and loop through it
parse the incoming data
parse_str($_POST['formData'], $searcharray);
then loop through the array
for ($i = 0 ; $i <= sizeof($searcharray) ; $i++){
$name = $searcharray['name'][$i];
$email= $searcharray['email'][$i];
$class = $searcharray['class'][$i];
$present= ($searcharray['present'][$i]);
}
and my form code is
<form action="" id="attendanceForm">
<?php
if(mysqli_num_rows($result)>0){
$i=0;
while($row = $result->fetch_assoc()){
?>
<tr>
<input type="hidden" value="<?php echo($row['id']);?>">
<td><input type="text" name="name[]" value="<?php echo $row['fullname'];?>" readonly></td>
<td><input type="text" name="email[]" value="<?php echo $row['email'];?>" readonly</td>
<td><input type="text" name="class[]" value="<?php echo $row['class'];?>" readonly</td>
<td><input type="radio" value="present" name="present[<?php echo $i; ?>]" checked></td>
<td><input type="radio" value="absent" name="present[<?php echo $i; ?>]"></td>
</tr>
<?php $i++;
}
}
?>
<input id="submitAttendance" type="button" class="btn btn-success" value="Submit Attendance" name="submitAttendance">
</form>
I need some help...
I've created a form with a dynamic table in it. I would like to learn how to pass it through php and echo/print it to a php page to save the page as pdf.
I am stuck on this part. I've looked everywhere for some answers and I cant seem to be finding anything.
<form action="display_form.php" method="POST">
<table id="dataTable" width="auto" style="margin:-4px 0 0 0;" cellspacing="0px">
<tr>
<td style="width:20px;"><INPUT type="checkbox" name="chk" /></td>
<td><INPUT type="text" name="step" style="width:160px;" autocomplete="on" placeholder="step" required/></td>
<td><INPUT type="text" name="url" style="width:62px;" autocomplete="on" placeholder="url" required/></td>
<td><INPUT type="text" name="process" style="width:63px;" autocomplete="on" placeholder="process" required/></td>
<td>
<SELECT name="pass-fail" style="width:100px;">
<OPTION value="Pass">one</OPTION>
<OPTION value="Fail">two</OPTION>
</SELECT>
</td>
<td><INPUT type="text" name="comment" style="width:190px;" autocomplete="on" placeholder="Comment" required/></td>
</table>
<INPUT type="button" value="Add row" onclick="addRow('dataTable')" />
<INPUT type="button" value="Delete row" onclick="deleteRow('dataTable')" />
<INPUT type="submit" value="Send"/>
I have called the php file display_form.php where I would like to display the dynamic table data in the. The function to save it as a pdf is in there as well. But I am mainly after the printing of the form to the php file. I am having no luck there at all!
I'm not worried on posting the table to mysql. I have other ways where i don't need it in mysql
I would really appreciate the help
2 things
you have not closed tr
Next
Make all input elements as array. Then you can fetch them in your php file
<td style="width:20px;"><INPUT type="checkbox" name="chk[]" /></td>
<td><INPUT type="text" name="step[]" style="width:160px;" autocomplete="on" placeholder="step" required/></td>
<td><INPUT type="text" name="url[]" style="width:62px;" autocomplete="on" placeholder="url" required/></td>
<td><INPUT type="text" name="process[]" style="width:63px;" autocomplete="on" placeholder="process" required/></td>
<td>
<SELECT name="pass-fail[]" style="width:100px;">
<OPTION value="Pass">one</OPTION>
<OPTION value="Fail">two</OPTION>
</SELECT>
</td>
<td><INPUT type="text" name="comment[]" style="width:190px;" autocomplete="on" placeholder="Comment" required/></td>
In your php file, use foreach loop to get all the values.
$i = 0;
$array = array();
foreach ($_POST['step'] as $row) {
$array[$i]['step'] = $row;
$array[$i]['chk'] = isset($_POST['ckh'][$i]) ? 1 : 0;
$array[$i]['url'] = $_POST['url'][$i];
$i++;
//...//so on
}
print_R($array);
As I can see, the javascript addRow function should add another tr-row in the table (the closing is missing by the way).
Now you have in every table row inputs with the same names. That won't work.
You need to say, that they are arrays, to access them via $_POST['step'][0] and so on.
Just give them name="step[]" and the form will automatically count the index for you.
Use name='step[]',name='url[]',name='process[]' as input type for your add row functionality,
Now when you submit form you are getting array then store that array as below.
$step = $_REQUEST['step'];
$url = $_REQUEST['url'];
$process = $_REQUEST['process'];
Now use foreach loop.
foreach($step as $key => $row){
echo $step[$key];
echo $url[$key];
echo $process[$key];
}
Now format your data in html then generate pdf as per below link.
http://phptopdf.com/
if (isset($_POST)) {
$step = $_REQUEST['step'];
$url = $_REQUEST['url'];
$process = $_REQUEST['process'];
$pass_fail = $_REQUEST['pass-fail'];
$comment = $_REQUEST['comment'];
foreach ($step as $key => $row) {
{
?>
<table id="dataTable" width="auto" style="margin:-4px 0 0 0;" cellspacing="0px">
<tr>
<td><INPUT type="text" name="step[]" style="width:160px;" value="<?php echo $step[$key]; ?>"/></td>
<td><INPUT type="text" name="url[]" style="width:62px;" value="<?php echo $url[$key]; ?>"/></td>
<td><INPUT type="text" name="process[]" style="width:63px;" value="<?php echo $process[$key]; ?>"/>
</td>
<td><input name="pass-fail[]" style="width:100px;" value="<?php echo $pass_fail[$key]; ?>"/></td>
<td><INPUT type="text" name="comment[]" style="width:190px;" value="<?php echo $comment[$key]; ?>"/>
</td>
</tr>
</table>
<?php }
}
}
Working just fine now!!
here the table is displaying correctly
I am looking to display the current value of the NumberedEntered value in the input box.
Currently it will only display the current value when I click submit then refresh. Does anyone know how I can echo it back as soon as I click submit?
Thanks.
<?php foreach($users as $user) : ?>
<tr>
<form action "index.php" method "post" name='form1'>
<td align="center" width="40%" ><?php echo $user['FullName']; ?><input type="hidden" Name="FullName" value="<?php echo $user['FullName']; ?>" /></td>
<td width="30%"><input type="number" name="NumberedEntered" value="<?php echo $user['NumberedEntered']; ?>" /></td>
<td><input type="submit" value="submit"></td>
</form>
</tr>
<?php endforeach; ?>
I have a 5x2 HTML table: http://jsfiddle.net/duxTU/
The table is enclosed in a form.
When the form is submitted it redirects to same page
The input field values are stored in an associative array
The values are displayed in respective fields where they were initially entered.
Code:
<?php
if(isset($_POST['submit'])) {
$contacts_array array(
$_POST["name1"] => $_POST['name2'],
$_POST["name3"] => $_POST['name4'],
$_POST["name5"] => $_POST['name6'],
$_POST["name7"] => $_POST['name8'],
$_POST["name9"] => $_POST['name10']);
$array_filtered=array_filter($contacts_array);
$arrayKeys = array_keys($array_filtered);
$arrayValues = array_values($array_filtered);
}
?>
The array has been filtered to omit null values that may creep in.
Now, how do I make the the HTML form display values? I know about sticky forms but since i must keep the form size constant(5x2), I am trying to use code similar to following
<input type="text" value="<?php echo $arrayKeys[0]; ?> ">
<input type="text" value="<?php echo $arrayValues[0]; ?> ">
... till $arrayKeys[4] and $arrayValues[4], respectively.
This gives me undefined offsets, for example $arrayKeys[5] was not set as no value was entered in the respective form column before submission.
Any solution to this issue??
Maybe this would work for you:
<?php
function getPost($param){
return isset($_POST[$param])?$_POST[$param]:"";
}
?>
<form action="#" method="post">
<table>
<tr>
<td>Name</td><td>Age</td>
</tr>
<tr>
<td><input type="text" name="name1" value="<?= getPost("name1") ?>"></td>
<td><input type="text" name="name2" value="<?= getPost("name2") ?>"></td>
</tr>
<tr>
<td><input type="text" name="name3" value="<?= getPost("name3") ?>"></td>
<td><input type="text" name="name4" value="<?= getPost("name4") ?>"></td>
</tr>
<tr>
<td><input type="text" name="name5" value="<?= getPost("name5") ?>"></td>
<td><input type="text" name="name6" value="<?= getPost("name6") ?>"></td>
</tr>
<tr>
<td><input type="text" name="name7" value="<?= getPost("name7") ?>"></td>
<td><input type="text" name="name8" value="<?= getPost("name8") ?>"></td>
</tr><tr>
<td><input type="text" name="name9" value="<?= getPost("name9") ?>"></td>
<td><input type="text" name="name10" value="<?= getPost("name10") ?>"></td>
</tr>
<tr>
<td><input type="submit" value="submit" name="submit"></td>
</tr>
</table>
</form>
The getPost helper was created because I didn't want to have to type the conditional every time.
You can filter out the nulls for whatever other form processing you are doing.
If you just want to display values, a foreach would work great:
foreach ($array_filtered as $k=>$v) {
echo "<input type='text' value='".$k."'>";
echo "<input type='text' value='".$v."'>";
}
foreach loops through an array. $k is the current key. $v is the current value.
Of course, this should be inside of your form HTML.
Struggling a little on this one.
This is my foreach code
<?php
$subtotal = 0;
foreach ($go_cart['contents'] as $cartkey=>$product):?>
<td><input type="hidden" name="itemdescription_0" value="Description" /></td>
<?php endforeach; ?>
And for this line
<td><input type="hidden" name="itemdescription_0" value="Description" /></td>
I need it to up by one for each foreach. Such as:
<td><input type="hidden" name="itemdescription_0" value="Description" /></td>
<td><input type="hidden" name="itemdescription_1" value="Description" /></td>
<td><input type="hidden" name="itemdescription_2" value="Description" /></td>
I'm pretty sure you have the quick solution for me on this one...
Thanks guys!, appreciate it.
HERE IS WHAT I FORGOT TO MENTION:
I have multiple fields for this action. So i have description, itemcount, itemamount etc.
So they should be like this
<td><input type="hidden" name="itemdescription_0" value="Description" /></td>
<td><input type="hidden" name="itemcount_0" value="Description" /></td>
<td><input type="hidden" name="itemamount_0" value="Description" /></td>
But with the solution you sent me i get.
<td><input type="hidden" name="itemdescription_1" value="Description" /></td>
<td><input type="hidden" name="itemcount_2" value="Description" /></td>
<td><input type="hidden" name="itemamount_3" value="Description" /></td>
Sorry, my mistake!...
Change:
$subtotal = 0;
to
$subtotal = 0;
$i = 0;
Then do the following:
<td><input type="hidden" name="itemdescription_<?php echo $i; ?>" value="Description" /></td>
<td><input type="hidden" name="itemcount_<?php echo $i; ?>" value="Description" /></td>
<td><input type="hidden" name="itemamount_<?php echo $i; ?>" value="Description" /></td>
Right BEFORE you close your outer foreach() loop do this on its own line:
$i++;
What you are doing is first initializing the variable $i by setting it to 0. You then output the current value of $i. Then you will use the post-increment operator with $i as $i++ at the end of your loop to increase its value by 1 so the next time it is output on next run of the loop it will be increased by 1.
Here's the code
<?php
for ($i=0; $i<=2; $i++)
{
echo "<td><input type=\"hidden\" name=\"itemdescription_" . $i . "value=\"Description\" /></td>"."\n";
}
?>