Keep the check mark after form is submitted - php

I have 2 forms that they need to be used to make a report. The first form gets data from the database (MariaDB) onchange of one of the cells (the on change events happens when the user checks the cell with the report) once that the cell is selected with the details of the report, those details are used on submit by the second form. Until that point everything is perfect. The problem is that I can't keep the check mark on the cell of the first form once that it is selected because the page is reloaded onchange. That makes the user believe that they didn't select any report.
So, what I want to do is to keep checked the cell of the first form after it gets the data from the database. Then, the user will click on "Submit" to submit the second form.
Kind regards and thanks in advance for your help!
<form name="f1" id="form1" action="" method="post">
<div class="table-responsive">
<table class="table table-striped">
<thead>
<tr>
<th>Select</th>
<th>Assets </th>
<th>Description </th>
<th>Date</th>
<th>Action</th>
</tr>
</thead>
<tbody>
<?php
while($agency_row = $agency_stmt->fetch(PDO::FETCH_ASSOC)){
$value1 = str_replace(' ',' ',str_pad($agency_row["agency"], -10));
$value2 = str_replace(' ',' ',str_pad($agency_row["report_name"], 70));
$formatedDate = date("D M d H:i:s Y", $agency_row["scan_end"]);
$value3 = str_replace(' ',' ',str_pad($formatedDate, 20));
echo "<tr>";
echo "<td><input onchange='f1.submit()'; type='checkbox' class='i-checks' name='agency' value='" . $value1 . ":" . $agency_row["report_name"] . ":" . $agency_row["scan_start"] . ":" . $agency_row["scan_end"] . "'></td>";
echo "<td>$value1</td>";
echo "<td>$value2</td>";
echo "<td>$formatedDate</td>";
echo "<td>$value3</td>";
echo "</tr>";
}?>
</tbody>
</table>
</form>
<form name="f2" id="form2" action="report.php" method="post">
<input type="hidden" name="agency" value="<?php echo "$agency";?>">
<input type="hidden" name="report_name" value="<?php echo "$report_name";?>">
<input type="hidden" name="scan_start" value="<?php echo "$scan_start";?>">
<input type="hidden" name="scan_end" value="<?php echo "$scan_end";?>">
<input type="hidden" value="yes" name="isPlugName">
<input type="hidden" value="yes" name="isPlugFam">
<input type="hidden" value="yes" name="isPlugInfo">
<input type="hidden" value="yes" name="isSynopsis">
<input type="hidden" value="yes" name="isDescription">
<input type="hidden" value="yes" name="isSolution">
<input type="hidden" value="yes" name="isSeeAlso">
<input type="hidden" value="yes" name="isPlugOut">
<input type="hidden" value="plugin" name="byVuln">
<input type="hidden" value="yes" name="isCvss">
<input type="hidden" value="yes" name="isVulnPub">
<input type="hidden" value="yes" name="isExploit">
<input type="hidden" value="yes" name="isCve">
<input type="hidden" value="yes" name="isBid">
<input type="hidden" value="yes" name="isOsvdb">
<input type="hidden" value="yes" name="isCert">
<input type="hidden" value="yes" name="isIava">
<input type="hidden" value="yes" name="isCWE">
<input type="hidden" value="yes" name="isMS">
<input type="hidden" value="yes" name="isSec">
<input type="hidden" value="yes" name="isEdb">
<input type="hidden" value="yes" name="isAffected">
<input type="hidden" value="yes" name="isService">
<input type="hidden" value="4" name="critical">
<input type="hidden" value="3" name="high">
<input type="hidden" value="2" name="medium">
<input type="hidden" value="1" name="low">
<input type="submit" name="submithost" value="submit">
</form>

Change
echo "<td><input onchange='f1.submit()'; type='checkbox' class='i-checks' name='agency' value='" . $value1 . ":" . $agency_row["report_name"] . ":" . $agency_row["scan_start"] . ":" . $agency_row["scan_end"] . "'></td>"
to
$checked=(isset($_REQUEST['agency']) && $_REQUEST['agency'] == $value1) ? ' checked' : '';
echo "<td><input onchange='f1.submit()'; type='checkbox' class='i-checks' name='agency' value='" . $value1 . ":" . $agency_row["report_name"] . ":" . $agency_row["scan_start"] . ":" . $agency_row["scan_end"] . $checked ."'></td>"

Related

How to check multiple radio buttons in array loop in php

