|
Hi Richard,
For one thing, this is the furthest I have gone with this query.
FINALLY, it was able to save something to the database.
Incredible.
There are just two issues,
1, It is inserting only one row
2,It is just inserting only one character.
For instance, if first name is Richard, it inserts R.
If Karin, it inserts K.
But atleast, something is getting inserted.
This is awesome.
Thank you very sir.
Always to the rescue!
|
|
|
|
|
I am so close to getting it to work correctly.
Looking at this page below, I discovered the way I had the hidden form fields coded was the reason only one character of each value was getting inserted into the database.
After tweaking it, full record with full values are now getting inserted - YEA!
I still have one more issue and it is to get all records inserted instead of just the first row.
Perhaps, the hidden form fields still have something to do with it?
If anyone can spot this, I will be extremely grateful.
<?php
error_reporting(E_ALL);
";
if(isset($_POST['submitted'])) {
if(trim($_POST['employeename']) === '') {
$nameError = 'Please enter your name.';
$hasError = true;
} else {
$employeename = $_POST['employeename'];
}
if(trim($_POST['email']) === '') {
$emailError = 'Please enter your email address.';
$hasError = true;
} else if (!preg_match("/^[[:alnum:]][a-z0-9_.-]*@[a-z0-9.-]+\.[a-z]{2,4}$/i", trim($_POST['email']))) {
$emailError = 'You entered an invalid email address.';
$hasError = true;
} else {
$email = trim($_POST['email']);
}
if(trim($_POST['ttitle']) === '') {
$nameError = 'Please enter your title.';
$hasError = true;
} else {
$ttitle = $_POST['ttitle'];
}
$rowIDs = $_POST['rowIDs'];
$row2IDs = $_POST['row2IDs'];
echo 'Employee Name: '. $employeename .' ';
echo 'Employee Title:' . $ttitle .' ';
echo 'Your email: '. $email . ' ';
echo ' ';
foreach ($rowIDs as $id) {
$sourcename = $_POST['sourcename' . $id];
$sourceaddress = $_POST['sourceaddress' . $id];
$income = $_POST['income' . $id];
echo 'Name: '. $sourcename . ' ';
echo 'Address: '. $sourceaddress . ' ';
echo 'Income: '. $income . '
';
}
foreach ($row2IDs as $id) {
$spousename = $_POST['spousename' . $id];
$spouseAddress = $_POST['spouseAddress' . $id];
$spouseIncome = $_POST['spouseIncome' . $id];
echo 'Name: '. $spousename . ' ';
echo 'Address: '. $spouseAddress . ' ';
echo 'spouseIncome: '. $spouseIncome . '
';
}
}
?>
Return to make changes
|
|
|
|
|
The issues, as I tried to point out yesterday, is that you are not presenting proper variable values in your calls to MySQL. You must capture the data from the user into proper variables which the database code can read. Like Richard D. I am not a PHP expert, but it is fairly clear that PHP treats its variables somewhat strangely.
|
|
|
|
|
Is the debug code printing out the correct data?
The debug code is taking the row numbers from rowIDs and row2IDs , and appending them to the field names. It expects fields called sourcename1 , sourcename2 , etc. (This matches the answer I posted to your previous question.)
The database code you posted is expecting the fields to be named sourcename[] , sourceaddress[] etc. in the HTML. It expects them all to have the same number of values, which doesn't match the code from your previous question - you might have three "source" rows and only one "spouse" row. You'd also need to check what happens if one of the fields was left blank - if PHP doesn't leave a blank entry in the array, you might end up putting the address for row 3 with the name from row 2.
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
Thank you both.
This has been the most depressing project I have been on and it is because of just this one.
At this point, I am willing to pay to get this to work because the pressure from upper management right now is too intense.
Ok Richard, yesterday, I posted the markup just to show if the problem could be coming from there.
With your solution in mind, here is the adjusted markup that allowed it to at least submit to the db even though it was submitting just one row:
<form action='final.php' method = 'POST'>
<input type="hidden" name="employeename" value="<?php echo $employeename; ?>">
<input type="hidden" name="ttitle" value="<?php echo $ttitle; ?>">
<input type="hidden" name="email" value="<?php echo $email; ?>">
<input type="hidden" name="sourcename[]" value="<?php echo $_POST['sourcename']; ?>">
<input type="hidden" name="sourceaddress[]" value="<?php echo $_POST['sourceaddress']; ?>">
<input type="hidden" name="income[]" value="<?php echo $_POST['income' . $id]; ?>">
<input type="hidden" name="spousename[]" value="<?php echo $_POST['spousename']; ?>">
<input type="hidden" name="spouseAddress[]" value="<?php echo $_POST['spouseAddress']; ?>">
<input type="hidden" name="spouseIncome[]" value="<?php echo $_POST['spouseIncome']; ?>">
<input type="submit" value="submit" />
</form>
I know you guys say you are no php experts but you are still 10 times more knowledgeable about php than me.
Given the solution the great Richard D posted yesterday. what could be adjusted with the above markup of hidden form fields to match with Richard's solution.
The problem could be with this markup of the processing code.
This has become agonizing.
Note: the fields that don't have [] are not array.
|
|
|
|
|
Start by rendering all of the required fields, with the correct names and values:
<?php foreach ($rowIDs as $id) { ?>
<input type="hidden" name="rowIDs[]" value="<?php echo $id; ?>">
<input type="hidden" name="<?php echo 'sourcename' . $id ?>" value="<?php echo $_POST['sourcename' . $id]; ?>">
<input type="hidden" name="<?php echo 'sourceaddress' . $id ?>" value="<?php echo $_POST['sourceaddress' . $id]; ?>">
<input type="hidden" name="<?php echo 'income' . $id ?>" value="<?php echo $_POST['income' . $id]; ?>">
<?php }?>
<?php foreach ($row2IDs as $id) { ?>
<input type="hidden" name="row2IDs[]" value="<?php echo $id; ?>">
<input type="hidden" name="<?php echo 'spousename' . $id ?>" value="<?php echo $_POST['spousename' . $id]; ?>">
<input type="hidden" name="<?php echo 'spouseAddress' . $id ?>" value="<?php echo $_POST['spouseAddress' . $id]; ?>">
<input type="hidden" name="<?php echo 'spouseIncome' . $id ?>" value="<?php echo $_POST['spouseIncome' . $id]; ?>">
<?php } ?>
In final.php , you read them using the same code:
$rowIDs = $_POST['rowIDs'];
foreach ($rowIDs as $id) {
$sourcename = $_POST['sourcename' . $id];
$sourceaddress = $_POST['sourceaddress' . $id];
$income = $_POST['income' . $id];
...
}
$row2IDs = $_POST['row2IDs'];
foreach ($row2IDs as $id) {
$spousename = $_POST['spousename' . $id];
$spouseAddress = $_POST['spouseAddress' . $id];
$spouseIncome = $_POST['spouseIncome' . $id];
...
}
The only slight issue is that you won't be able to have the source and spouse on the same row in your database table, because they're independent lists. You'll have to put them on different rows:
$sql = 'INSERT INTO `myDB`.`wp_myTable` ( `employeeID`'
. ', `sourcename`, `sourceaddress`, `income` )'
. ' VALUES ( ? , ? , ? , ? )';
if( $sth = mysqli_prepare($conn,$sql) ) {
mysqli_stmt_bind_param($sth, 'ssss',
$last_id,
$sourcename,
$sourceaddress,
$income);
$rowIDs = $_POST['rowIDs'];
foreach ($rowIDs as $id) {
$sourcename = $_POST['sourcename' . $id];
$sourceaddress = $_POST['sourceaddress' . $id];
$income = $_POST['income' . $id];
mysqli_stmt_execute($sth);
}
mysqli_stmt_close($sth);
}
$sql = 'INSERT INTO `myDB`.`wp_myTable` ( `employeeID`'
. ', `spousename`,`spouseAddress`,`spouseincome` )'
. ' VALUES ( ? , ? , ? , ? )';
if( $sth = mysqli_prepare($conn,$sql) ) {
mysqli_stmt_bind_param($sth, 'ssss',
$last_id,
$spousename,
$spouseAddress,
$spouseIncome);
$row2IDs = $_POST['row2IDs'];
foreach ($row2IDs as $id) {
$spousename = $_POST['spousename' . $id];
$spouseAddress = $_POST['spouseAddress' . $id];
$spouseIncome = $_POST['spouseIncome' . $id];
mysqli_stmt_execute($sth);
}
mysqli_stmt_close($sth);
}
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
YEA!!!!!!!!!!!!!!!!!!!!!!
wow!!!
I don't know if I should stand up and dance.
WOW!
My manager gave till 9AM tomorrow to get it working. Needless to say, I didn't sleep well last night.
Sorry I am just responding now because I just got to work.
I responded earlier from work.
Richard D, I don't know if you remember me saying this a couple of years ago.
There is guy called Bill Wilkinson, one of the best in this business.
Like you, he would say, not familiar with this but he ends up solving it.
Like him, anytime you are involved in my thread, I feel a weight lifted off me because like him, you always see that the issue is resolved.
THANK YOU.
Ok, let me calm down and ask a few questions.
As you may know, there are many more fields than this.
From your comment, they will all end up inserting into different rows.
The good news is that they belong to same employeeID.
So, how do I tie them up together when I attempt to display the records?
I probably need UNION ALL and GROUP BY?
Boy, I feel so relieved.
As always, thanks for coming to my rescue - again!
You are such a load of talent. Is there something you don't know about technology?
I have spent over a week on this with really great experts and they still could not get it to work for me.
|
|
|
|
|
Personally, I'd be inclined to split them into multiple tables - "employee details", "source details", and "spouse details". You'd have a one-to-many relationship from the employee details table to the source details table, and another from the employee details table to the spouse details table.
To display the full details, you'd then have three queries: one to get the employee details; one to get the list of source details; and one to get the list of spouse details.
SELECT `employeename`, `ttitle`, `email` FROM `myDB`.`wp_employeeDetails` WHERE `employeeID` = ?
SELECT `sourcename`, `sourceaddress`, `income` FROM `myDB`.`wp_sourceDetails` WHERE `employeeID` = ?
SELECT `spousename`, `spouseAddress`, `spouseincome` FROM `myDB`.`wp_spouseDetails` WHERE `employeeID` = ?
You could do that with your current design, but you'd need some way to identify whether a record was the employee details, the source details, or the spouse details. It could get quite messy.
SELECT `employeename`, `ttitle`, `email` FROM `myDB`.`wp_myTable` WHERE `employeeID` = ? AND `employeename` Is Not Null
SELECT `sourcename`, `sourceaddress`, `income` FROM `myDB`.`wp_myTable` WHERE `employeeID` = ? AND `sourcename` Is Not Null
SELECT `spousename`, `spouseAddress`, `spouseincome` FROM `myDB`.`wp_myTable` WHERE `employeeID` = ? AND `spousename` Is Not Null
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
Ok I see.
So, let me say for instance that I have these three tables (they will be up to 7).
CEATE TABLE employees (
employeeid INT NOT NULL,
employeename varchar(25),
ttitle varchar(25),
email varchar(25),
PRIMARY KEY (employeeid)
);
create table wp_spousedetails
(
spouseid INT NOT NULL,
name varchar(30) not null,
foreign key (employeeid) references menus(employeeid),
primary key (spouseid)
);
create table wp_sourcedetails
(
sourceid INT NOT NULL,
name varchar(30) not null,
foreign key (employeeid) references menus(employeeid),
primary key (sourceid)
);
Please don't worry if there is any syntax error with these. That's not the point of posting these ddl statements. I can fix those.
My question is to reference them all, all I need is employeeid, no?
Select e.employeename, e.ttitle, e.email, so.sourcename,so.sourceincome, sp.spousename,sp.spouseincome.... from employees e on so.employeeid = e.employeeid, sp.employeeid = e.employeeid.
That's really all I need right?
I am trying to calm down from excitement. This has been agonizing.
Thank you, thank you sir
|
|
|
|
|
Essentially, but if you join all of the tables together, you're going to get the details repeated across multiple rows.
For example, if employee 1 has 3 sources and 2 spouses, you'll get six rows:
Employee Source Spouse
-----------------------------
1 A X
1 A Y
1 B X
1 B Y
1 C X
1 C Y
That may or may not work, depending on your requirements.
With three separate queries, filtered by the employee ID, you'd get one record for the employee, three records for the sources, and two records for the spouses.
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
LAST question (I can't believe that I have arrived at this), the query that I need between the two queries you posted is the bottom portion of the queries?
|
|
|
|
|
From this message[^], the second set of queries should work with your current, single-table design. You'd use the first set if you split the data into three tables.
Obviously, you'll need to fix up the table and column names to match your database, and add any missing columns to the queries.
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
Thank you very.
My anxiety level has dropped to almost zero since your solution.
You saved me again.
Your reward, I promise will come one day.
May not be from me but it will come.
|
|
|
|
|
Hi Richard,
Sorry for calling you out but I did because I needed an urgent help.
This is the code you helped me out with (Thanks again).
//markup
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Forms</title>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap-theme.min.css" integrity="sha384-rHyoN1iRsVXV4nD0JutlnGaslCJuC7uwjduW9SVrLvRYooPp2bWYgmgJQIXwl/Sp" crossorigin="anonymous">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js" integrity="sha384-Tc5IQib027qvyjSMfHjOMaLkfuWVxZxUPnCJA7l2mCWNIpG9mGCD8wGNIcPD7Txa" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/handlebars.js/4.0.6/handlebars.min.js"></script>
<style type="text/css">
.bs-example{
margin-left: 250px;
margin-top: 30px;
}
</style>
</head>
<body>
<form class="form-inline" action="<?php echo get_option('siteurl'); ?>/forms/preview.php" id="contactForm" role="form" method="post">
<div class="form-group">
<label for="employeename">Employee Name</label><br>
<input type="text" name="employeename" id="employeename" style="width:375px;" placeholder="your name..." class="form-control" value="" class="required requiredField" />
</div>
<div class="form-group">
<label for="ttitle">Title</label><br>
<input type="text" name="ttitle" id="ttitle" style="width:375px;" placeholder="Your title..." class="form-control" value="" class="required requiredField" />
</div><br><br>
<script id="row-template" type="text/x-handlebars-template">
<div>
<!--reseed attribute IDs in case of gap resulting from deletions -->
<input type="hidden" name="rowIDs" value="{{rowNumber}}" />
<div class="form-group">
<label for="sourcename1{{rowNumber}}">Name</label><br>
<input type="text" name="sourcename1{{rowNumber}}" id="sourcename1{{rowNumber}}" value="" class="required requiredField" />
<?php if($nameError != '') { ?>
<?=$nameError;?>
<?php } ?>
</div>
<div class="form-group">
<label for="sourceaddress1{{rowNumber}}">Address</label><br>
<input type="text" name="sourceaddress1{{rowNumber}}" id="sourceaddress1{{rowNumber}}" style="width:250px;" class="form-control" value="" class="required requiredField" />
<?php if($nameError != '') { ?>
<?=$nameError;?>
<?php } ?>
</div>
<div class="form-group">
<label for="income1{{rowNumber}}">Income</label><br>
<input type="text" style="width:250px;" class="form-control" name="income1{{rowNumber}}" id="income1{{rowNumber}}" value="" class="required requiredField" />
<?php if($nameError != '') { ?>
<?=$nameError;?>
<?php } ?>
</div>
<input id="Button{{rowNumber}}" type="button" rel="remove-row" value="Remove" />
</div>
</script>
<div id="addrow" data-row-count="1" data-template-id="row-template">
<div>
<!--
<input type="hidden" name="rowIDs" value="1" />
<div class="form-group">
<label for="sourcename1">Name</label><br>
<input type="text" name="sourcename1[]" id="sourcename1" value="" class="required requiredField" />
<?php if($nameError != '') { ?>
<?=$nameError;?>
<?php } ?>
</div>
<div class="form-group">
<label for="sourceaddress1">Address</label><br>
<input type="text" name="sourceaddress1[]" id="sourceaddress1" style="width:250px;" class="form-control" value="" class="required requiredField" />
<?php if($nameError != '') { ?>
<?=$nameError;?>
<?php } ?>
</div>
<div class="form-group">
<label for="income1">Income</label><br>
<input type="text" name="income1[]" id="income1" style="width:250px;" class="form-control" value="" class="required requiredField" />
<?php if($nameError != '') { ?>
<?=$nameError;?>
<?php } ?>
<input type="button" value="Add More" rel="add-row" data-target="addrow" />
</div>
</div>
</div><br><br>
<script id="row-template-2" type="text/x-handlebars-template">
<div>
<!--reseed attribute IDs in case of gap resulting from deletions -->
<input type="hidden" name="row2IDs" value="{{rowNumber}}" />
<div class="form-group">
<input type="text" style="width:250px;" class="form-control" name="spousename{{rowNumber}}" id="spousename{{rowNumber}}" value="" class="required requiredField" />
<?php if($nameError != '') { ?>
<?=$nameError;?>
<?php } ?>
</div>
<div class="form-group">
<input type="text" style="width:250px;" class="form-control" name="spouseAddress{{rowNumber}}" id="spouseAddress{{rowNumber}}" value="" class="required requiredField" />
<?php if($nameError != '') { ?>
<?=$nameError;?>
<?php } ?>
</div>
<div class="form-group">
<input type="text" style="width:250px;" class="form-control" name="spouseIncome{{rowNumber}}" id="spouseIncome{{rowNumber}}" value="" class="required requiredField" />
<?php if($nameError != '') { ?>
<?=$nameError;?>
<?php } ?>
</div>
<input id="Button{{rowNumber}}" type="button" rel="remove-row" value="Remove" />
</div>
</script>
<div id="addrow2" data-row-count="1" data-template-id="row-template-2">
<div>
<!--
<input type="hidden" name="row2IDs" value="1" />
<div class="form-group">
<label for="spousename">Name</label><br>
<input type="text" style="width:250px;" class="form-control" name="spousename[]" id="spousename" value="" class="required requiredField" />
<?php if($nameError != '') { ?>
<?=$nameError;?>
<?php } ?>
</div>
<div class="form-group">
<label for="spouseAddress">Address</label><br>
<input type="text" style="width:250px;" class="form-control" name="spouseAddress[]" id="spouseAddress" value="" class="required requiredField" />
<?php if($nameError != '') { ?>
<?=$nameError;?>
<?php } ?>
</div>
<div class="form-group">
<label for="spouseIncome">Income</label><br>
<input type="text" style="width:250px;" class="form-control" name="spouseIncome[]" id="spouseIncome" value="" class="required requiredField" />
<?php if($nameError != '') { ?>
<?=$nameError;?>
<?php } ?>
<input type="button" value="Add More" rel="add-row" data-target="addrow2" />
</div>
</div>
</div> <br><br>
<div class="form-group">
<label for="email">Email</label><br>
<input type="text" style="width:750px;" class="form-control" name="email" id="email" value="" class="required requiredField email" />
<?php if($emailError != '') { ?>
<?=$emailError;?>
<?php } ?>
</div>
<button type="submit" class="btn btn-primary">Submit</button><br>
<input type="hidden" name="submitted" id="submitted" value="true" />
</form>
<script type='text/javascript'>
$(function(){
var templates = {
"row-template": Handlebars.compile($("#row-template").html()),
"row-template-2": Handlebars.compile($("#row-template-2").html())
};
$("form").on("click", "input[rel='add-row']", function(){
var containerId = $(this).data("target");
var container = $("#" + containerId);
var templateId = container.data("templateId");
var template = templates[templateId];
var rowCount = container.data("rowCount");
rowCount++;
var context = { rowNumber: rowCount };
var html = template(context);
container.append(html);
container.data("rowCount", rowCount);
});
$("form").on("click", "input[rel='remove-row']", function(){
var row = this.parentNode;
var container = row.parentNode;
container.removeChild(row);
});
});
</script>
</body>
</html>
//Server side:
".print_r($_POST, 1)."";
if(isset($_POST['employeename']))
$employeename = $_POST['employeename'];
if(isset($_POST['ttitle']))
$ttitle = $_POST['ttitle'];
$rowIDs = $_POST['rowIDs'];
echo $employeename .' ';
echo $ttitle .' ';
for ($id = 0; $id < $rowIDs; $id++){
$sourcename1 = $_POST['sourcename1'][$id];
$sourceaddress1 = $_POST['sourceaddress1'][$id];
$income1 = $_POST['income1'][$id];
$spousename = $_POST['spousename'][$id];
$spouseAddress = $_POST['spouseAddress'][$id];
$spouseIncome = $_POST['spouseIncome'][$id];
echo 'Name: '. $sourcename1 .' ';
echo 'Address: '. $sourceaddress1.' ';
echo 'Income: '. $income1.' ';
echo 'Name: '. $spousename .' ';
echo 'Address: '. $spouseAddress.' ';
echo 'Income: '. $spouseIncome.' ';
}
?>
<input type='text' name='employeename' value="<?php echo="" $employeename?="">">
<input type='text' name='ttitle' value="<?php echo="" $ttitle;="" ?="">">
<input type='text' name='sourcename1[]' value="<?php echo="" $sourcename1;="" ?="">">
<input type='text' name='sourceaddress1[]' value="<?php echo="" $sourceaddress1;="" ?="">">
<input type='text' name='income1[]' value="<?php echo="" $income1;="" ?="">">
There are two problems here and I think they are all related.
1, When I run the code, it is displaying only one row of data.
When I click to add another row and then fill enter data into that row, it doesn't display it.
2, Second issue is the error I am getting which says,
Notice: Undefined offset: 1 in C:\xampp\htdocs\disclosures\forms\preview.php on line 13
Notice: Undefined offset: 1 in C:\xampp\htdocs\disclosures\forms\preview.php on line 14
Notice: Undefined offset: 1 in C:\xampp\htdocs\disclosures\forms\preview.php on line 15
Notice: Undefined offset: 1 in C:\xampp\htdocs\disclosures\forms\preview.php on line 16
Notice: Undefined offset: 1 in C:\xampp\htdocs\disclosures\forms\preview.php on line 17
Notice: Undefined offset: 1 in C:\xampp\htdocs\disclosures\forms\preview.php on line 18
Any ideas what I am doing wrong?
Man, I am supposed to demo this app this afternoon.
Many thanks in advance
modified 23-Feb-17 8:59am.
|
|
|
|
|
samflex wrote: $sourcename1 = $_POST['sourcename1'][$id];
That should be:
$sourcename1 = $_POST['sourcename1' . $id];
Same for the other fields.
samflex wrote: When I run the code, it is displaying only one row of data.
Because you're only rendering a single row in the HTML. The additional rows are added via script when you click the button.
samflex wrote: When I click to add another row and then fill enter data into that row, it doesn't display it.
What do you mean?
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
phew!
Thanks so much Richard for coming to my rescue again.
The reason I changed to this:
$sourcename1 = $_POST['sourcename1'][$id];
was because the one you suggested I use which I just used again just now was giving the error below:
tice: Undefined index: sourcename10 in C:\xampp\htdocs\disclosures\forms\preview.php on line 13
My concern was that I didn't screw something up in the markup. I tried to follow the solution you provided.
What I meant by your question is that when I Add another row, it is not being captured on my php server code.
|
|
|
|
|
samflex wrote: Undefined index: sourcename10 in C:\xampp\htdocs\disclosures\forms\preview.php on line 13
Because you're using:
for ($id = 0; $id < $rowIDs; $id++)
so $id is the index, not the value.
Use the "foreach" version[^], as I suggested, and $id will be the value:
for ($rowIDs as $id)
If you want to stick to the standard for loop, you'll need to use:
for ($index = 0; $index < $rowIDs; $index++){
$id = $rowIDs[$index];
$sourcename1 = $_POST['sourcename1' . $id];
...
}
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
Believe me sir,
I see your solutions as money in the bank.
However, when I tried that original FOR loop you gave me, I got this:
rning: Invalid argument supplied for foreach() in C:\xampp\htdocs\disclosures\forms\preview.php on line 12
Now, I just tried your latest solution and this is what I am getting.
Notice: Undefined index: sourcename11 in C:\xampp\htdocs\disclosures\forms\preview.php on line 14
Notice: Undefined index: sourceaddress11 in C:\xampp\htdocs\disclosures\forms\preview.php on line 15
Notice: Undefined index: income11 in C:\xampp\htdocs\disclosures\forms\preview.php on line 16
Notice: Undefined index: spousename1 in C:\xampp\htdocs\disclosures\forms\preview.php on line 17
Notice: Undefined index: spouseAddress1 in C:\xampp\htdocs\disclosures\forms\preview.php on line 18
Notice: Undefined index: spouseIncome1 in C:\xampp\htdocs\disclosures\forms\preview.php on line 19
Name:
Address:
Income:
-----------
Name:
Address:
Income:
I am just wandering if the problem is in the markup.
Kind of frustrating.
I really appreciate your help.
modified 21-Feb-17 15:19pm.
|
|
|
|
|
Try changing the name of the hidden inputs to rowIDs[] , both in the Handlebars template and the static HTML row.
<input type="hidden" name="rowIDs[]" value="{{rowNumber}}" />
...
<input type="hidden" name="rowIDs[]" value="1" />
$rowIDs = $_POST['rowIDs'];
for ($rowIDs as $id) {
...
}
It looks like PHP needs the [] on the end of the form field name in order to treat the posted values as an array.
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
Right.
I have added them and now this:
Notice: Undefined index: sourcename11 in C:\xampp\htdocs\disclosures\forms\preview.php on line 13
Notice: Undefined index: sourceaddress11 in C:\xampp\htdocs\disclosures\forms\preview.php on line 14
Notice: Undefined index: income11 in C:\xampp\htdocs\disclosures\forms\preview.php on line 15
Notice: Undefined index: spousename1 in C:\xampp\htdocs\disclosures\forms\preview.php on line 16
Notice: Undefined index: spouseAddress1 in C:\xampp\htdocs\disclosures\forms\preview.php on line 17
Notice: Undefined index: spouseIncome1 in C:\xampp\htdocs\disclosures\forms\preview.php on line 18
Same errors. Unreal!
Thanks for your patience sir.
|
|
|
|
|
OK, remove the "1" from the field names in the template, and from the PHP script:
<input type="text" name="sourcename{{rowNumber}}" id="sourcename{{rowNumber}}" value="" class="required requiredField" />
<!--
$sourcename1 = $_POST['sourcename' . $id];
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
Nope,
Infact changing ROWIDS to rowIDs[] seems to have multiplied how many of those errors are rendered.
It's like million records (errors) displayed when infact, only entered records into two rows.
Who would have thought this would be this challenging.
Maybe I can all over from the markup to make that it is correct before looking at the php server side script?
Sorry for all the inconvenience sir.
|
|
|
|
|
Looks like you've already added [] to some fields that don't need it.
<script id="row-template" type="text/x-handlebars-template">
<div>
<!--reseed attribute IDs in case of gap resulting from deletions -->
<input type="hidden" name="rowIDs[]" value="{{rowNumber}}" />
<div class="form-group">
<label for="sourcename{{rowNumber}}">Name</label><br>
<input type="text" name="sourcename{{rowNumber}}" id="sourcename{{rowNumber}}" value="" class="required requiredField" />
</div>
<div class="form-group">
<label for="sourceaddress{{rowNumber}}">Address</label><br>
<input type="text" name="sourceaddress{{rowNumber}}" id="sourceaddress{{rowNumber}}" style="width:250px;" class="form-control" value="" class="required requiredField" />
</div>
<div class="form-group">
<label for="income{{rowNumber}}">Income</label><br>
<input type="text" style="width:250px;" class="form-control" name="income{{rowNumber}}" id="income{{rowNumber}}" value="" class="required requiredField" />
</div>
<input id="Button{{rowNumber}}" type="button" rel="remove-row" value="Remove" />
</div>
</script>
<div id="addrow" data-row-count="1" data-template-id="row-template">
<div>
<input type="hidden" name="rowIDs[]" value="1" />
<div class="form-group">
<label for="sourcename1">Name</label><br>
<input type="text" name="sourcename1" id="sourcename1" value="" class="required requiredField" />
<?php if($nameError != '') { ?>
<span class="error"><?=$nameError;?></span>
<?php } ?>
</div>
<div class="form-group">
<label for="sourceaddress1">Address</label><br>
<input type="text" name="sourceaddress1" id="sourceaddress1" style="width:250px;" class="form-control" value="" class="required requiredField" />
<?php if($addressError != '') { ?>
<span class="error"><?=$addressError;?></span>
<?php } ?>
</div>
<div class="form-group">
<label for="income1">Income</label><br>
<input type="text" name="income1" id="income1" style="width:250px;" class="form-control" value="" class="required requiredField" />
<?php if($incomeError != '') { ?>
<span class="error"><?=$incomeError;?></span>
<?php } ?>
</div>
<input type="button" value="Add More" rel="add-row" data-target="addrow" />
</div>
</div>
$rowIDs = $_POST['rowIDs'];
for ($rowIDs as $id) {
$sourcename = $_POST['sourcename' . $id];
$sourceaddress = $_POST['sourceaddress' . $id];
$income1 = $_POST['income' . $id];
echo 'Name: '. $sourcename1 . '<br />';
echo 'Address: '. $sourceaddress1 . '<br />';
echo 'Income: '. $income1 . '<br />';
}
NB: You'll need a separate loop to read the spouse name, address and income.
If it still doesn't work, check what values are being submitted to your PHP script.
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
I just tried your latest code. I entered records into the first row, dynamically added another row and populated that as well.
Below is the debug output as well as the errors.
This AngularJS dynamic code is so gorgeous and works great in terms of the markup but this has been an unbelievable nightmare for me.
Richard, sir, I am wondering if you can give me a hint on how to make the original code I posted look as pretty as this one you did.
What I mean is that the page once loaded looks nice and is nicely laid out but when you click to Add another row, the new row looks different.
Since I can't get this to work and I begged my manager for another, perhaps that might be easier to implement?
DEBUG POST DATA: Array
(
[employeename] =>
[ttitle] =>
[rowIDs] => 1
[sourcename1] => John Doe
[sourceaddress1] => 123 Doe street
[income1] => $36,000.00
[sourcename12] => Jane Doe
[sourceaddress12] => 23 Jane Way
[income12] => $67,000.00
[spousename] => Array
(
[0] =>
)
[spouseAddress] => Array
(
[0] =>
)
[spouseIncome] => Array
(
[0] =>
)
[email] =>
[submitted] => true
)
Warning: Invalid argument supplied for foreach() in C:\xampp\htdocs\disclosures\forms\preview.php on line 14
Notice: Undefined variable: sourcename1 in C:\xampp\htdocs\disclosures\forms\preview.php on line 28
"> Notice: Undefined variable: sourceaddress1 in C:\xampp\htdocs\disclosures\forms\preview.php on line 29
"> Notice: Undefined variable: income1 in C:\xampp\htdocs\disclosures\forms\preview.php on line 30
"> Notice: Undefined variable: spousename in C:\xampp\htdocs\disclosures\forms\preview.php on line 31
"> Notice: Undefined variable: spouseAddress in C:\xampp\htdocs\disclosures\forms\preview.php on line 32
"> Notice: Undefined variable: spouseIncome in C:\xampp\htdocs\disclosures\forms\preview.php on line 33
|
|
|
|
|
samflex wrote: [rowIDs] => 1
...
sourcename12
Why have you ignored my suggestion to remove the extra "1" from the field names in the template, and to add the [] suffix to the name of the rowIDs fields?
If you'd followed my suggestions, the submitted data would look like:
[rowIDs] => Array
(
[0] => 1
[1] => 2
)
[sourcename1] => John Doe
[sourceaddress1] => 123 Doe street
[income1] => $36,000.00
[sourcename2] => Jane Doe
[sourceaddress2] => 23 Jane Way
[income2] => $67,000.00
NB: rowIDs is an array, and the fields from the row have sequential names - sourcename1 , sourcename2 , etc.
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|