How to delete many records at a stretch? - php

In Php as we all know, there are no inbuilt controls by itself like Asp.Net's GridView etc. I am using Html's <table> to build up the grid and keep the row's id in one hidden field. I've also placed one checkbox at the beginning of each row and a delete button at the bottom of the grid. The problem i face is, how do i get all the id's that are checked so that i can pass those ids in my IN clause of Delete?

Take a look at what is currently being submitted:
var_dump($_POST);
You will see all of the field values. If you do the checkboxes right, you'll have an array of rowID's to delete, and you can simply implode(',',$_POST['checkBoxes']) or something similar when building your query.
Security would be a concern here... I'm sure someone else will post in depth about that, but you definitely want to validate that the user can delete these records.

Name every checkbox with a semi-unique name like tablerow[numeric_id]. When you submit the form you can simply catch all posted tablerow value that was checked.

First you name all your checkboxes by suffixing [] in their name so that an array gets created, later this is how you can get those that are checked and act accordingly:
for($i = 0; $i < count($_POST['checks']); $i++)
{
if (isset($_POST['checks'][$i]))
{
// this was checked !!
}
}
Where checks is the name of all those checkboxes eg:
<input type="checkbox" name="checks[]" value="1" />
<input type="checkbox" name="checks[]" value="2" />
<input type="checkbox" name="checks[]" value="3" />
And this is how you can get those for your IN clause in your query:
$checked_array = array();
for($i = 0; $i < count($_POST['checks']); $i++)
{
if (isset($_POST['checks'][$i]))
{
$checked_array[] = mysql_real_escape_string($_POST['checks'][$i]);
}
}
// build comma separated string out of the array
$values = implode(',', $checked_array);
Now you can use the $values in your IN clause of your query.

Related

How to have checkbox items create a list

