Dynamic web record - php

I'm trying to build a simple app:
Six columns with these values
Nr, Name, checkbox 1, checkbox 2, checkbox 3, checkbox 4.
The checkboxes are project stages. I'm using MySQL to store the data and retrieving it to a webpage with PHP.
My problem is on how to go updating the checkboxes, and also how to filter the results (display only records with checkbox 1 checked).
Or can anyone recommend other solutions/platforms?
This is not overly complex I thought someone must knew a better or easier way to do this.

After your sql query, something like this would work:
if($row['checkbox1'])
echo "<INPUT TYPE=CHECKBOX NAME=checkbox1 CHECKED>";
else
echo "<INPUT TYPE=CHECKBOX NAME=checkbox1>";
Query for filtering:
select * from table where checkbox1 = 1;

<input type='checkbox' name='aname' value='avalue' <?php
if(isChecked($dbrow, $itemname)) // you write this function
{
echo "checked='checked'";
}
?>/>

Related

Answers from Radio buttons and checkboxes to mysql using php

For a survey app which has a combination of radiobuttons and checkboxes. How do I use PHP to insert the selected values to mysql db using php?
This is the query for fetching the questions from db:
while($row2 = $result2->fetch_assoc())
{
if($row2["subtype"] =="radio")
{
echo "<input id = \"radio\" class='radio_input' type=\"radio\" name=answergroup[".$row["PK_QUESTION_ID"].
"] value=".$row2["opt_id"].">".$row2["opt_text"]."</input>";
else if($row2["subtype"] =="checkbox")
{
echo "<input id = \"checkbox\" class='radio_input' type=\"checkbox\" name=answergroup[".$row["PK_QUESTION_ID"].
"] value=".$row2["opt_id"].">".$row2["opt_text"]."</input>";
}
}
I tried using $_POST['answergroup'] for fetching the selected options, but in the case of a checkbox, only one option is being fetched even if there are multiple selections made.
When you are dealing with multiple checkboxes with the same name, in that case the name of checkboxes must be an array like:
<input type="checkbox" name="options[]" value="1">
On form submit you can get the values like:
$options = $_POST['options'];
here $options is an array, so use foreach() to get its elements.

Adding new table column on radio button condition

I have the table below which I have fileld with some default values:
while($records=mysql_fetch_array($result))
{
echo "<tr>";
echo "<td>".$records['item1']."</td>";
echo "<td>".$records['item2']."</td>";
echo "<td>".$records['item3']."</td>";
echo "<td>".$records['item4']."</td>";
echo "<td>".$records['item5']."</td>";
echo "<td>".$records['item6']."</td>";
echo "<td>".$records['item7']."</td>";
echo "</tr>";
}
echo "</table>";
I also have a my_sql query statement that looks like this and references a function which sorts all the data by asc or desc:
$sortBy=$_POST['SortBy'];
$sortIn=$_POST['SortIn'];
$sql="SELECT * FROM Results ORDER BY $sortBy $sortIn";
$result=mysql_query($sql, $conn);
echo "<table>";
All of this works successfully with a bunch of connection statements that I won't include.
I also have a 2 option radio button that looks like this:
Include newItem
<input type="radio" name="IncludeNewItem" value="1" />Yes
<input type="radio" name="IncludeNewItem" value="0" />No
What I am trying to do is set it so that when I submit the user options from the HTML page, the table is generated (and sorted) and depending on the state of the radio button, it should either display or not display another column which already exists in the database. (For examples sake, let's call this 'item 8').
I tried creating something that looks like this:
$includeNI = if(isset($_POST['IncludeNewItem']))
{Alter Table Results Add $newItem };
But I'm really unsure about the syntax as I'm new to this language.
All help is appreciated.

Insert value of checkbox and number input into database

I tried combining two answers from here:
counting how many checkbox are checked php/html
And here:
validating input types "checkbox" and "number" php/html
My goal is to count how many checkboxes were selected, and insert their values along with the number value in the number input.
I found various other posts about it, but couldn't figure out an answer.
Here is the code:
echo '<form action="" method="post">';
Then I have a for loop, where I create my table rows, with the checkboxes.
Where $id = $row[0] is updated on each loop, which is the id equivalent of my entry.
(The output of
echo '<td style="vertical-align: top;">' . $row[0] . '</td>';
Shows the id correctly)
echo '<tr>';
echo "<td style='vertical-align: top;'><input type='checkbox' name='choice[$id][id]' value='$id'></td>";
echo "<td style='vertical-align: top;'><input type='number' name='choice[$id][order]' size='20'></td>";
echo '</tr>';
echo '<b> <input type="submit" value="Insert"></b>';
Then, on the next page, I have this:
$var_checkbox=$_POST['choice'];
$sql_var_id = "SELECT id FROM custom_form WHERE id=(SELECT max(id) FROM custom_form)";
$var_id_result = mysqli_query($link,$sql_var_id);
$var_id = mysqli_fetch_array($var_id_result);
$count=count($var_checkbox[$var_id[0]]);
for($i=0; $i<$count; $i++){
if($var_checkbox[$i]!= NULL){
$sql1 = sprintf("INSERT INTO custom_form_has_property (custom_form_id,property_id,field_order) VALUES ('%d','%d','%d');",
$var_id[0],
$var_checkbox[$var_id[0]][id],
$var_checkbox[$var_id[0]][order]
);
$result1 = mysqli_query($link,$sql1);
}
}
The problem is, that no values of checkboxes or numbers are inserted.
(As a side note, to try and be more specific)
If instead of matrixes, I use
echo "<td style='vertical-align: top;'><input type='checkbox' name='choice[]' value='$row[0]'></td>";
echo '<td style="vertical-align: top;"><input type="number" name="order[]" size="2"></td>';
And
$var_checkbox=$_POST['choice'];
$order = $_POST['order'];
Then
echo "Orders: $order[0],$order[1],$order[2],$order[3],$order[4],$order[5],$ordemr[6]";
echo "Choices: $var_checkbox[0],$var_checkbox[1],$var_checkbox[2],$var_checkbox[3],$var_checkbox[4],$var_checkbox[5],$var_checkbox[6]";
Will output
Orders: 1,,2,,3,,Choices: 1,3,13,,,,
I need the choiced from the checkboxes to be somehow linked to the orders from the number field. And the only way to achieve that, is if they have the same name, but different indexes, which is why I have the matrix, in the first index, it's saved the id number, which is generated on each loop, and in the second, the actual name, which makes the distinction between them (id and order).
I tried various other things, but it all comes back to the same problem...
The value of order (number) is stored into the array, even when null, while the value of choices (checkbox) doesnt.
I could do an array_filter(), but there is no guarantee that the user will not input an order, while not ticking a choice checkbox.
If I would compare the length of choice with the length of order, after filtered, and if they output the same size, there is still no guarantee, that the user didnt check checkbox of line x, and added a value in the order field, on line y.
Maybe there is a way to pass unchecked values to the choice[] variable as null, the same way it happens in order[]?
What is this? choice[.$id.] ? what are the dots for?
I believe this is the bug!
You probably were trying to concatenate $id into the string before and just forgot to remove them?
Corrected line:
echo "<td style='vertical-align: top;'><input type='checkbox' name='choice[$id][id]' value='$id'></td>";
there are some way to do this ..
you have to count total no of selected check box via jQuery and set those value in hidden field at view page on each check box check or on form submit
you have to take counter on insert part and save the ids and at last update ids value with counter value in database ....

Trying to copy data to a table with checkboxes, a form, and php

My webpage is pulling data from two tables - applications and archiveapps - and displays them using the following:
$sql = "SELECT * FROM applications";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
echo "Lots of info, including some html thrown in for style."
I want users to be able to click a checkbox listed next to each row and then hit an "Archive Selections" button that then moves all the selected entries from the applications table to the archiveapps one.
So far I've tried a form with it's code half in the echo above (so that a checkbox would go by each listed row from the $sql query) with the submit button outside (so that there would only be one "Archive Selections" button) but I'm sure this isn't proper syntax.
To actually move the data, I had this for the checkbox:
<form method='post' action=''><input type='checkbox' name='archname' value=".$row["charname"].">
(The above was inside an echo statement, so I assume it was able to pull the $row["charname"] no problem, but am unsure how to verify that.)
A little further down the page is the submit button and </form>.
And then I've got the function I want it to run when the submit button is clicked, to check if boxes have been selected and copy them into the archiveapps table. I'm sure there's something I'm missing here, probably in referencing what exactly is selected, and then copying that row's data to the other table... but honestly I just don't know enough about php to know what I'm missing.
if(!empty($_POST['archname'])) {
foreach($_POST['archname'] as $check) {
function archiveapp() {
$sqli="INSERT INTO archiveapps SELECT * FROM applications";
if ($conn->query($sqli) === TRUE) {
echo "<i>Archived</i>";
} else {
echo "Error: " . $sqli . "<br>" . $conn->error;
}}}}
Most of this has just been gathered from google searches and kind of mushed together, so I'm sure there are a lot of things done wrong. Any pointers or advice would be greatly appreciated!
Oh and my end goal is to copy the data to the archiveapps table and then delete it from the applications table, but for now I've just been focusing on the copying part, since I assume deleting a row will be fairly simple? Either way it's not the priority for this question.
Thanks in advance for any help!
Use a tool like FireBug to see what you are actually posting to server. You have multiple checkboxes with the same name, so you are sending only 1 value (the last checked, I think).
You need to send all of them, so use brackets after the name:
<!-- this is just an example -->
<input type="checkbox" name="archname[]" value="1">
<input type="checkbox" name="archname[]" value="2" checked>
<input type="checkbox" name="archname[]" value="3" checked>
Then on PHP, inside $_POST['archname'] you will get this:
Array(2, 3)
After this, you can do a foreach(...) loop like you are already doing, inserting only the ID you get from the $_POST variable:
foreach($_POST['archname'] as $id){
$sqli = "INSERT INTO archiveapps (id) VALUES (".(int)$id.")";
}

Checking the value of two checkboxes and entering a corresponding value into a database PHP

So what I am trying to achieve is I have two checkboxes. One is for employee completion and the other for manager. The action I want when BOTH are checked (have a value of 2) is to check the the third checkbox named full complete and to enter a value of 2 into the database.
So far I have the following, however, the synax must be wrong because it will not allow me to have this && because it is boolean.
if($row['employee_complete'] == 2) && ($row['manager_complete'] == 2) {
echo "<td>"."<input type = 'checkbox' checked='checked' name ='fullcomplete' value=
'fullcomplete'/>"."</td>";
}
else
{
echo "<td>"."<input type = 'checkbox' name ='fullcomplete' value= 'full complete'/>"."
</td>";
}
This also does not enter any value into the database, do I need to perform a full insert SQL query?
Things to perhaps consider:
-The checkboxes are echoed in a table inside an associative array.
Thank you.

Categories