Hi I have a check box dynamically created as follows;(not the complete code here) there are a number of check boxes created, hence name is an array. (Is it right the way I have started the array inside <td></td>?)
while ($rec = mysqli_fetch_array($result))
{
echo "<tr>";
echo "<td class='tbldecor1'><input type='checkbox' name='delete[]' value='$Val1'></td>";
echo "</tr>";
}
I need to get the user selected check box values to a loop in order to delete the records selected. If I forget about the deleting part of it, to get the user selected values to a loop I used something like below;
$AA = $_POST['delete'];
while($AA == 'checked')
{
echo $AA; // trying to print the user checked options so that I can subsequently code to delete them.
}
but seems an erroneous approach! Can someone please give me an idea?
foreach($_POST['delete'] AS $key => $val) {
...
}
checkboxes which aren't checked in the form aren't submitted, so automatically you'll only get the checked checkboxes in the _POST data.
it's input field array which gives you all checked value into array in $_POST, so use following way
if(isset($_POST['delete'])) {
foreach($_POST['delete'] as $value){
echo $value;
//do stuff
}
}
Related
I have a bootstrap table which has a form in it, with each row containing a column where the value can be changed from a drop-down box. On clicking the 'Save changes' button, all the rows will be updated with the new values.
The form/table works as intended in normal cases. But if I use the search functionality of the bootstrap table to filter out a few of the rows and then try to update rows with the values, the wrong rows are getting affected.
So from the example in the above image, if I filter out to view just the second row like in the picture below, then the changes, or the 'Update' query is executed on the actual first row, that is to the row which had 'Bob' as the 'technician'.
I'd like to know how to solve this issue.
Here is the relevant code:
foreach($tickets as $tickets)
{
$users = $app['database']->selectAll('users');
echo ("<input type='text' style = 'display:none' value = '$tickets->id' name = 'ticketid[]'>");
echo "<td class = '$technician->color'>$technician->name</td>";
echo "<td>";
echo '<select name = "user[]" id="user" class="form-control">';
echo "<option value = $technician->id>$technician->name</option>";
foreach($users as $users)
{
echo "<option value = $users->id>$users->name</option>";
}
echo "</select>";
echo "</td>";
echo "</tr>";
}
For the database part, I am calling a function transferTask which accepts first the table name, then two arrays, one array containing updated usernames from the dropdown fields and one containing corresponding ids.
transferTask('tickets', $user[$i], $ticketid[$i])
The above function is executed for each row in the table. I think it's an issue wit the name array being passed from the form to this function but I'm not sure. Help is appreciated!
I created a table in HTML, and I'm inserting all the information from a specific database table, into this HTML table using a foreach loop.
I created a button called delete, to delete a specific row from the database, but I'm having some problems because I don't know how I can delete the table information using a foreach loop, because I always use a while statement.
I'm doing a kind of MVC structure, but with my own rules, so I got the model and the view, I'm calling all the functions at view.
And I'm doing something like this on view:
function fname($array) {
foreach($array as $key) {
echo $key['row1'];
echo "<input type='submit' value='delete'";
}
}
And I made my function on model:
function function() {
connect();
$show = ("SELECT * FROM mytable");
$array = db_array($show, 'v');
fname($array);
}
Can you guys help me with an example? Thanks.
Create a form inside the loop for each row i.e. if it prints out 10 rows it means 10 different forms. While submitting form (clicking delete), send the row id as hidden parameter to the controller (in action) while in turns send it to model. Model has the delete query which gets execute and the row gets deleted.
View -
foreach($array as $key)
{
$row_id = $key['row_id'];
echo "<form action='/deleterow' method='POST'>"
echo $key['row1'];
echo "<input type='submit' value='delete'>";
echo "<input type='hidden' name='id' value=$row_id>";
echo "</form>";
}
Controller -
function deleterow($id)
{
$this->model_name->delete($id);
}
Model -
function delete($id)
{
$sql = "DELETE FROM table_name WHERE id=$id";
$exe = mysql_query($sql) or die(mysql_error());
}
I would try this
function fname($array){
foreach($array as $key=>$val){
echo $val['FIELD'];
echo '<input name="Z" type="hidden" value="'.$key.'" />';
echo '<input type="submit" value="Delete"/>';
}
}
I am assuming you are going to make a page where someone can delete something?
You need to echo out the recode name? (this depends on what you are pulling out of the table and fields involved)
The when the delete button is pressed you can act upon hidden field Z
I've 10 question and each have 4 options, i want to check radio values, whether it is correct or not with the result stored in database when a user submit the form.
thanks in advance...
while($row = mysqli_fetch_array($result)){
foreach( $_POST as $key=>$val){
if($row['answer'] == $val){ echo "Correct";}
else{ echo "Incorrect";
}
#broken_heart is right. you should provide your code. then some body will guide you.... anyway, this is the one way.. try once...
Steps:
assign different names with auto increment for each radio button group. name should be like this radio_name[$i] .... here $i is auto increment...
submit the form
Get value like this..
$radio_values=$_POST['radion_name'];
for($i=0;$i<count($radio_values);$i++)
{
$radio_value= $radio_values[$i];
}
Basically what I am looking to do is populate a dropdown box from the contents of a mysql_array into a dropdown box...after the user makes a selection and submits the form the dopdown box will not contain the selection they just used....This form will have to be submitted mutiple times...I can get it to work no problem if it was just submitted once...so for multiple times i was trying to put what they had already used into an array and then compare that against the mysql_array so the already selected items wont appear again....this is what i have and i might be way off...
$_SESSION['name']=array();
array_push($_SESSION['name'],$_POST['selEmpl']);
function e(){
$sql="select name from IMSemploy ORDER BY name ASC";
$results=mysql_query($sql) or die(mysql_error());
echo"<select name=\"selEmpl\" size=\"1\">";
?><option value="Select">Select</option> <?
foreach ($_SESSION['name'] as $value){
while ($row = mysql_fetch_array($results)){
if($row['name']!=$value){
echo'<option value="'.$row['name'].'">'.$row['name'].'</option>';
}
}
}
echo'</select>';
}
So you want to hide the values that were already clicked?
You need to loop through the sql results set just once and don't display the values that are already stored in the session:
while ($row = mysql_fetch_array($results))
{
if (array_search($row['name'], $_SESSION['name']) === false)
{
echo'<option value="'.$row['name'].'">'.$row['name'].'</option>';
}
}
Apart from that you are destroying your $_SESSION['name'] variable every time you run the script, you need to set it to an empty array only the first time:
session_start();
if (!isset($_SESSION['name']))
{
$_SESSION['name']=array();
}
Remove the foreach loop and test for this condition before displaying an option element
if (!in_array($row["name"], $_SESSION["name"])) {
echo'<option value="'.$row['name'].'">'.$row['name'].'</option>';
}
I am a newbie in php. I have a form with multiple checkbox values. I wanna retrieve the checked values and diplay these values on other php form.
Below is the code which works perfectly fine if we add the checkboxes without while loop.
But when added through while loop I am not able to fetch the selected items.
xyzhtml.php(html form)
<?PHP
require ("DBConnect.php");
$selectQuery =mysql_query( "SELECT * FROM fruits where approved = 0");
while($row = mysql_fetch_array($selectQuery))
{
$fruit_name = $row['fruit_name'];
echo "<input type=\"checkbox\" name=\"things[]\" value=\"$fruit_name\">";
echo "<br>";
}
?>
On click of submit I call other php clled "xyz.php".
Below is the code used in it.
<?php
$checkBox = $_POST['things'];
echo $checkBox[0];
for($i=0; $i<sizeof($checkBox); $i++){
echo($checkBox[$i]);
}
?>
Please help.
Thanks in advance.
two things to check:
are you getting right values from the MySQL SELECT statement (check your HTML for empty checkboxes values and check your MySQL database for fruits that have approved field set to 0 to see if there are any)
when you don't tick a checkbox and submit the form, unticked checkboxes' values do not get submitted - have you thought about that?
Use foreach to record $_POST['things']
if (!empty($_POST['things'])) {
foreach ($_POST['things'] as $value) {
echo "the value are: ".$value;
}
}
Note: teh $_POST['things'] not $_POST['things[]']
I think your code should work, but it won't display anything if no checkboxes are selected.
I would however remove the explicit echo $checkbox[0] (unless this is for testing purposes).
Try a print_r($checkbox) to see what's actually in that array.