I'm trying to pass a text value field over to the next page for every checkbox selected, but I'm only getting the last text fields value, example:
checkbox textfield
selected ABCD
selected ABCDE
I am only getting back the ABCDE every time
page1.php
echo "<td width='10px'><input name='question[$rowid][]' type='checkbox' value='1' /></td>";
echo "<td width='230px'><input name='newname' type='text' value='$certn'/></td>";
page2.php
foreach ($_POST['question'] as $key => $ans) {
$nn = $_POST['newname'];
echo $key . $nn;
echo "</br>";
}
Help will be greatly appreciated
It's a little hard to work out exactly what you're doing here but I think your statement I'm only getting the last text fields value indicates your problem - you have multiple fields with the same name. If you do this and don't make them into an array ([]), you will only get the last value on the page.
I think you want something more like this:
Page 1:
echo "<td width='10px'><input name='question[$rowid]' type='checkbox' value='1' /></td>";
echo "<td width='230px'><input name='newname[$rowid]' type='text' value='$certn'/></td>";
Page 2:
foreach ($_POST['question'] as $key => $ans) {
// $_POST['newname'] is now also an array, and the keys should correspond to
// those in the $_POST['question'] array
$nn = $_POST['newname'][$key];
echo $key . $nn;
echo "</br>";
}
The line:
echo "<td width='10px'><input name='question[$rowid][]' type='checkbox' value='1' /></td>";
will not be correctly interpreted. You must change it to:
echo "<td width='10px'><input name='" . $question[$rowid][] . "' type='checkbox' value='1' /></td>";
Arrays are not substitued inside a string.
Related
I m trying to store checkbox value subject_id but when i store this value to database it just store the last selected checkbox inside the database... please help me to sort out the code so I can get all selected values.
I want to save all the checkbox values I selected,but if I check more then one checkbox it always save the last checkbox.
function get_class_section($day='',$section='',$class='')
{
print_r($section);
$sections = $this->db->get_where('class_routine' , array(
'day' => $day,'section_id'=>$section,'class_id'=>$class
))->result_array();
foreach ($sections as $row) {
echo "<table class=\"table table-bordered datatable\" id=\"table_export\" >";
echo "<thead>";
echo "<th>Subject</th><th colspan=\"2\">status</th>";
echo "</thead>";
echo "<tbody>";
echo "<tr>";
echo "<td>". $this->crud_model->get_subject_name_by_id($row['subject_id'],"<input type=\"text\" name=\"subject_id\" value=\"subject_id\" >");
echo "<input type=\"checkbox\" name=\"types[]\" value=\"".$row['subject_id']."\" set_checkbox('types[]', 'subject_id')>";
echo "</td>";
echo "<td>present <input type=\"checkbox\" value=\"1\" ></td>"
. "<td>Absent <input type=\"checkbox\" name=\"types[]\" value=\"0\"></td>";
echo "</tr>";
echo "</tbody>";
echo "</table>";
}
}
how to save all the check-boxes that i selected
You need foreach loop to get all checked checkboxes
Try This:
if(isset($_REQUEST['types']) && $_REQUEST['types']!=array())
{
foreach ($_REQUEST['types'] as $key => $value) {
// in $value,you will get checkbox value
}
}
You should use hidden element for all checkbox
Try this
. "<td>Absent <input type=\"checkbox\" name=\"types[]\" value=\"0\"></td>
<input type=\"hidden\" name=\"types[]\" value=\"0\"> ";
I'm trying to create a Table where you can 'Sign Off' items populating it. Currently the table looks to be populated fine, however when you go to 'Sign Off' an item - it will only act on the latest mysql row ['directory'] as well as the latest text field accompanying it.
When I say 'Sign Off' I mean to insert someone's initials into the mysql table row associated with that item. I first of course need to get the POST submission handled correctly.
That probably wasn't articulated perfectly, so please take a look at my current code;
echo "<form action='signoff.php' method='post' id='signoffform' style='display:inline;'>";
echo "<p3><table id='myTable3' class='tablesorter'>
<thead>
<tr>
<th>To Delete</th>
<th>Directory</th>
<th>Space Used</th>
</tr>
</thead>";
while($row = mysqli_fetch_array($result))
{
echo "<tr>";
echo "<td><input type='text' name='signoff' id='signoff' form='signoffform' /><button>Sign Off</button> Signed Off By:" . $row['signoff'] . "</td>";
echo "<td><input type='text' name='dir' id='dir' form='signoffform' value='" . $row['directory'] . "' readonly /></td>";
echo "<td>" . $row['used'] . "</td>";
echo "</tr>";
}
echo "</table></p3>";
echo "</form>";
signoff.php
<?php
echo $_POST["signoff"];
echo "<br />";
echo $_POST["dir"];
?>
Please feel free to request more info
... when you go to 'Sign Off' an item - it will only act on the latest mysql row ['directory'] as well as the latest text field accompanying it.
The problem is because of your name attributes, name='signoff' and name=dir. When you loop through the result set, the name attribute value gets overwritten in each iteration. Hence whatever button you click, you'll always get the last row values.
So the solution would be like this:
Change your while loop in the following way,
// your code
while($row = mysqli_fetch_array($result)){
echo "<tr>";
echo "<td><input type='text' name='signoff[".$row['directory']."]' id='signoff' form='signoffform' /><button>Sign Off</button> Signed Off By:" . $row['signoff'] . "</td>";
echo "<td><input type='text' name='dir[".$row['directory']."]' id='dir' form='signoffform' value='" . $row['directory'] . "' readonly /></td>";
echo "<td>" . $row['used'] . "</td>";
echo "</tr>";
}
// your code
And on signoff.php page, process your form in the following way,
foreach($_POST['dir'] as $dir){
if(!empty($_POST['signoff'][$dir])){
$signoff = $_POST['signoff'][$dir];
// That how you can get $dir and the corresponing $signoff value
}
}
Sidenote: If you want to see the entire $_POST array structure, do var_dump($_POST); on signoff.php page.
when I am trying to get values from checkbox in PHP , I get
this as output, printing $_POST
Array ( [days] => on [submit] => save )
the view code
$days_numbers = explode(',',$user->work_days);
$week = array('Saturday','Sunday' ,'Monday','Tuesday' ,'Wendnesday' ,'Thursday' ,'Friday');
?>
<form method='post' action='' >
<?php
for($i=0 ; $i< count($week); $i++)
{
if(in_array($i,$days_numbers))
{ echo "<input type='checkbox' name='days' checked >" . $week[$i]. "<br/>";
}else
echo "<input type='checkbox' name='days' >" . $week[$i] . "<br/>";
}
?>
Your input element is missing with value attribute
replace
echo "<input type='checkbox' name='days' >" . $week[$i] . "<br/>";
with
echo "<input type='checkbox' name='days' value=".$week[$i]." >" . $week[$i] . "<br/>";
I found that i forget to get write html view like this:
echo "<input type='checkbox' name='days[]' value='$i' checked >" . $week[$i] . "<br/>";
I forget to give value of input so the output was on and also get the value as an array by adding name name="days[] .
while($row = mysqli_fetch_assoc($result))
{
echo "<tr>";
echo "<form action='./form_page.php' method='post'>";
echo "<td style='width:9%'>" . $row['studentID']."</td>";
echo "<td style='width:9%'><input type='submit' name='Prove' value='Approve' /></td>";
echo "<td style='width:9%'><input type='submit' name='$count' value='Delete' /></td>";
echo "</form>";
echo "</tr>";
}
I want get the specific student id, but when I reached to here, I dont know how can I get it. If I put $row['studentID'] for button name, I cant use POST['varname'] beacause the name would be different when I select is different.
Submit buttons are identified by their name.
For e.g. If there are 3 forms with 3 submit buttons named Accept1, Accept2 and Accept3.
When user submits a form by clicking on Accept1, the POST variable will be $_POST['Accept1']. Similarly $_POST['Accept2'], $_POST['Accept3'] respectively.
You can also print POST variable to get a clear idea i.e. print_r($_POST).
If i put $row['studentID'] for button name, i cant use POST['varname']
You can loop over all the values in $_POST and see if any of them match the pattern of a student ID.
Alternatively, you can use a button element (which will let you have display text and a value which don't match each other):
<button name="approve"
value="<?php echo htmlspecialchars($row['studentID'])>
Approve
</button>
and then just test for $_POST['approve'].
Try like this..
while($row = mysqli_fetch_assoc($result))
{
echo "<tr><form action='./form_page.php' method='post'>";
echo "<td style='width:9%'>" . $row['studentID']."</td>";
echo "<input type='hidden' name='studentID' value='".$row['studentID']."'>";
echo "<td style='width:9%'><input type='submit' name='Prove' value='Approve'>;
echo "<td style='width:9%'><input type='submit' name='$count' value='Delete'>
</td></form></tr>";
}
By using input type hidden you will be able to get the student id when submit the from. Hope this will help
I have a for loop which actually displays a product name and several buttons like: Edit, Update , Cancel
For each product i am displaying , it will have its own set of Edir, Update, and Cancel button as below.
Paint Edit Update Cancel
I want to loop through the buttons so that for each category, I can perform a different action. I was thinking about using something like btn_edit1, btn_edit2 for the name of the button and use a for loop. 1, 2 are the category ids.
Maybe Im not clear enough. Sorry for that. Can anyone give me some suggestions?
for($i = 0; $i<count($obj_categories_admin->categories);$i++)
{
echo "<tr>";
echo "<td width='1500'>";
echo "<input type='text' name='name' size = '30' value='" . $obj_categories_admin->categories[$i]['name'] . "'/>";
echo "</td>";
echo "<td width='500'>";
echo "<input type='submit' value = 'Update details' name='submit_update_category_" .
$obj_categories_admin->categories[$i]['category_id'] . "'/>";
echo "</td>";
echo "<td width='500'>";
echo "<input type='submit' value = 'Edit Sub Categories' name='submit_edit_sub_" .
$obj_categories_admin->categories[$i]['category_id'] . "'/>";
echo "</td>";
echo "<td width='500'>";
echo "<input type='submit' value = 'Delete' name='submit_delete_category_" .
$obj_categories_admin->categories[$i]['category_id'] . "'/>";
echo "</td>";
echo "<td width='500'>";
echo "<input type='submit' value = 'Cancel' name='cancel'" . "'/>" ;
echo "</td>";
echo "</tr>";
}
I want to do something like
foreach($_POST as $key => $value)
{
}
so that when i click on a button it performs an action depending on the category_id.
I have tried this as suggested:
echo "<input type='submit' name='submit[add_category]'" .
"[" . $obj_categories_admin->categories[$i]['category_id'] . "]". " value='Add' />";
Now in my class, i have:
$a1 = $_POST['submit'];
$which_action = reset(array_keys($a1));
$which_category = reset(array_keys($a1[$which_action]));
But, i am getting the error : undefined index submit
I would give the name attributes of my submit buttons using following pattern:
name="submit[which_action][which_category]"
For example for your 'Update' button for category 123:
name="submit[update][123]"
When the user clicks any of the submit buttons, to determine which specific button the user has clicked you just need check for $_POST['submit'] in your PHP code:
$a1 = $_POST['submit'];
$which_action = reset(array_keys($a1));
$which_category = reset(array_keys($a1[$which_action]));
Well i would use something like this:
<fieldset>
<!-- product info -->
<input name="productName[paint]" />
<input name="productName[edit]" />
<input name="productName[delete]" />
<input name="productName[cancel]" />
</fieldset>
that way when you get it to the serverside everything will be nice and tidy in nested arrays.