I am new to PHP and the project I am working on is a codeigniter php form writing back to a FileMaker database.
The checkboxes should be populating as a list field.
Here is my form
<input name="fieldname[]" value="value1"<?php echo (set_checkbox('fieldname', $join->fieldname))?> type="checkbox" class="form-check-input"> value1
<input name="fieldname[]" value="value2"<?php echo (set_checkbox('fieldname', $join->fieldname))?> type="checkbox" class="form-check-input"> value2
There are about 7 checkboxes.
I assume that I have to setup a foreach loop to get it to go thought to FileMaker's field, but unsure how to do that.
If I take away the array, the last one selected goes through to the layout.
Any help would be great.
Presumably, these values mirror a valueList back in FileMaker. If so, it's best to assign the valueList to a variable:
$values = $layout->getValueList('pizzaToppings');
And use that in your for loop:
foreach($values as $value)...
In your if(isset()) block, post a return-delimited array to FileMaker:
// value(s)!
if ($_POST['fieldname']) {
$valueArray = implode("\r", $_POST['fieldname']);
}
// pass the array to setField()
$record->setField('filemakerField', $valueArray);
I worked it out. To enter checked boxes into a return separated list:
View:
At the top of the section with the checkboxes
<?php $join->fieldname = explode("\n", $join->fieldname); ?>
// The actual input in the form
<input name="fieldnameXX[checkboxValue]" value="value" <?php echo (in_array('value', set_value('fieldname[value]', $join->fieldname)) ? ' checked="checked"': '')?> type="checkbox" class="form-check-input" > Value
Factory:
$fieldnameZZ = $data['fieldnameXX'];
$data['fieldnameXX'] = FALSE;
unset($data['fieldnameXX']);
$sacraments = implode("\n", $fieldnameZZ);
$data['fieldname'] = $fieldnameZZ;
I'm not sure if this is the best way to have done it, but it works.
$ids=implode(",", $_REQUEST["fieldname"]);
$result3=mysqli_query($dbh,'SELECT* FROM excel_tenant WHERE ID IN ("' .
$ids . '") AND
ManagerID = "'.$_SESSION["ManagerID"].'" ORDER BY ID DESC ') or
die(mysqli_error($dbh));

Checkbox Values stored in DB and want to display

Please see this link
http://thedesigningworld.com/bea
Here's a Small form contains 8-9 fields + a group of checkboxes
I want to save all details in DB + want to display in a table in proper manner, but it not works properly
Here's the code which i used
for($i=0;$i<count($_POST[wert1]);$i++)
{
if($_POST[wert1][$i]!= "")
{
$check1[] =$_POST['wert1'][$i]; } }
$new1=implode(',', $check1);
$result = "INSERT into table1(check1) values($new1)";
$result = mysqli_query($con, $result);
So i've one doubt that for each checkbox row, should i need to define same array name or different like here i used array name as wert1[] for first row
Checkbox values are not transmitted if the box is not checked.
If you have influence, you could put a hidden input field of the same name before the checkbox and the value "0", like:
<input type="hidden" name="checkbox_name" value="0" />
<input type="checkbox" name="checkbox_name" value="1">Some Text</input>
In you example site, you're using array notation, which is basically a good thing. However, you have not given an index so you might not recognize missing elements.

counting number of checkboxes check in email

Here is my php code:
$tasks = ' ';
$help = $_POST['help'];
if(empty($help))
{
$tasks = "None selected.";
}
else
{
$N = count($help);
$tasks = $N;
}
And the HTML is:
<input type="checkbox" name="help" value="sign"> //with several inputs with different values
On the form submit, it emails and outputs everything appropriately except the count of the array. It outputs the $tasks variable at the end of the email always as 1, except when no check boxes are selected. Any combination of selecting checkboxes (1-6) ends up with an array of 1 length. Anyone know why? Thanks!
You'll need to make the checkboxes an array. Change the name to:
<input type="checkbox" name="help[]" value="sign">
You should change your HTML code to:
<input type="checkbox" name="help[]" value="sign">
so that help will be an array. If you only use help, $_POST['help'] will only contain the last value.
you have to rename fields name="help[]" so it can be parsed as array.

PHP avoiding a long POST

This is more of a technique question rather than maybe code. I am having a php form with many fields (items to select). Naturally some of the items might be selected and some not. How do I know which ones are selected when i post the data from page 1 to page 2? I thought of testing each one if empty or not, but there are just too many fields and it doesn't feel at all efficient to use or code.
Thanks,
UPDATE EDIT:
I've tried the following and maybe it will get me somewhere before I carry on testing the repliers solutions...
<html>
<body>
<form name="test" id="name" action="testprocess.php" method="POST">
<input type="text" name="choices[shirt]">
<input type="text" name="choices[pants]">
<input type="text" name="choices[tie]">
<input type="text" name="choices[socks]">
<input type="submit" value="submit data" />
</form>
</body>
</html>
and then second page:
<?php
$names = $_POST['choices'];
echo "Names are: <br>";
print_r($names);
?>
This gives out the following:
Names are: Array ( [shirt] => sdjalskdjlk [pants] => lkjlkjlk [tie]
=> jlk [socks] => lkjlkjl )
Now what I am going to try to do is iterate over the array, and since the values in my case are numbers, I will just check which of the fields are > 0 given the default is 0. I hope this works...if not then I will let you know :)
I think what you're looking for is this:
<form action="submit.php" method="POST">
<input type="checkbox" name="checkboxes[]" value="this" /> This
<input type="checkbox" name="checkboxes[]" value="might" /> might
<input type="checkbox" name="checkboxes[]" value="work" /> work
<input type="submit" />
</form>
And then in submit.php, you simply write:
<?php
foreach($_POST['checkboxes'] as $value) {
echo "{$value} was checked!";
}
?>
The square brackets in the name of the checkbox elements tell PHP to put all elements with this name into the same array, in this case $_POST['checkboxes'], though you could call the checkboxes anything you like, of course.
You should post your code so we would better understand what you want to do.
But from what I understood you are making a form with check boxes. If you want to see if the check boxes are selected, you can go like this:
if(!$_POST['checkbox1'] && !$_POST['checkbox2'] && !$_POST['checkbox3'])
This looks if all the three check boxes are empty.
Just an idea:
Create a hidden input field within your form with no value. Whenever any of the forms fields is filled/selected, you add the name attribute of that field in this hidden field (Field names are saved with a comma separator).
On doing a POST, you can read this variable and only those fields present in this have been selected/filled in the form.
Hope this helps.
Try this.....
<?php
function checkvalue($val) {
if($val != "") return true;
else return false;
}
if(isset($_POST['submit'])) {
$values = array_filter(($_POST), "checkvalue");
$set_values = array_keys($values);
}
?>
In this manner you can get all the values that has been set in an array..
I'm not exactly sure to understand your intention. I assume that you have multiple form fields you'd like to part into different Web pages (e.g. a typical survey form).
If this is the case use sessions to store the different data of your forms until the "final submit button" (e.g. on the last page) has been pressed.
How do I know which ones are selected when i post the data from page 1 to page 2?
is a different question from how to avoid a large POST to PHP.
Assuming this is a table of data...
Just update everything regardless (if you've got the primary / unique keys set correctly)
Use Ajax to update individual rows as they are changed at the front end
Use Javascript to set a flag within each row when the data in that row is modified
Or store a representation of the existing data for each row as a hidden field for the row, on submission e.g.
print "<form....><table>\n";
foreach ($row as $id=>$r) {
print "<tr><td><input type='hidden' name='prev[$id]' value='"
. md5(serialize($r)) . "'>...
}
...at the receiving end...
foreach ($_POST['prev'] as $id=>$prev) {
$sent_back=array( /* the field values in the row */ );
if (md5(serialize($sent_back)) != $prev) {
// data has changed
update_record($id, $sent_back);
}
}

PHP multiple radio buttons

how can i process all radio buttons from the page?
<input type="radio" name="radio_1" value="some" />
<input type="radio" name="radio_1" value="some other" />
<input type="radio" name="radio_2" value="some" />
<input type="radio" name="radio_2" value="some other" />
this buttons will be added dynamically so i will not know the radio buttons name(and also the number of the buttons). Is there a way to get all radio values, with a for loop or something like this? thanks
Use a foreach loop
<?php
foreach ( $_POST as $key => $val )
echo "$key -> $val\n";
?>
$key will be the name of the selected option and $val, well, the value.
Since the browser will just change all your input to HTTP-formatted form data, you won't be able to tell what data is from a radio button versus a text box or other input.
If the naming convention is the same as your example, just loop until you don't find a value:
<?
for ($idx = 1; $idx <= 1000; $idx++) {
if (isset($_REQUEST["radio_$idx"])) {
// handle value
}
}
?>
EDIT Alternatively, if your form is generated dynamically, you could write the number of radio buttons it created as a hidden field in the form.
If you are able to alter the form that is being generated, you could write a hidden input that provided a list of all the radio buttons that you want to look for. As you are writing the radio buttons, just make a semi-colon-separated list of all the names that you make. When you are done, write that to a hidden input. Something like this:
On the source form:
<input type="hidden" name="radio_button_list" value="some_value;other_value" />
Then in your handler:
<?
$list = explode(';', $_REQUEST['radio_button_list']);
foreach ($list as $name) {
$value = $_REQUEST[$name];
// handle name & value
}
?>
jheddings' example says it all. However, you will never get the names / values of all buttons - just the selected one from each group. If you need literally all values, you will have to use Javascript.

Categories