I have a program where I am populating the check box, once I click on the submit button, I am inserting the data into DB and I want to retain the checkbox state as checked for the values that I have inserted, so that when a user logs in again he can see the items he has checked the previous time. This is my code
<?php
$accQry = "select * from brands";
$result1 = mysql_query($accQry, $db1->conn);
$i = 1;
while ($row = mysql_fetch_assoc($result1)) {
?>
<label> <input type="checkbox" value="<?php echo $row['brand_id']; ?>" name="chk1[]" ><?php echo $row['brand_name']; ?></label>
<?php $i++;
}
?>
This is the code to populate and I don't know how to go about it further.
Checkbox has attribute checked, which should be set when you want to check it - how checkbox works
Related
I have the following codes to show dynamic checkboxes.
while($result = mysqli_fetch_array($query)){
$oaName = $result['oaName'];
echo '<input class="checkBoxes" type="checkbox" name="checkBoxArray[]" value="'.$oaName.'" style="float:left;"'; ?> <?php if(isset($_POST['checkBoxArray'])) echo "checked='checked'"; ?> <?php echo '>'; ?>
}
I want to retain status of only those checkboxes that I checked as checked after form submission. But with the above codes, all checkboxes are showing checked after form submission. Does anyone know what am i doing wrong here?
Edit 1
checkBoxArray[] are checkboxes names which are getting from database
After a few trials, I am able to get it done by the following way. The key here is the use of in_array
while($result = mysqli_fetch_array($query)){
$oaName = $result['oaName'];
echo '<div class = "checkbox-group" required ><input class="checkBoxes" type="checkbox" name="checkBoxArray[]" value="'.$oaName.'" style="float:left;"'; ?> <?php if(in_array($oaName,$_POST['checkBoxArray'])) echo "checked='checked'"; ?> <?php echo '></div>
}
I am trying to increment project number based on the last entry. The the primary key PROJECTNOID auto-increments but is not the same format as the project number (Ex: PROJECTNOID = 1 and Project Number = 19000). I don't want this to be a dropdown box even though some of my code shows the opposite.
<?php
connect = mysqli_connect("**", "**", "**", "**");
$query4 = "SELECT PROJECTNOID, ProjectNumber FROM tblProjects ORDER BY
PROJECTNOID";
$result4 = mysqli_query($connect,$query4);
$options4 = "";
while($row4 = mysqli_fetch_row($result4);){
$options4 = $options4."<input value=$row4[0]$row4[1]</input>";
}
?>
Here is the html textbox:
<label for="txtfield">Project Number</label>
<!--<input type="text" id="reqtxtfield" name="projectnumber"
value="<?php ?>" readonly/>-->
<?php echo $options4;?>
But it would look like how you had it but instead of '1' inside the
box it would display '19000' and there would be nothing outside of the
box other than the label "Project Number". As far as i'm aware you can
assign a value to the text box, regardless of whatever the input is. I
would like it to display the value from one field name but actually
contain the value from a different field name. Both are in the same
table of course.
OK - gotcha. Unfortunately, you cannot do that. A textbox can only have one value and the user is always free to change that value, even if you make it read-only. You can test that out by using the developer toolbar in your browser. Probably a good time to mention that all user input should be considered dangerous and you should never trust it. Once they have submitted the form you need to verify it.
What I would recommend in your case is to use a hidden <input> which contains the value you actually want to submit; projectnoid. You can then display the Project Number in any manner you choose.
<form>
<h1>Project Number: 19000</h1>
<input type="hidden" name="projectnoid" value="1">
<input type="submit" name="submit">
</form>
To generate this, you would:
<?php
while($row4 = mysqli_fetch_row($result4)){
$projectnoid = $row[0];
$projectNumber = $row[1];
echo '<h1>' . $projectNumber . '</h1>';
echo '<input type="hidden" name="projectnoid" value="'. $projectnoid .'">
}
PHP:
<?php
$query_5 = "SELECT MAX(ProjectNumber) FROM tblProjects;";
$result_5 = mysqli_query($conn, $query_5);
$row_5 = mysqli_fetch_array($result_5);
$nextproject=$row_5['MAX(ProjectNumber)']+1;
?>
HTML:
<html>
<form class="myform" action="<?php echo htmlspecialchars($_SERVER[" PHP_SELF "]);?>" method="post">
<label for="txtfield">Project Number</label>
<input type="text" id="reqtxtfield" name="projectnumber" value="<?php echo $nextproject ?>" readonly/><br>
After running successful insert query:
echo "<meta http-equiv='refresh' content='0'>"; //REFRESH PAGE TO UPDATE PROJECT NUMBER
I am currently doing up a list of names that has signed up for a activity which is then needed to go through another selection process and to be put into the database. I have currently done the getting and displaying of names from the people that has signed up.
However, I am not too sure on the process of how I can get those checked which is being selected in the selection process.
This is currently the codes that I have done to get and display the names of the people who have signed up. Upon clicking submit, it would go to another page which would then process, get those checked and marked as 1 or true to the database (MySQL) or either set as 0 or false if it has not been checked.
<?php
if ($totalShortlist > 0) {
?>
<?php
while ($row = mysqli_fetch_assoc($result)) {
$firstName = $row['first_name'];
$lastName = $row['last_name'];
?>
<form id="selectionProcess" action = "doShortlistProcess.php?id=<?php echo $id ?>" method = "post">
<fieldset data-role="controlgroup">
<label><input type="checkbox" name="selectionProcess" id="selectionProcess" value="<?php echo $id ?>"/><?php echo $firstName; ?> <?php echo $lastName; ?></label>
</fieldset>
<?php
}
?>
<input type="submit" class="btnSelect" data-theme="b" data-inline="true" name="shortlistCandidate" value="Submit Shortlisted Candidates"/>
</form>
<?php
}
// end for loop
else {
echo "No candidates to be shortlisted";
}
mysqli_close($link);
?>
The $id as shown is the brought over from the list of categories on the previous page, which allows to get all the names in that categories to be displayed.
Your code places the form in the wrong place compared to the while loop. The action also doesn't need you to compose a query string. Better code (careful, I've also cut out some presentation):
if ($totalShortlist > 0) {
?>
<form id="selectionProcess" action = "doShortlistProcess.php" method = "post">
<fieldset>
<?php
// use while loop to display a tick box for each item
while ($row = mysqli_fetch_assoc($result)) {
$firstName = $row['first_name'];
$lastName = $row['last_name'];
?>
<label>
<input type="checkbox" name="selectionProcess" id="selectionProcess" value="<?php echo $id ?>"/>
<?php echo $firstName; ?> <?php echo $lastName; ?>
</label>
<?php
} // end of while loop
?>
</fieldset>
<input type="submit"/>
</form>
<?php
} // end of shortlist > 0
I recommend you test this code using the GET method. It will show you what form the submission takes.
To process the result, you should use the list of responses, process it and turn it into one or more SQL statements.
An alternative would be to use an action for each checkbox, but then use ajax to a php page that would modify the status of single subscriber. The change would be made immediately as users click.
i have 2 checkbox that deliver the different value..
<input type="hidden" name="position[]" value="<?php echo $excheck['check_id']; ?>">
<input name="position[]" class="checkbox" type="checkbox" <?php if($pos == $poss) echo 'checked = "checked"'; ?> value="<?php echo $eq['pos_unit_name']; ?>">
its for determined whether it is checked or not..
if it not checked , then the value is still delivered which is the check_id..
the problem is, if another checkbox is checked, and the current checkbox is remain, how do i add the code?
this is currently my code..
$check = $_POST['position'];
foreach ($check as $id)
{
if(is_numeric($id))
{
$up = mysql_query("delete from checklist where check_id = $id");
}
}
what i want to accomplish is , if its unchecked, the data is deleted based on the check_id and if another checkbox is checked , insert a new data, and if the checked box is remain checked, then it just updated the value based on the check_id
First need to use unique name for each form fields. you have two fields name as position so use different name
and query var should be quoted like
$up = mysql_query("delete from checklist where check_id = '$id'");
What you want to do in this case is to add the identifier as the key:
<input name="position[<?php echo $excheck['check_id']; ?>]" value="1" />
And then:
foreach ($_POST['position'] as $id => $checked) {
// do stuff with $id
}
I am making a form and one of the inputs are a number of checkboxes that come from a database. If the form isn't filled out correctly, the user should be returned to the form but with the selected checkboxes should still be checked once a user returns.
The page containing the form uses an if, if the user made a mistake, he/she receives a form that contains sessions so the stuff that was filled in is still there. The other form doesn't have sessions.
This is the code I use to read out the checkbox options from the database:
$STH = $DBH -> prepare("SELECT * FROM tbloptions");
$STH -> execute();
$STH -> setFetchMode(PDO::FETCH_OBJ);
while($row = $STH -> fetch())
{
$optionID = $row -> optionID;
$option = $row -> option;
?>
<input type="checkbox" name="<?php echo $option ?>" value="<?php echo $optionid ?>"> <?php echo $option ?><br>
<?php
}
?>
what code should I use for the form with the sessions and what code should I use to create the sessions
thanks
Adding square braces to the checkbox name creates an array of the selected values in the group
<input type="checkbox" name="chk_group[]" value="<?php echo $optionid ?>" /><?php echo $option ?><br />
This can be retrieved in PHP with a simple loop:
<?php
if (isset($_POST['chk_group'])) {
$optionArray = $_POST['chk_group'];
for ($i=0; $i<count($optionArray); $i++) {
echo $optionArray[$i]."<br />";
}
}
?>
Next, you can redisplay the form with the "checked" arg if the option is in the $optionArray array
This depends how you process your form variables. The following assumes no processing and that the data is POSTed.
Determine if the value of checkbox "name" is set to "value" and if so, check it:
<input type="checkbox" name="<?php echo $option ?>" value="<?php echo $optionid ?>" <?php if($_POST[$option]==$optionid){echo' checked="checked"';} ?>> <?php echo $option ?>
...