I have fetched data and show in the radio buttons while the radio buttons are in loop, and insert the data in loop too.
The problem is that I can check only one radio button from the form, for each question one radio button must be check, which I can't.
<form method="POST" class="form-horizontal">
<?php
$count=1;
$que="SELECT * FROM addques WHERE quz_id='$var'";
$dbd=mysqli_query($conn,$que);
while ($cmd=mysqli_fetch_array($dbd)) {
$quest=$cmd['qusname'];
$ans_id=$cmd['ans_id'];
$opt1=$cmd['qpta'];
$opt2=$cmd['optb'];
$opt3=$cmd['optc'];
$opt4=$cmd['optd'];
$answ=$cmd['answer'];?>
<b>Question <?php echo $count++;?> :<br><?php echo $quest;?></b><br><br>
<fieldset>
<input type="hidden" name="ansid[]" value="<?php echo $ans_id; ?>">
<input type="radio" name="ans[]" value="1"><?php echo $opt1;?><br><br>
<input type="radio" name="ans[]" value="2"><?php echo $opt2;?><br><br>
<input type="radio" name="ans[]" value="3"><?php echo $opt3;?><br><br>
<input type="radio" name="ans[]" value="4"><?php echo $opt4?><br><br><br>
</fieldset>
<?php } ?>
<button type="submit" name="submit" class="btn btn-primary">Submit</button>
</form>
</div>
</div>
</div>
<?php
$i=0;
if (isset($_POST['submit'])) {
while ( $i<$noquestions ) {
$query="INSERT INTO `result`( `quz_id`, `ans_id`, `answer`) VALUES('";
$query.=$var . "', '";
$query.=$_POST['ansid'][$i] . "', '";
$query.=$_POST['ans'][$i] . "' )";
$db=mysqli_query($conn,$query);
$i++;
}
}
?>
The reason why you're getting that bug is you have given name="ans[]" so each time the index value incrementing.
You can fix it by giving the following code
<input type="radio" name="ans" value="1">
<input type="radio" name="ans" value="2">
And so on.
You have been using radio button inside a loop so you can use it by setting a loop variable like name="ans[$i]"
echo '<input type="radio" name="ans['.$i.']" value="1">
<input type="radio" name="ans['.$i.']" value="2">';

PHP multiple checkbox array with different values - Get total price of checked checkboxes

How to calculate sum of PHP multiple checkboxes values array - Total Price of checked checkboxes?
For example, result should be displayed like:
Total Price of Selected Programming Languages : C++,Java = 1200$
<form method="post" action="#">
0<input type="checkbox" name="count[]" id="count[]" value="0"/>
<input type="hidden" name="language[]" id="language" value="C"/>C [$800]
<input type="hidden" name="price[]" id="price" value="800"> <br/>
1<input type="checkbox" name="count[]" id="count[]" value="1"/>
<input type="hidden" name="language[]" id="language" value="C++"/>C++ [$700]
<input type="hidden" name="price[]" id="price" value="700"> <br/>
2<input type="checkbox" name="count[]" id="count[]" value="2"/>
<input type="hidden" name="language[]" id="language" value="Assembler"/>Assembler [$600]
<input type="hidden" name="price[]" id="price" value="600"><br/>
3<input type="checkbox" name="count[]" id="count[]" value="3"/>
<input type="hidden" name="language[]" id="language" value="Java"/>Java [$500]
<input type="hidden" name="price[]" id="price" value="500"> <br/>
4<input type="checkbox" name="count[]" id="count[]" value="4"/>
<input type="hidden" name="language[]" id="language" value="PHP"/>PHP [$400]
<input type="hidden" name="price[]" id="price" value="400"> <br/>
<input type="submit" name="sbt" id="sbt" value="SUBMIT">
</form>
This is the PHP code:
<?php
if(isset($_POST['sbt'])){
$count = $_POST['count'];
$sub_menu = $_POST['sub_menu'];
$sub_price = $_POST['sub_price'];
$sub_price1 = $_POST['sub_price'];
//$total_price = array($sub_menu => $sub_price);
foreach($count as $j)
echo $sub_menu[$j] . '['.$sub_price[$j]. ']' ;
}
?>
$items = array();
$total = 0;
foreach($_POST['price'] as $k => $price) {
if(in_array($k, $_POST['count'])) {
$items[] = $_POST['language'][$k];
$total += intval($price);
}
}
$items = implode(", ", $items);
echo $items . " = $" . $total;
You need to make sure the array indexes are the same for each group:
0<input type="checkbox" name="count[0]" value="0"/>
<input type="hidden" name="language[0]" value="C"/>C [$800]
<input type="hidden" name="price[0]" value="800"> <br/>
1<input type="checkbox" name="count[1]" value="1"/>
<input type="hidden" name="language[1]" value="C++"/>C++ [$700]
<input type="hidden" name="price[1]" value="700"> <br/>
Then you can get the price and language for each count:
$price = array_intersect_key($_POST['price'], $_POST['count']);
$language = array_intersect_key($_POST['language'], $_POST['count']);
Then implode the text and sum the price:
echo "Total Price of Selected Programming Languages: "
. implode(',', $language) . ' = $'
. array_sum(price);

