I have a loop in a form for dynamically adding/removing items, however, only the last item is removed every time. I can add items just fine though.
I can't figure out why no matter which items I try to remove from both add_friends and register_tasks, ONLY the last item is removed. When I echo $_POST['task_name'] it's always the last item on the list, never the one I selected(unless of course I select the last item).
Here is the form
<center><h2>Create Event</h2></center>
<form method="post">
<?
$form = new common_functions();
if($form->check_saved_forms($my_username, "event") == TRUE){
$this->existing_forms(); //allow the user to select existing forms that they have created
}
?>
<tr><td>Title:</td><td><input type="text" width="20%" name="event_title" value="<? echo $form_data[0]; ?>" /></td></tr>
<tr><td>Details:</td><td><input type="text" width="20%" name="event_details" value="<?echo $form_data[1]; ?>" /></td></tr>
<tr><td>Date:</td><td><input type="text" width="20%" name="event_date" id="event_date" value="<? echo $form_data[2]; ?>" /></td></tr>
<tr><td> <input type="submit" name = "submit_event" id = "lOBut" value="Create Event" /></td></tr>
</table>
<?
//this is the section giving me trouble
$form->add_friends($added_friends);
$form->register_tasks($tasks,$nums);
?>
</form>
Both add_friends and register_tasks dynamically add and remove friends/tasks by a textbox, and a list of the added items are shown. users can remove items, however, right now only the last item is removable and I can't figure out why.
Here is register (add_friend is the same, but labeled differently)
function register_tasks($tasks,$nums){
<table>
<td><input type="textbox" size="50" name="register_description"/> </td></tr>
<td><input type ="textBox" name="register_num" /> </td>
<td><input type="submit" name="add_register_tasks" value="Add"/></td></tr>
</table>
<?
if(count($tasks) > 0){
?>
<table>
<tr><td><b>Registration Item Description</b></td><td><b># Of Available Spots</b></td></tr><br>
<?
foreach (array_combine($tasks, $nums) as $task => $task_num){
?>
<tr><td><? echo $task; ?></td><td><? echo $task_num; ?></td>
<td><input type="submit" name="remove_task" value="Remove"/></td>
<td><input type="hidden" name="task_name" value="<? echo $task; ?>"/>
<td><input type="hidden" name="task_num" value="<? echo $task_num; ?>"/></td></tr>
<?
}
?></table><?
}
You need to place the form tag inside the loop. Right now what happens is all the records are displayed in a single form and no matter what item you choose, it takes only the last value. Another alternate solution would be to use GET method and pass the number/name as query string.
Related
Am having table data (retrieve data from mysql table and fetch in to table). table contains several records.I want to display checked checkbox value with input box value and checkbox when i clicking button in php. Checked checkbox value and checked input has deen displayed correctly using join function. but checked with checkbox is not showing correctly. In my code, when i clicking button all checked check values are displayed. my problem to display only checked checkbox with checkbax using join function.
My table:
<table border="0" cellpadding="10" cellspacing="1" width="500" class="tblListForm">
<tr class="listheader">
<td></td>
<td>Username</td>
<td>First Name</td>
<td>Last Name</td>
<td>Permissions</td>
<td>CRUD Actions</td>
</tr>
<?php
$i=0;
while($row = mysqli_fetch_array($result)) {
if($i%2==0)
$classname="evenRow";
else
$classname="oddRow";
?>
<tr class="<?php if(isset($classname)) echo $classname;?>">
<td><input type="checkbox" class="chk_id" name="chk_id" id="chk_id" value="<?php echo $row["userId"]; ?>" /></td>
<td><?php echo $row["userName"]; ?></td>
<td><input type="text" name="firstName" class="firstName" id="firstName" value="<?php echo $row["firstName"];?>" /></td>
<td><?php echo $row["lastName"]; ?></td>
<td><input type="checkbox" name="grant" class="grant" id="grant" value="Y" /></td>
<td><img alt='Edit' title='Edit' src='images/edit.png' width='15px' height='15px' hspace='10' /> <img alt='Delete' title='Delete' src='images/delete.png' width='15px' height='15px'hspace='10' /></td>
</tr>
<?php
$i++;
}
?>
</table>
<input type="button" id="save_value" name="save_value" value="Save" />
my jquery code what i have tried:
$('#save_value').click(function () {
alert("Checkbox running");
var chk_id = [];
var firstName = [];
var grant = [];
$.each($("input[ id='chk_id']:checked"), function () {
chk_id.push($(this).val());
firstName.push($(this).parent().parent().find("#firstName").val());
grant.push($(this).parent().parent().find($("#grant").is(':checked'));
});
alert(chk_id);
alert(firstName);
alert(grant);
});
Here,
am getting checked checkbox and checked input value. my problem to dispaly the checked checkbox with check value.
Thanks#
You made a few small mistakes:
You can't have multiple elements with the same ID, IDs must be unique. So I removed all duplicate IDs from your HTML (id="chk_id",id="firstName",id="grant") and in your JS, used the classes instead.
You missed a closing bracket in grant.push($(this).parent().parent().find($(".grant").is(':checked')));.
.find($(".grant").is(':checked')) isn't working as you expect, and also not necessary.
Use this instead: .find(".grant:checked").
And finally, the reason why your alert showed two values whether the checkboxes were checked or not: grant.push( ... ); always pushes something into the array, if the jQuery-selector matched nothing and would return false, that value would still be pushed into the array.
In fact, if you correct all three points above, and don't check the permission checkbox, the value in the array will be undefined. If you do check the box, it will be Y.
So, in order to make it work, you just have to put the grant.push( ... ); inside an if-clause, where you check for ".grant:checked":
if ($p.find(".grant:checked").length) {grant.push($p.find(".grant:checked").val());}
- $p stands for $(this).parent().parent(), I stored a reference in a var.
- .length checks if the length of the returned object is greater than 0. Without it, the if-clause would still always be true, because jQuery still returns an object (with value undefined).
See code snippet below for a demo:
$('#save_value').click(function() {
var chk_id=[], firstName=[], grant=[];
$.each($("input[class='chk_id']:checked"),function() {
var $row = $(this).parent().parent();
chk_id.push($(this).val());
firstName.push($row.find(".firstName").val());
if ($row.find(".grant:checked").length) {grant.push($row.find(".grant:checked").val());}
});
console.log(chk_id, firstName, grant);
});
table,input[type=button] {float:left;} /*ONLY SO console.log() DOESN'T COVER BUTTON*/
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table border="0" cellpadding="0" cellspacing="0" width="500" class="tblListForm">
<tr class="listheader"><td></td><td>Username</td><td>First Name</td><td>Last Name</td><td>Permissions</td></tr>
<tr class="evenRow">
<td><input type="checkbox" class="chk_id" name="chk_id" value="4" /></td>
<td>sardhar</td>
<td><input type="text" name="firstName" class="firstName" value="sardhar" /></td>
<td>mohamed</td>
<td><input type="checkbox" name="grant" class="grant" value="Y" /></td>
</tr>
<tr class="oddRow">
<td><input type="checkbox" class="chk_id" name="chk_id" value="3" /></td>
<td>fg</td>
<td><input type="text" name="firstName" class="firstName" value="vb" /></td>
<td>vb</td>
<td><input type="checkbox" name="grant" class="grant" value="Y" /></td>
</tr>
</table>
<input type="button" id="save_value" name="save_value" value="Save" />
jsfiddle: https://jsfiddle.net/3utno9at/
I've got a MySQL database and a table called courses. I would like to show the records of this table within an html table, but I would also like to edit them with a submit button at the end.
I know this has been asked before, but I'm still struggling with how I should properly proceed.
I currently have:
<tbody>
<?php
foreach ($db->getResultSet() as $row) {
extract($row);
?>
<tr>
<form id="myTable" name="editrecord" action="course.php" method="post">
<input type="hidden" name="code" value="<?php echo $code; ?>" /><!--PrimaryKey-->
<td><?php echo $code; ?></td>
<td><input type="text" name="name" value="<?php echo $name; ?>" /></td>
<td><input type="text" name="desc" value="<?php echo $desc; ?>" /></td>
<td><input type="submit" name="Edit" value="Edit!" /></td>
</form>
</tr>
<?php
}
?>
</tbody>
Now this seems to work fine. However I'm aware that <form> should not be inside <tr> or anywhere else other that <td>
I'm aware I could use various divs and set display properties to table layout etc. however I'd like to avoid this as I am using pre-made javascript that can sort and filter data which requires me to use <table>
I also tried a for loop outside the table to create 1 form per record and use the primary key as the form's id. Then proceed with echo "<td><input form='$code' .... But this doesn't work in Explorer
What is the correct way to implement something like this?
Please help me out of this.....
I am designing a table which is inside a form.
the table is generated based on while loop.
In each row there is a download button.
when i click download the POST value should get the same row information.
But my POST variable is giving me the last row information only.
I tried using input-type as hidden... But it did not work
Here is the code for your reference
enter code here
<form name="simpleform" method="post" action="insert.php">
<?php
$data = "environment";
$user_name = $_SESSION['username'];
$serch = mysql_query("SELECT * FROM data WHERE (data_category = '" . $data . "') ");
while ($record=mysql_fetch_assoc($serch))
{?>
<tr class="warning">
<td >
<input type="text" value=<?php echo $record['data_ID'];?> readonly="readonly" >
<input type="hidden" value=<?php echo $record['data_ID'];?> name="dataid" />
</td>
<td >
<input type="text" value=<?php echo $record['data_name'];?> readonly="readonly" >
<input type="hidden" value=<?php echo $record['data_name'];?> name="dataname" />
</td>
<td >
<input type="text" value=<?php echo $record['data_downloads'];?> readonly="readonly">
<input type="hidden" value=<?php echo $record['data_downloads'];?> name="datadown" />
</td>
<td >
<input type="text" value="" >
<input type="hidden" value="" name="datause" />
</td>
<td>
<input type="submit" name="simplesubmit" value="Go to download" />
</td>
</tr>
<?php }
exit;
?>
</tbody>
</form>
The problem is that you are using the same name attribute for all your controls. Thus, when PHP receives the form, they get overwritten, and you only see the last value of the form.
The simplest way to avoid that is just appending [] to the end of your names -- eg name=dataid[]. This will make PHP take all arguments as an array, so you don't lose data.
The second problem, is that your submit button also has the same name - you should diversify it by using some row-specific data in its name, such as 'name="submit-'.$record['data_name'].'"'
For more info, more code from you is needed, such as what are the data you are printing like.
Every post button can have its name and value, so if you change your code to produce a traceable post button name you can just do whatever you want with that.
<table>
<tr>
<td>...</td>
<td>...</td>
<td><input type="submit" name="submit[1]" value="OK" />
</tr>
<tr>
<td>...</td>
<td>...</td>
<td><input type="submit" name="submit[2]" value="OK" />
</tr>
</table>
When the form is posted its very easy to capture which button is clicked;
if ($_POST["submit"]) {
$id = key($_POST["submit"]);
}
Thanks for info... and good response. As you said , i replaced the same and saw the post value is giving me all arguments as array. My purpose is to let the client download file that he clicks. so if the client click the first row button in the table, the post value should get only that Data_name. So that i can run a query to get the URL of that data_name and download
Is there a way that I can post two values at the same time from a single field in a table but hide one from the user?
I would like the following form to post the values ID and reason_name when it is submitted but have the user only be able to see (and edit in the text box) the reason_name.
<form name="add_positioning" method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<table border="1" class="autoTable_pos">
<tr>
<td>Positioning</td><td> </td></tr>
<?
$sql= "SELECT * FROM gradeReason WHERE reason_userID = $user_id AND category = 'positioning' AND current = 1";
$result = mysqli_query($mysqli,$sql) or die(mysqli_error($mysqli));
while($row = mysqli_fetch_array($result)){?>
<tr>
<td>
<input type="text" name="reason[]" size="25" value="<? echo $row['reason_name']; ?>"/>
</td>
<td>
<input type="button" value="Delete" class="delRow_pos"/>
</td>
</tr>
<?
}
?>
<tr>
<td>
<input type="text" name="reason[]" size="25"/>
</td>
<td>
<input type="button" value="Delete" class="delRow_pos"/>
</td>
</tr>
<tr>
<td colspan="2" align="right">
<input type="submit" value="Save" name="save_reasons"/>
</td>
</tr>
</table>
</form>
The form POST action so far (basic echo at the moment for my own sanity to check that it posts the values correctly, which it does...)
if(isset($_POST['save_reasons'])) {
foreach($_POST['reason'] as $item) {
echo $item.'<br/>';
}
}
This table displays the values that are held in a database but enables the user to add more values by dynamically adding a new row (using JQUERY I haven't included) to the table when they type in an empty one at the bottom and also allows them to edit or delete existing values.
For each value posted I intend to check if the ID value is empty, if it is it means that it is a new value and enter a new record into the database, if it isn't update the existing record in the database with the corresponding ID. I don't have a problem writing that bit, I just can't think how to get the ID value posted as well as the reason_name while keeping ID hidden from the user.
Add the ID to the name attribute of the Text boxes of the reasons loaded from the DB. Leave the other Text boxes added using JQ without the ID.
E.g.
Text box which shows the existing reason loaded from the DB
<input type="text" name="reason[][ID]" size="25"/>
Text box added with JQ
<input type="text" name="reason[]" size="25"/>
Then once you submit the form you get you will get the following array.
array
'reason' =>
array
0 =>
array
14 => string 'VAL1' (length=4)
1 => string 'VAL2' (length=5)
By checking for an array in the each element in the "reason" array, you can differentiate two type of Text Boxes.
Here is the complete code I have tested.
<?PHP
//var_dump($_POST);
foreach($_POST['reason'] as $item=>$value)
{
if(is_array($value)){
foreach($value as $ID=>$reason)
{
echo "Existing Reason : ".$ID." - ".$reason."<br>";
}
}
else{
echo "New Reason : ".$item." - ".$value.'<br/>';
}
}
?>
<form action="" method="POST">
<input type="text" name="reason[][14]" size="25" value="aaaa"/>
<input type="text" name="reason[]" size="25" value="bbbbb"/>
<input name="" type="submit">
</form>
Assuming the id is at $row['reason_id'] I think you want:
$i = 0;
while($row = mysqli_fetch_array($result)){?>
<tr>
<td>
<input type="hidden" name="reason_id[<? echo $i; ?>]" size="25" value="<? echo $row['reason_id']; ?>"/>
<input type="text" name="reason[<? echo $i; ?>]" size="25" value="<? echo $row['reason_name']; ?>"/>
</td>
<td>
<input type="button" value="Delete" class="delRow_pos"/></td></tr>
<? $i++ } ?>
This way you can later
if(isset($_POST['save_reasons'])) {
foreach($_POST['reason'] as $key => $item) {
$id = $_POST['reason_id'][$key];
echo $id . " " . $item.'<br/>';
}
}
I had following code.My textbox attend is looped for number of times.I want to get value of each attend when my form is post to insert value in database.
<form method="post" action="insert.php">
<label>Subject</label>
<input type="text" maxlength="30" name="subject" />
<label>Total Lectures</label>
<input type="text" maxlength="30" name="total" />
<table width="537" border="1">
<tr>
<td width="90">Name</td>
<td width="139">Roll Number </td>
<td width="136"> </td>
<td width="144">Attendence</td>
</tr>
<?php
$query=mysql_query("SELECT * FROM STUDENT");
$i=0;
while($rec=mysql_fetch_array($query)){
$i++;
?>
<tr>
<td><?php echo $rec['name']; ?></td>
<td><?php echo $rec['roll_number']; ?></td>
<td> </td>
<td><input type="text" maxlength="10" name="atten" /></td>
</tr>
<?php } ?>
</table>
<input type="submit" value="submit" />
</form>
and my insert.php page is
if($_SERVER['REQUEST_METHOD']=='POST'){
$query=mysql_query("SELECT * FROM STUDENT");
while($rec=mysql_fetch_array($query)){
$attend=$_POST['atten'];
$subject=$_POST['subject'];
$total=$_POST['total'];
$query1=mysql_query("INSERT INTO course VALUES('$i','$subject','$total','$attend','')") or die(mysql_error());
}
}
I am getting only 1 value of text box.
The problem is that you have many inputs all with the name atten. When you post this form, only one of those values will be carried forward.
If you change the name to atten[] all of the values would be posted as an array, which you would then have to loop through to build you insert query.
You could access this posted array as follows:
$attendee_array = $_POST['atten'];
foreach($attendee_array as $attendee) {
// perform insert or build insert query (prefereable as you can do all these inserts at once) here
// make sure to escape your data for input
}
Change the names of your inputs:
<input name="subject[]" value="Lorem" />
<input name="total[]" value="ipsum" />
<input name="atten[]" value="dolor" />
Then:
$_POST['subject'][0] == 'Lorem'
$_POST['total'][4] == 'amet'
If so, that would make my life ten times easier, as I could send an
indefinite amount of information through a form and get it processed
by the server simply by looping through the array of items with the
name "subject". etc.
You need to use input array like:
<input type="text" maxlength="10" name="attend[]" />
And then you will get all the values of attend in an array and you can loop through that $_POST['attend'].