Php not receiving data from form - php

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.

Related

Insert only populated fields into MySQL

im trying to insert only populated ID fields to MySQL, meaning if two out of three are populated, only two to be inserted. I got blind with many code lines, and can't see where Im making a mistake.
Logic is that I'll search for the serial number, once selected it will populate row ID from the serial number into the ID field.
I want to pass ID field value to the database. However, below code is submitting all ID fields even the empty ones (where serial number is not selected), and I dont need to pass records for not populated ID field.
Where am I making a mistake?
Thanks
$idCount = count($_POST['assetsn_id']);
echo $idCount;
for($i=0; $i < $idCount; ++$i) {
$assetsn_id = $_POST['assetsn_id'][$i];
$assetsn_location_address = $_POST['assetsn_location_address'];
$assetsn_location_rack = $_POST['assetsn_location_rack'];
$assetsn_location_shelf = $_POST['assetsn_location_shelf'];
$assetsn_location_bin = $_POST['assetsn_location_bin'];
$assetsn_location_createdby = $_SESSION['user']['id'];
$assetsn_location_userlogid = $_SESSION['user']['userlogid'];
$sql = "INSERT INTO asset_serial_locations SET assetsn_id=?, assetsn_location_address=?, assetsn_location_rack=?, assetsn_location_shelf=?, assetsn_location_bin=?, assetsn_location_createdby=?, assetsn_location_userlogid=?";
$result = modifyRecord($sql, 'sssssss', [$assetsn_id, $assetsn_location_address, $assetsn_location_rack, $assetsn_location_shelf, $assetsn_location_bin, $assetsn_location_createdby, $assetsn_location_userlogid]);
if ($result) {
$_SESSION['success_msg'] = "Location updated successfully!";
header("location: " . BASE_URL . "workshop/location/");
exit(0);
} else {
$_SESSION['error_msg'] = "Something went wrong. Could not update locations.".'<br />'.mysqli_error($conn);
}
}
Serial number #1
<input class="form-control form-control-lg" type="search" name="" autocomplete="off" placeholder="search for serial number..." />
<input class="form-control form-control-lg" type="text" name="assetsn_id[]" autocomplete="off" placeholder="ID" /><br /><br />
Serial number #2
<input class="form-control form-control-lg" type="search" name="" autocomplete="off" placeholder="search for serial number..." />
<input class="form-control form-control-lg" type="text" name="assetsn_id[]" autocomplete="off" placeholder="ID" /><br /><br />
Serial number #3
<input class="form-control form-control-lg" type="search" name="" autocomplete="off" placeholder="search for serial number..." />
<input class="form-control form-control-lg" type="text" name="assetsn_id[]" autocomplete="off" placeholder="ID" />
Use array_filter to remove nulls values from an array :
$_POST['assetsn_id'] = array_filter($_POST['assetsn_id']);
$idCount = count($_POST['assetsn_id']);
echo $idCount;
for($i=0; $i < $idCount; ++$i) {
....
}
Here what array_filter can remove :
$a = array(0, '0', NULL, FALSE, '', array());
var_dump(array_filter($a));
// array()

how to use the single input received multiple times in php

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 : "-";)?>'/>

Adding Query HTML/PHPMYADMIN