How to echo 0, When not check checkbox?

How to echo 0, When not check checkbox ?
When press submit button, It's will echo 11
But i want to echo 101 (echo 0 for not checked ckeckbox).
How can i do that ?
<form id="form02" name="form02" method="post">
<input name="h_type[]" type="checkbox" id="h_type[]" value="0" checked/><input type="hidden" name="more[]" value="1"> <br/>
<input name="h_type[]" type="checkbox" id="h_type[]" value="1" /><input type="hidden" name="more[]" value="1"> <br/>
<input name="h_type[]" type="checkbox" id="h_type[]" value="2" checked/><input type="hidden" name="more[]" value="1"> <br/>
<input type="submit" name="button" id="button" value="Submit" />
</form>
<?php
$data_h_type=$_POST['h_type'];
$data_more=$_POST['more'];
if(count($data_h_type)>0){
foreach($data_h_type as $key=>$value){
echo $data_more[$value];
echo "<br>";
}
}
?>
echo isset($data_more[$value]) ? $data_more[$value] : 0;

Checkbox only returns first value

I´m creating a form with checkboxes. But somehow I only get the first result back..
Hope you can help!
<strong>My form </strong>
<form action="historie.php" method="post">
<?php
// ophalen van bijbehorende producten
$sql_product_opzoek = "SELECT * FROM product_bestellingen WHERE bestelnummer = $bestelnummer_opzoek";
$sql_p_opzoek = mysqli_query($con, $sql_product_opzoek);
$count = 0;
while ($row = mysqli_fetch_array($sql_p_opzoek)) {
$artikelnr_retour = $row['artikelnr'];
$merk_retour = $row['merk'];
$artikelnr_lev_retour = $row['artikelnr_lev'];
$kleur_retour = $row['kleur'];
$maat_retour = $row['maat'];
$prijs_retour = $row['prijs'];
if ($count === 0) {
$sql_opzoek = "SELECT * FROM bestellingen WHERE bestelnummer = $bestelnummer_opzoek";
$sql_b_opzoek = mysqli_query($con, $sql_opzoek);
while ($row = mysqli_fetch_array($sql_b_opzoek)) {
$tent = $row['tent'];
}
echo $tent;
}
echo "
<input type=\"checkbox\" name=\"retour[" . $artikelnr_retour . "]\" value=\"" . $artikelnr_retour . "\">
<input type=\"hidden\" name=\"merk\" value=\"" . $merk_retour . "\">
<input type=\"hidden\" name=\"art_lev\" value=\"" . $artikelnr_lev_retour . "\">
<input type=\"hidden\" name=\"kleur\" value=\"" . $kleur_retour . "\">
<input type=\"hidden\" name=\"maat\" value=\"" . $maat_retour . "\">
<input type=\"hidden\" name=\"prijs\" value=\"" . $prijs_retour . "\">
<input type=\"hidden\" name=\"bestelnummer\" value=\"" . $bestelnummer_opzoek . "\">
";
if ($count === 0) {
echo "<input name=\"submit\" type=\"submit\">";
}
echo "</form>";
$count++;
}
This results in the next HTML
<strong>HTML</strong>
<form action="historie.php" method="post">
<tr>
<td>654655</td>
<td>Huggo Boss</td>
<td>xwsmcdD</td>
<td> 13</td>
<td>45</td>
<td>€ 99,95</td>
<td>123456</td>
<td>
<input type="checkbox" name="retour[654655]" value="654655">
<input type="hidden" name="merk" value="Huggo Boss">
<input type="hidden" name="art_lev" value="xwsmcdD">
<input type="hidden" name="kleur" value=" 13">
<input type="hidden" name="maat" value="45">
<input type="hidden" name="prijs" value="99,95">
<input type="hidden" name="bestelnummer" value="987654">
</td>
<td><input name="submit" type="submit"></td>
</tr>
</form>
<tr>
<td>100254</td>
<td>Maripe</td>
<td>Stun</td>
<td> 66</td>
<td>33</td>
<td>€ 295,95</td>
<td></td>
<td>
<input type="checkbox" name="retour[100254]" value="100254">
<input type="hidden" name="merk" value="Maripe">
<input type="hidden" name="art_lev" value="Stun">
<input type="hidden" name="kleur" value=" 66">
<input type="hidden" name="maat" value="33">
<input type="hidden" name="prijs" value="295,95">
<input type="hidden" name="bestelnummer" value="987654">
</td>
<td></td>
</tr>
</form>
To get the results I use the next PHP
PHP to get results
foreach ($_POST['retour'] as $value) {
echo $value;
}
This results only in the first result: 654655
Hope you can help!
Your form is close befor the second check box. Please try this code:-
<strong>HTML</strong>
<form action="historie.php" method="post">
<tr>
<td>654655</td>
<td>Huggo Boss</td>
<td>xwsmcdD</td>
<td> 13</td>
<td>45</td>
<td>€ 99,95</td>
<td>123456</td>
<td>
<input type="checkbox" name="retour[654655]" value="654655">
<input type="hidden" name="merk" value="Huggo Boss">
<input type="hidden" name="art_lev" value="xwsmcdD">
<input type="hidden" name="kleur" value=" 13">
<input type="hidden" name="maat" value="45">
<input type="hidden" name="prijs" value="99,95">
<input type="hidden" name="bestelnummer" value="987654">
</td>
<td></td>
</tr>
<tr>
<td>100254</td>
<td>Maripe</td>
<td>Stun</td>
<td> 66</td>
<td>33</td>
<td>€ 295,95</td>
<td></td>
<td>
<input type="checkbox" name="retour[100254]" value="100254">
<input type="hidden" name="merk" value="Maripe">
<input type="hidden" name="art_lev" value="Stun">
<input type="hidden" name="kleur" value=" 66">
<input type="hidden" name="maat" value="33">
<input type="hidden" name="prijs" value="295,95">
<input type="hidden" name="bestelnummer" value="987654">
</td>
<td></td>
</tr>
<input name="submit" type="submit">
</form>
There is no need to use <input type="checkbox" name="retour[654655]" value="654655">
Just use array. i.e.
<input type="checkbox" name="retour[]" value="654655">
<input type="checkbox" name="retour[]" value="100254">
This will give you $retour[0]=654655 and $retour[1]=100254
If you check both of the boxes, then both values should come through $_POST. If you only check one of the boxes, it only sends that value through $_POST.
Example:
One checkbox checked:
Both checkboxes checked:
Solution:
Try the following to make sure there is something in the $_POST at all times:
<input type="hidden" name="retour[654655]" value="0">
<input type="checkbox" name="retour[654655]" value="1">
If the value of $_POST['654655'] equals 1, it is present. If it equals 0 it is not present. The value of a checkbox will not be submitted as long as it is not checked.With the solution presented above the value of the hidden field will be overridden when the checkbox is checked.

