I have an error that I can't figure out...
Om my webpage there is a form that the user has the ability to add some new input fields to. If the user is submitting the form, then the optional fields is empty when the php-file is handing them, why?
HTML:
<form method="post" action="newRequest.php">
<input type="text" name="title" />
<input type="hidden" name="fname" value="0" />
<input type="checkbox" name="fname" value="1"/>
<input type="hidden" name="ename" value="0" />
<input type="checkbox" name="ename" value="1" />
<input type="hidden" name="seat" value="0" />
<input type="checkbox" name="seat" value="1" />
<input type="hidden" name="fields" value="0" />
<input type="text" id="fields" name="fields" />
<input type="submit" />
</form>
PHP:
if (strlen($_POST[title]) > 2) {
$toDb[title] = $_POST[title];
} else {
error('title');
}
$toDb[fname] = $_POST[fname];
$toDb[ename] = $_POST[ename];
$toDb[seat] = $_POST[seat];
if ($_POST[fields] > 0) {
$i = 0;
while ($i < $_POST[fields]) {
$toDb[optional][$i] = $_POST[optional-$i];
$i++;
}
$toDb[optional] = serialize($toDb[optional]);
} else {
$toDb[optional] = 0;
}
newEvent($toDb,$dbh);
JQuery that is adding dynamical fields:
$(document).ready(function() {
$('#fields').focusout(function(){
var fields = $('#fields').val();
var i = 0;
while(i < fields) {
$('#fields').after("Valfritt fält "+(i+1)+":<input type='text' name='optional"+i+"' />");
i++;
}
})
})
You should quote array indexes. It should be
$toDb['optional'][$i] = $_POST['optional'.$i];
You are missing commas in $_POST
$toDb['fname'] = $_POST['fname'];
$toDb['ename'] = $_POST['ename'];
$toDb['seat'] = $_POST['seat'];
Here is your modified code
if (strlen($_POST['title']) > 2) {
$toDb['title'] = $_POST['title'];
} else {
error('title');
}
$toDb['fname'] = $_POST['fname'];
$toDb['ename'] = $_POST['ename'];
$toDb['seat'] = $_POST['seat'];
if (count($_POST) > 0) {
$i = 0;
while ($i < count($_POST)) {
$toDb['optional'][$i] = $_POST['optional-'.$i];
$i++;
}
$toDb['optional'] = serialize($toDb['optional']);
} else {
$toDb['optional'] = 0;
}
newEvent($toDb,$dbh);
Also use count() to check if $_POST has values > 0.
I faced the same problem and I solved it using Javascript, like this :
add a new text field every time a button is pressed
Related
for example, I receive two inputs value1 and value2 and I want this input for different functions. Like addition, subtraction and multiplication.
my code
<?php
$x = $_POST['fnum'];
$y = $_POST['snum'];
if (isset($_POST['add'])) {
$sum = $x + $y;
echo "Result:<input type='text' value='$sum'/>";
}
if (isset($_POST['sub'])) {
$sub = $x - $y;
echo "Result:<input type='text' value='$sub'/>";
}
if (isset($_POST['mul'])) {
$mul = $x * $y;
echo "Result:<input type='text' value='$mul'/>";
}
<body>
<form method="post">
Enter first number <input type="text" name="fnum" />
<hr />
Enter second number <input type="text" name="snum" />
<hr />
<input type="submit" name="add" value="ADD" />
<input type="submit" name="sub" value="Subtract" />
<input type="submit" name="mul" value="Multiply" />
</form>
</body>
In this it is asking me to feed input for each operation separately
Good, Just use the posted value in your form input like -
Enter first number <input type="text" name="fnum" value="<?php echo #$_POST['fnum'];?>"/><hr/>
Enter second number <input type="text" name="snum" value="<?php echo #$_POST['snum'];?>"/><hr/>
So that user don't need to put the same value again and when press the other buttons the form will automatically submit with the previous values.
Note: Remember, you need to check all the posted value is set or not and use proper conditions of POST method. Have a look at the given example of your problem as solution, I give it here to give you a proper guide.
Example:
<?php
$x = 0;
$y = 0;
if(isset($_POST['submit'])) {
$x = $_POST['fnum'];
$y = $_POST['snum'];
$operator = "";
if($_POST['submit'] == 'ADD') {
$operator = "+";
} elseif($_POST['submit'] == 'Subtract') {
$operator = "-";
} elseif($_POST['submit'] == 'Multiply') {
$operator = "*";
}
$result = $x . $operator . $y;
}?>
Your form will be-
<form method="post">
Enter first number <input type="text" name="fnum" value="<?php echo $x;?>"/><hr/>
Enter second number <input type="text" name="snum" value="<?php echo $y;?>"/><hr/>
<input type="submit" name="submit" value="ADD"/>
<input type="submit" name="submit" value="Subtract"/>
<input type="submit" name="submit" value="Multiply"/>
</form>
Result:
<input type='text' value='<?php echo (isset($result) ? $result : "-";)?>'/>
I have got the php, html code.
And I want to post the multiple checkbox values, but this does not work dunno why, I can print count or array, it prints(0), does not matter
the array values is always empty
<form action = 'main.php?w=creatNewTemplate2' method = 'post'>
<input type = 'text' name = 'templateName' maxlength = '30'/><br />
<input type= 'checkbox' name= 'exercises[]' value='A' />A<br />
<input type= 'checkbox' name= 'exercises[]' value='B' />B<br />
<input type = 'submit' value = 'Sukurti'/>
</form>
if($w == "creatNewTemplate2")
{
$d = $_POST['exercises'];
$ddd = count($d);
print_r($_POST);
}
I think this could work for you:
if($w == "creatNewTemplate2")
{
$d = $_POST['exercises'];
$ddd = count($d);
for ($x = 0; $x < $ddd; $x++) {
echo $_POST['exercises'][$x].'<br>';
}
}
If you use same name you need to loop them
This should be work
Or you can change the two checkbox to select multiple
<form action="main.php?w=creatNewTemplate2" method="post">
<input type="text" name="templateName" maxlength="30"/><br/>
<select multiple name="exercices[]">
<option>A</option>
<option>B</option>
</select>
<input type="submit" value="Sukurti"/>
</form>
<?php
if($_GET["w"] == "creatNewTemplate2")
{
foreach ($_POST["exercises"] as $ex) {
echo $ex . '<br>';
}
}
I am trying to check whether any of the radio buttons on a web page have been selected when using PHP. I have searched around a lot and tried multiple things but nothing has seemed to work. I am trying to return a value of false to the variable $check so that later on after multiple of these checks I can stop the form from submitting.
HTML:
<form id="Quiz" method="post" action="markquiz.php" novalidate="novalidate">
<fieldset>
<legend>What does the 'S' in OWASP stand for?</legend>
<p><label for="Superman"><input type="radio" name="Q1" value="Superman" required="required"/>Superman</label>
<label for="Segregation"><input type="radio" name="Q1" value="Segregation"/>Segregation</label>
<label for="Security"><input type="radio" name="Q1" value="Security"/>Security</label>
<label for="Simple"><input type="radio" name="Q1" value="Simple"/>Simple</label>
</p>
</fieldset>
<p>
<input id ="submit" type="submit" value="Submit"/>
<input type="reset" value="Reset"/>
</p>
</form>
PHP:
//Q1
if (isset($_POST["Q1"])){
$AnsQ1 = $_POST["Q1"];
if ($AnsQ1 == "Security") {
$Score = $Score + 1;
}
else {
$check = false;
}
Thanks!
Try this and do add name="submit" in your button
if (isset($_POST["submit"])){
$AnsQ1 = $_POST["Q1"];
if ($AnsQ1 != "") {
$Score = $Score + 1;
}
elseif($AnsQ=="") {
$check = false;
}
if(isset($_POST['Submit'])) {
$AnsQ1 = $_POST["Q1"];
if ($AnsQ1 == "Security") {
$Score = $Score + 1;
} else {
$check = false;
}
}
I have a excerpt of my form, as follows:
// If logged in shows this box (it's backed behind a include php file that makes the login check
<form action="./saveList.php" method="post" id="ListaComprasForm">
<div class="submitButtonEncap">
<button type="submit" method="POST" name="submitButton" class="btn btn-primary roundedBorder" form="ListaComprasForm" value="submit">
<span class="glyphicon glyphicon-floppy-disk" aria-hidden="true"></span> Save List
</button>
</div>
<input type="text" id="Limpeza0" name="Limpeza[]" class="listInput" autocomplete="off" maxlength="50">
<input type="checkbox" class="x" id="L_chck_0" name="L_chckb_[]" value="check"/>
<input type="text" id="Limpeza1" name="Limpeza[]" class="listInput" autocomplete="off" maxlength="50">
<input type="checkbox" class="x" id="L_chck_1" name="L_chckb_[]" value="check"/>
<input type="text" id="Limpeza2" name="Limpeza[]" class="listInput" autocomplete="off" maxlength="50">
<input type="checkbox" class="x" id="L_chck_2" name="L_chckb_[]" value="check"/>
<input type="text" id="Limpeza3" name="Limpeza[]" class="listInput" autocomplete="off" maxlength="50">
<input type="checkbox" class="x" id="L_chck_3" name="L_chckb_[]" value="check"/>
<input type="text" id="Limpeza4" name="Limpeza[]" class="listInput" autocomplete="off" maxlength="50">
<input type="checkbox" class="x" id="L_chck_4" name="L_chckb_[]" value="check"/>
<input type="text" id="Limpeza5" name="Limpeza[]" class="listInput" autocomplete="off" maxlength="50">
<input type="checkbox" class="x" id="L_chck_5" name="L_chckb_[]" value="check"/>
<input type="text" id="Limpeza6" name="Limpeza[]" class="listInput" autocomplete="off" maxlength="50">
<input type="checkbox" class="x" id="L_chck_6" name="L_chckb_[]" value="check"/>
</form>
This form, on submit, will submit to a DB wether or not checkboxes and TextBox fields have been filled or not. The checkboxes that have been checked or not, he'll submit them over as Y and N or nullo for the Textboxes respectively through some sorting code as follows:
if (isset($_POST['submitButton'])){
$L_chck[] = array();
$H_chck[] = array();
$V_chck[] = array();
$F_chck[] = array();
$Cong_chck[] = array();
$Cons_chck[] = array();
$Outros_chck[] = array();
$Outros = array();
for ($counter = 0; $counter <= 6; $counter++){
$L_chck[$counter] = (isset($_POST['L_checkb_'.$counter]) ? 'Y' : 'N');
$H_chck[$counter] = (isset($_POST['H_checkb_'.$counter]) ? 'Y' : 'N');
$V_chck[$counter] = (isset($_POST['V_chckb_'.$counter]) ? 'Y' : 'N');
$F_chck[$counter] = (isset($_POST['F_chckb_'.$counter]) ? 'Y' : 'N');
$Cong_chck[$counter] = (isset($_POST['Cong_chckb_'.$counter]) ? 'Y' : 'N');
$Cons_chck[$counter] = (isset($_POST['Cons_chckb_'.$counter]) ? 'Y' : 'N');
$Outros_chck[$counter] = (isset($_POST['Outros_chckb_'.$counter]) ? 'Y' : 'N');
if (empty($_POST['Outros'.$counter])) {
$Outros[$counter] = $_POST['Outros'.$counter] = 'nullo';
} else {
$Outros[$counter] = $_POST['Outros'.$counter];
}
}
for ($i=0; $i <= 4; $i++) {
/*if ((isset($_POST['Limpeza'.$i])) == false) {*/
if (empty($_POST['Limpeza'.$i])) {
$Limpeza[$i] = $_POST['Limpeza'.$i] = 'nullo';
} else {
$Limpeza[$i] = $_POST['Limpeza'.$i];
}
if (empty($_POST['Higiene'.$i])) {
$Higiene[$i] = $_POST['Higiene'.$i] = 'nullo';
} else {
$Higiene[$i] = $_POST['Higiene'.$i];
}
if (empty($_POST['Vegetais'.$i])) {
$Vegetais[$i] = $_POST['Vegetais'.$i] = 'nullo';
} else {
$Vegetais[$i] = $_POST['Vegetais'.$i];
}
if (empty($_POST['Fruta'.$i])) {
$Fruta[$i] = $_POST['Fruta'.$i] = 'nullo';
} else {
$Fruta[$i] = $_POST['Fruta'.$i];
}
if (empty($_POST['Congelados'.$i])) {
$Congelados[$i] = $_POST['Congelados'.$i] = 'nullo';
} else {
$Congelados[$i] = $_POST['Congelados'.$i];
}
if (empty($_POST['Conservas'.$i])) {
$Conservas[$i] = $_POST['Conservas'.$i] = 'nullo';
} else {
$Conservas[$i] = $_POST['Conservas'.$i];
}
}
This data is then passed on as arguments to a function to be serialized and sent to DB fields, all of them varchar(255):
savingListData($L_chck, $H_chck, $V_chck, $F_chck, $Cong_chck, $Cons_chck, $Outros_chck, $Limpeza, $Higiene, $Vegetais, $Fruta, $Congelados, $Conservas, $Outros);
So far, every piece of data, being it checked/filled or not, it transposes over to the DB as null. In the case of the checkboxes, N, and in the case of the Textboxes nullo.
I'm led to believe that something is wrong in the sorting but I'm puzzled and can't figure out why. Would really appreciate some help!!
EDIT: Added more of the form in a shortened manner. (It's a big form!)
I think you are trying to retrieving it using wrong name. Its L_chckb_[]
<input type="checkbox" class="x" id="L_chck_0" name="L_chckb_[]" value="check"/>
So you need to use the loop counter outside the name key like this:
$L_chck[$counter] = (isset($_POST['L_chckb_'][$counter]) ? 'Y' : 'N');
The problem is: the name attribute in the tag is L_chckb_ but you are trying to retrieve it by L_checkb_ so that is the issue.
Because L_checkb_ is not set and so it is giving you N.
I have a list a checkboxes with accompanying input text fields. If the user checks a box, the accompanying text field will be add to an array.
I am new to PHP and was wondering if anyone can help me in the right direction.
Should I use a for loop, foreach, while, unique "name" for each input, or something else?
Below is what I have so far.
<?php
if(isset($_POST['submit'])){
$array = array();
while(isset($_POST['check'])){
if(!isset($_POST[$some_text]) || empty($_POST[$some_text])){
echo "Please include your text for each checkbox you selected.";
exit();
}
$array[] = $_POST['some_text];
}
}
?>
<form>
<input type="checkbox" name="check"><input type="text name="some_text">
<input type="checkbox" name="check"><input type="text name="some_text">
<input type="checkbox" name="check"><input type="text name="some_text">
<!-- I might have around 100 of these -->
<!-- submit button here -->
</form>
You first need a way to associate your checkboxes with their corresponding text fields, and a way to tell them apart. For example:
<form>
<input type="checkbox" name="check[]" value="1"><input type="text name="text1">
<input type="checkbox" name="check[]" value="2"><input type="text name="text2">
<input type="checkbox" name="check[]" value="3"><input type="text name="text3">
</form>
Now you can loop through it as follows:
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
if (isset($_POST['check']) && is_array($_POST['check']) && !empty($_POST['check'])) {
$array = array();
foreach ($_POST['check'] as $value) {
$text_field = 'text' . $value;
if (!isset($_POST[$text_field]) || trim($_POST[$text_field]) == '') {
echo "Please include your text for each checkbox you selected.";
exit;
}
$array[] = $_POST[$text_field];
}
}
}
Try this:
$select = array();
foreach($_POST['check'] as $key => $selected){
$select[$key] = $selected;
}
Please try this:
<?php
if(isset($_POST['submit'])){
for($i=0; $i<100; $i++)
{
if($_POST['check_'.$i])
{
$array[] = $_POST['input_'.$i];
}
}
}
?>
#################### HTML
<form method="post" action="">
<?php for($i=0; $i<100; $i++) { ?>
<input type="checkbox" name="check_<?php echo $i ?>"><input type="text" name="input_<?php echo $i ?>">
<?php } ?>
<input type="submit" name="submit">
</form>