Okay, so i really need help about the add query. I don't know what i did wrong. Please help. This is the code for my update.php
<form action="updateprocess.php" method="POST">
Description <input type="text" id="desc" name="desc">
SWL.Tonne <input type="text" id="swltonne" name="swltonne">
Inches <input type="text" id="inches" name="inches">
Model <input type="text" id="model" name="model">
SafetyFactor <input type="text" id="safetyfactor" name="safetyfactor">
Date/LPO <input type="text" id="datelpo" name="datelpo">
Manufacturer <input type="text" id="manu" name="manu">
Certificate.Number <input type="text" id="cnumber" name="cnumber">
Opening.Stock <input type="text" id="opstock" name="opstock">
January <input type="text" id="jan" name="jan">
February <input type="text" id="feb" name="feb">
March <input type="text" id="mar" name="mar">
April <input type="text" id="apr" name="apr">
May <input type="text" id="may" name="may">
June <input type="text" id="jun" name="jun">
July <input type="text" id="jul" name="jul">
August <input type="text" id="aug" name="aug">
September <input type="text" id="sep" name="sep">
October <input type="text" id="oct" name="oct">
November <input type="text" id="nov" name="nov">
December <input type="text" id="dec" name="dec">
Total.Used <input type="text" id="totuse" name="totuse">
Available.Balance <input type="text" id="avaibal" name="avaibal">
Minimumm.Stock <input type="text" id="minstocks" name="minstocks">
FOQ/FOI <input type="text" id="foqi" name="foqi">
Comment <input type="text" id="comm" name="comm">
</div>
<div class="submit-container"> <input class="submit-button"
type="submit" value="Add">
<input class="submit-button" type="submit" value="Clear" /></div>
</form>
and this is the updateprocess.php I really dont know what to do. My database name is franklinoffshore and i used mysqli
<?php
$desc = $_POST['desc'];
$swltonne = $_POST['swltonne'];
$inches = $_POST['inches'];
$model = $_POST['model'];
$safetyfactor = $_POST['safetyfactor'];
$datelpo = $_POST['datelpo'];
$manu = $_POST['manu'];
$cnumber = $_POST['cnumber'];
$opstock = $_POST['opstock'];
$jan = $_POST['jan'];
$feb = $_POST['feb'];
$mar = $_POST['mar'];
$apr = $_POST['apr'];
$may = $_POST['may'];
$jun = $_POST['jun'];
$jul = $_POST['jul'];
$aug = $_POST['aug'];
$sep = $_POST['sep'];
$oct = $_POST['oct'];
$nov = $_POST['nov'];
$dec = $_POST['dec'];
$totuse = $_POST['totuse'];
$avaibal = $_POST['avaibal'];
$minstocks = $_POST['minstocks'];
$foqi = $_POST['foqi'];
$comm = $_POST['comm'];
//connect to the serverand database
$franklinoffshore = mysqli_connect("localhost","root","","franklinoffshore");
//query the database
{
$result = mysqli_query($franklinoffshore, "INSERT INTO inventory_hooks(Description,SWLTonne,Inches,Model,SafetyFactor,DateLPO,Manufacturer,CertificateNumber,OpeningStocks,January,February,March,April,May,June,July,August,September,October,November,December,TotalUsed,AvailableBalance,MinimumStocks,FoqFoi,Comment)
VALUES('$description','$swltonne','$inches','$model','$safetyfactor','$datelpo','$manufacturer','$cnumber','$opstock','$jan','$feb','$mar','$apr','$may','$jun','$jul','$aug','$sep','$oct','$nov','$dec','$totuse','$avaibal','$minstocks','$foqi','$comm')");
}
?>
It keeps saying that Lines (too many lines) are Undefined Index. Or was i missing something?
PS: Its an inventory list thats why its toooo much.
Try this:
<?php
$franklinoffshore = mysqli_connect("localhost","root","","franklinoffshore");
$desc = mysqli_real_escape_string($franklinoffshore, $_POST['desc']);
$swltonne = mysqli_real_escape_string($franklinoffshore,$_POST['swltonne']);
$inches = mysqli_real_escape_string($franklinoffshore,$_POST['inches']);
$model = mysqli_real_escape_string($franklinoffshore,$_POST['model']);
$safetyfactor = mysqli_real_escape_string($franklinoffshore,$_POST['safetyfactor']);
$datelpo = mysqli_real_escape_string($franklinoffshore,$_POST['datelpo']);
$manu = mysqli_real_escape_string($franklinoffshore,$_POST['manu']);
$cnumber = mysqli_real_escape_string($franklinoffshore,$_POST['cnumber']);
$opstock = mysqli_real_escape_string($franklinoffshore,$_POST['opstock']);
$jan = mysqli_real_escape_string($franklinoffshore,$_POST['jan']);
$feb = mysqli_real_escape_string($franklinoffshore,$_POST['feb']);
$mar = mysqli_real_escape_string($franklinoffshore,$_POST['mar']);
$apr = mysqli_real_escape_string($franklinoffshore,$_POST['apr']);
$may = mysqli_real_escape_string($franklinoffshore,$_POST['may']);
$jun = mysqli_real_escape_string($franklinoffshore,$_POST['jun']);
$jul = mysqli_real_escape_string($franklinoffshore,$_POST['jul']);
$aug = mysqli_real_escape_string($franklinoffshore,$_POST['aug']);
$sep = mysqli_real_escape_string($franklinoffshore,$_POST['sep']);
$oct = mysqli_real_escape_string($franklinoffshore,$_POST['oct']);
$nov = mysqli_real_escape_string($franklinoffshore,$_POST['nov']);
$dec = mysqli_real_escape_string($franklinoffshore,$_POST['dec']);
$totuse = mysqli_real_escape_string($franklinoffshore,$_POST['totuse']);
$avaibal = mysqli_real_escape_string($franklinoffshore,$_POST['avaibal']);
$minstocks = mysqli_real_escape_string($franklinoffshore,$_POST['minstocks']);
$foqi = mysqli_real_escape_string($franklinoffshore,$_POST['foqi']);
$comm = mysqli_real_escape_string($franklinoffshore,$_POST['comm']);
//connect to the serverand database
$result = mysqli_query($franklinoffshore,
"INSERT INTO inventory_hooks
(Description,SWLTonne,Inches,Model,
SafetyFactor,DateLPO,Manufacturer,
CertificateNumber,OpeningStocks,
January,February,March,April,May,
June,July,August,September,October,
November,December,TotalUsed,AvailableBalance,
MinimumStocks,FoqFoi,Comment) VALUES
('$description','$swltonne',
'$inches','$model','$safetyfactor',
'$datelpo','$manufacturer','$cnumber',
'$opstock','$jan','$feb','$mar','$apr',
'$may','$jun','$jul','$aug','$sep','$oct',
'$nov','$dec','$totuse','$avaibal','$minstocks',
'$foqi','$comm')");
if($result) {
echo "It works";
}
?>

Group Arrays By Another Array?

I'm trying to sort one array by another array. Both these arrays get their content from a form.
Here's my form code:
<form method="post" action="">
<div class="groupcontainer">
<br/><label>Group One:</label><br/>
<input type="text" name="groupname[]" value="groupone" /><br/>
<br/><label>Variable Group One:</label><br/>
<input type="text" name="variable[]" value="variableone" />
<input type="text" name="variable[]" value="variabletwo" />
</div>
<br/>
<div class="groupcontainer">
<br/><label>Group Two:</label><br/>
<input type="text" name="groupname[]" value="grouptwo" /><br/>
<br/><label>Variable Group Two:</label><br/>
<input type="text" name="variable[]" value="variablethree" />
<input type="text" name="variable[]" value="variablefour" />
</div>
<br/>
<input type="submit" name="submit" value="Submit" />
</form>
Here's the PHP code:
<?php
if (!$_POST['submit'] == "") {
foreach($_POST['groupname'] as $groupname) {
$groupnum = 1;
foreach($_POST['variable'] as $variable) {
print "$".$groupname.$groupnum." = '".$variable."';<br/>";
$groupnum++;
}
print "$".$groupname." = array(";
for ($arrnum = 1; $arrnum <= count($_POST['variable']); $arrnum++) {
print "$".$groupname.$arrnum.", ";
}
print ");<br/><br/>";
}
}
?>
This is the result I get when I submit the form:
$groupone1 = '$variableone';
$groupone2 = '$variabletwo';
$groupone3 = '$variablethree';
$groupone4 = '$variablefour';
$groupone = array($groupone1, $groupone2, $groupone3, $groupone4, )
$grouptwo1 = '$variableone';
$grouptwo2 = '$variabletwo';
$grouptwo3 = '$variablethree';
$grouptwo4 = '$variablefour';
$grouptwo = array($grouptwo1, $grouptwo2, $grouptwo3, $grouptwo4, )
This is the result that I actually want:
$groupone1 = '$variableone';
$groupone2 = '$variabletwo';
$groupone = array($groupone1, $groupone2)
$grouptwo1 = '$variablethree';
$grouptwo2 = '$variablefour';
$grouptwo = array($grouptwo1, $grouptwo2)
The whole thing needs to be dynamic since I want to add as many groups and variables as I want.
I've been searching for an answer for days and already asked two people who didn't know an answer. Maybe you guys can help. Thanks!
Update:
Just to clarify a few points:
So basically I want to be able to add as many input forms as I want (I use jQuery for that) to create as many groups and variables as I want, for example like this:
$groupwuteva1 = 'hello';
$groupwuteva2 = 'bye':
$randomname1 = 'green';
$randomname2 = 'blue';
$randomname3 = 'red';
$blabla1 = 'abc';
$blabla2 = 'xyz';
$blabla3 = '123';
$blabla4 = 'bla';
Whatever I use as groupname will be used in array one, e.g. I call a group "Colors" and the variables I put into the form for that group are "blue", "red" and "green". Then I would get this code:
$colors1 = 'green';
$colors2 = 'blue';
$colors3 = 'red';
I hope this clairfies some questions. And thanks a ton for all responses so far!
You can take the group name as a container to store all associated variable values in it. And later, use variable variables and implode() function to process your html form.
HTML
<form method="post" action="">
<div class="groupcontainer">
<br/><label>Groupe One:</label><br/>
<input type="text" name="groupname[]" value="groupone" /><br/>
<br/><label>Variable Group One:</label><br/>
<input type="text" name="groupone[]" value="variableone" />
<input type="text" name="groupone[]" value="variabletwo" />
</div>
<br/>
<div class="groupcontainer">
<br/><label>Groupe Two:</label><br/>
<input type="text" name="groupname[]" value="grouptwo" /><br/>
<br/><label>Variable Group One:</label><br/>
<input type="text" name="grouptwo[]" value="variablethree" />
<input type="text" name="grouptwo[]" value="variablefour" />
</div>
<br/>
<input type="submit" name="submit" value="Submit" />
</form>
PHP
if(isset($_POST['submit'])){
foreach($_POST['groupname'] as $value){
$arr = array();
$i = 1;
foreach($_POST[$value] as $v){
$var = $value . $i;
$$var = $v;
echo $var . " = " . $$var . "<br />";
$arr[] = $$var;
++$i;
}
$output = $value . " = array(" . implode(",", $arr) . ")";
echo $output . "<br /><br />";
}
}
Output:
groupone1 = variableone
groupone2 = variabletwo
groupone = array(variableone,variabletwo)
grouptwo1 = variablethree
grouptwo2 = variablefour
grouptwo = array(variablethree,variablefour)
try this form:
<form method="post" action="">
<?php foreach(array('groupone', 'grouptwo') as $num):?>
<div class="groupcontainer">
<br/><label>Groupe <?php echo $num;?>:</label><br/>
<input type="text" name="groupname[]" value="<?php echo $num;?>" /><br/>
<br/><label>Variable Group <?php echo $num;?>:</label><br/>
<input type="text" name="variable[<?php echo $num;?>][]" value="<?php echo uniqid();?>" />
<input type="text" name="variable[<?php echo $num;?>][]" value="<?php echo uniqid();?>" />
</div>
<br/>
<?php endforeach;?>
<input type="submit" name="submit" value="Submit" />
</form>
and code:
if ($_POST) {
foreach($_POST['groupname'] as $groupname) {
$$groupname = array();
foreach($_POST['variable'][$groupname] as $variable) {
${$groupname}[] = $variable;
}
}
var_dump($groupone);
var_dump($grouptwo);
}

form $_POST error, dynamic fields

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

Categories