Get radio button data from mysql in input form

I've already searched and I don't understand why this isn't working.
<input type="radio" name="soort" value="in" echo ('.$_GET['soort'].'=="in")?"checked":"">In-Company<br />
<input type="radio" name="soort" value="open" echo ('.$_GET['soort'] . '"=="open")?"checked":">Open inschrijving<br />
This isn't working either:
<input type="radio" name="soort" value="in" echo ($soort=="in")?"checked":"">In-Company<br />
<input type="radio" name="soort" value="open" echo ($soort=="open")?"checked":">Open inschrijving<br />
Clearly I'm doing something wrong or I'm missing something.
If anyone could help me out here or put me in the right direction. Thank you!!!!
This is the entire form.
print '<form action="edit_dienst.php" method="post">
<p><strong>Titel</strong> <textarea name="navigatie" columns="20" rows="5">' . $row['navigatie'] . '</textarea></p>
<p><strong>Tekst</strong> <textarea name="tekst" columns="20" rows="5">' . $row['tekst'] . '</textarea></p>
<input type="radio" name="soort" value="in" echo ($soort=="in")?"checked":"">In-Company<br />
<input type="radio" name="soort" value="open" echo ($soort=="open")?"checked":">Open inschrijving<br />
<input type="hidden" name="id" value="' . $_GET['id'] . '" />
<input type="submit" name="submit" value="Pas aan!" />
</form><p></p>';
<?php $soort=$_GET['soort']; ?>
<input type="radio" name="soort" value="in" <?php echo ($soort=="in")?"checked":"" ?>>In-Company<br />
<input type="radio" name="soort" value="open" <?php echo ($soort=="open")?"checked":"" ?>>Open inschrijving<br />
since i see 'print', i infer that you are already in the php tag. you can the below script.
$soortInChecked = ($soort=="in")?"checked":"";
$soortOpenChecked = ($soort=="open")?"checked":"";
print '<form action="edit_dienst.php" method="post">
<p><strong>Titel</strong> <textarea name="navigatie" columns="20" rows="5">' . $row['navigatie'] . '</textarea></p>
<p><strong>Tekst</strong> <textarea name="tekst" columns="20" rows="5">' . $row['tekst'] . '</textarea></p>
<input type="radio" name="soort" value="in" '.$soortInChecked.' >In-Company<br />
<input type="radio" name="soort" value="open" '.$soortOpenChecked.' >Open inschrijving<br />
<input type="hidden" name="id" value="' . $_GET['id'] . '" />
<input type="submit" name="submit" value="Pas aan!" />
</form><p></p>';
the issue is that you have given the echo inside the quotes. check the tutorials for print/echo also the difference in single and double quotes in php to start with.

Categories