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.
Related
I have made a table with editable fields and i have to add functionality to the update button. I have looked up many posts but cant seem to figure out how it works.
Heres what i have at this moment:
<?php
if (isset($_GET['update'])) {
$query = mysql_query("UPDATE iekartas SET
iernosauk='$Ierices_Nosaukums', tips='$Tips', razotajs='$Razotajs',
izgatdat='$Izgatavosanas_datums', adrese='$Adrese' where id='$Iekartas_ID'", $connection);
}
$query = mysql_query("select * from iekartas", $connection);
while ($row = mysql_fetch_array($query)) {
echo "<b><a href='iekartas.php?update={$row['Iekartas_ID']}'>{$row['Ierices_Nosaukums']}</a></b>";
echo "<br />";
}
?>
<tr>
<form action="ierices.php" method="post">
<td><input class="checkbox" type="checkbox" id="<?php echo $row['Iekartas_ID'] ?>" name="id[]"></td>
<td> <input type="text" name="iernosauk" value=" <?php echo $row["Ierices_Nosaukums"]; ?>"></td>
<td> <input type="text" name="tips" value=" <?php echo $row["Tips"]; ?>"></td>
<td> <input type="text" name="razotajs" value=" <?php echo $row["Razotajs"]; ?>"></td>
<td> <input type="text" name="izgatdat" value=" <?php echo $row["Izgatavosanas_datums"]; ?>"></td>
<td> <input type="text" name="adrese" value=" <?php echo $row["Adrese"]; ?>"></td>
<td> <input type="hidden" name="id" value=" <?php echo $row["Iekartas_ID"]; ?>"></td>
<?php
$i++;
}
?>
<button type="button" action="update.php" name="updatedb" class="btn btn-danger" id="updatedb" value=update>Atjaunot</button>
</tr>
</form>
Button is client side so your "update.php" won't be triggered when you click on button. What you have to do is change your button type to submit so your form will actually send a POST request when you click on the button and it will call the action on the form (ierices.php)
<form action="ierices.php" method="post">
...
<button type="submit" class="btn btn-danger" id="updatedb">Atjaunot</button>
</form
I currently have an edit form, where all values are echoed from the db as the "values" for each input, the user would then retype and "save" the value to append to the db. All echoes work but i have been unable to echo the database value for the options... anybody knows how to?
& Thanks
Code below:
<fieldset>
<form id="input-form" method="POST" action="../php/edit-import-record.php">
Airway Bill Number*:
<input type=text name=AwbNo size=30 class="input" value="<?php echo $awb ?>" required>
Client Code:
<?php //OPEN DROP DOWN BOX
include("../login/dbinfo.inc.php");
$comm=#mysql_connect(localhost,$username,$password);
$rs=#mysql_select_db($database) or die( "Unable to select database");
$sql= "SELECT DISTINCT ClientCode, ClientName FROM tbl_client ORDER BY ClientCode";
$result = mysql_query($sql);
echo "<select name='ClientCode'>";
while ($row = mysql_fetch_array($result)) {
echo "<option value='" . $row['ClientCode'] ."'>" . $row['ClientName'] ."</option>";
}
echo "</select><br>"; //CLOSE DROP DOWN BOX
?><br>
Vessel Name*:
<input type=text name=VesselName class=form-control id=inputSuccess size=30 class="input" value="<?php echo $vsl ?>" required>
Number of Pieces:
<input type=number name=Pieces size=30 class="input" value="<?php echo $pcs ?>" >
Total Weight (kg):
<input type=number name=Weight size=30 class="input" value="<?php echo $wgt ?>" >
Carrier:
<input type=text name=Carrier size=30 class="input" value="<?php echo $car ?>" >
Sender:
<input type=text name=Sender size=30 class="input" value="<?php echo $snd ?>" >
Status:
<input type=text name=Status size=30 class="input" value="<?php echo $stt ?>" >
Arrival Date:
<input type=date name=ArrivalDate size=30 class="input" value="<?php echo $ard ?>" >
Customs:
<input type=text name=Customs size=30 class="input" value="<?php $ctm ?>" >
<br><small>Fields marked with * are required to be filled in.</small>
<div class="inputformbutton">
<button type="reset" class="btn btn-default btn-sm">Reset</button>
<button type="submit" class="btn btn-primary btn-sm">Create Record</button>
</div>
</fieldset>
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);
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>"
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.