Essentially I have an HTML form with two fields. It links to a MySQL
database with one table (table1) and three fields (ID, firstname, lastname).
I have javascript to clone the div that contains the fields so the user can
add and delete fieldsets at will. The fields on the form are named fname and
lname.
index.php
PHP
- include php connection file to db
/PHP
HTML
javascript to add/delete fields
div to store my fieldset (hidden)
form containing a span element to insert my cloned div's - and post file
is result.php
/HTML
My thoughts are that I need to set up the php array and that the array is
two dimensional. This would go in the PHP section of index.php. Something
like:
$fields = array(array(fname),
array(lname)
);
This means that $fields is essentially a row(s) of data, right?
The second part is the naming of my fields on my form as such:
First Name: <input type="text" id=fname[] />
Last Name: <input type="text" id=lname[] />
Am I even close? All of the info on the net that deals with arrays, gives
specific examples, where the values are coded into the array. Having an
array named 'fruit' and pushing 'apple'; banana and orange into it, ain't
helpin all that much! LOL I can't make the leap from the spelled-out arrays
to the user-input arrays. Mental block, maybe.
Before trying to add extra items with javascript, I suggest you get it
working with one set of data first on a static page.
Then add a second set of fields to your form and get the code working
with both fields in a loop.
Once you get the PHP code working, then you can add javascript, etc.
But are you sure you need the javascript? More commonly, individual
entries to the database are entered on separate pages; that is, if you
want to add 3 people to a database, you submit the form three times.
As to how to do it on the server - you've got the names working fine;
you can handle it as a two dimensional array on the server, or you can
just loop through the $_POST entires and add each one to the database as
you go (after validating the data, of course). You really don't need to
copy all the data into another array.
--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstu...@attglobal.net
==================
Thanks for the quick response!
I will try again with your instructions. There does need to be some sort of
functionality on the form for users to add a varied number of items (in this
case people). I do like the idea of looping through the array and posting
one at a time with validation better than one honkin' array pooped into
MySQL unvalidated. :)
Jen
"Jerry Stuckle" <jstu...@attglobal.net> wrote in message
news:hvbfpf$gn9$1...@news.eternal-september.org...
The id don't matter, you want to set the name, if you are going to post it
directly without using javascript. In your case I would number the cells, this
way you ensure that the cells with the same number will match.
And no it won't be a multidimensional array, it will just be two ordinary arrays
$_REQUEST['fname'] and $_REQUEST['lname']
It may be simpler to use some ajax call and send the data as a json, that way
you can send objects, multidimensional arrays in a simple way, but that will
require more javascript writing and help with that you have to seek in some
other newsgroup.
I agree with Jerry, take baby steps and master each step before you do
something advanced and it seems you need to get a good grip of the basics at
this point.
--
//Aho
If they are individual entries, then if one fails, the other(s) should
still be added. However, trying to determine which work and which don't
becomes a lot more problematic - which is why individual entries are
normally entered on separate pages.
I agree the ability to enter multiple rows on the same page is nice -
however, it makes the back-end programming much harder (when handling
errors).
It's just much easier to enter one per page.
I actually just kept searching until I found code that seemed to do what I
wanted it to do. I edited it to fit my fields and voila.
The PHP code is:
<?php
include 'connect.php';
mysql_select_db("testdb", $conn);
$fname = $_POST['FirstName'];
$lname = $_POST['LastName'];
$limit = count($fname);
for($i=0;$i<$limit;$i++) {
$fname[$i] = mysql_real_escape_string($fname[$i]);
$lname[$i] = mysql_real_escape_string($lname[$i]);
$query = "INSERT INTO table1 (FirstName, LastName) VALUES
('".$fname[$i]."','".$lname[$i]."')";
if(mysql_query($query,$conn))
echo "$i successfully inserted.<br/>";
else
echo "$i encountered an error.<br/>";
}
mysql_close($conn)
?>
I understand the loops and indices as I've encountered them many times in
VBA. I'm good with the logic.
The part I don't get is where you set $fname and $lname. If on the html form
the names have [] in them, it tells the php that it will be an array. So you
don't have to declare any arrays in the php file after you hit the submit
button? Or set array variables? Essentially I don't see the word "array" in
the code above and I'm confused as to why it doesn't need to be there. I at
least thought you'd have to $fname = array($_POST['FirstName']) or sumsuch.
Also in VBA at the end of the code you always release the variables. Do you
do that here as well?
it's a bit of a trick when you do that. Since simply creating a variable
$fname[] automatically creates an array (advancing the current index
position).
//top of script
$fname[] = 'Mary';
$fname[] = 'John';
...actually creates:
//top of script
$fname[0] = 'Mary';
$fname[1] = 'John';
So with multiple firstname[] variables in your form, the PHP interpreter
is creating:
$_POST['firstname'][0]
$_POST['firstname'][1]
...
$_POST['firstname'][n]
There is nothing stopping you from creating them at the start of the
script like so:
$fname = array();
$lname = array();
$fname = $_POST['FirstName'];
$lname = $_POST['LastName'];
> the code above and I'm confused as to why it doesn't need to be there. I at
> least thought you'd have to $fname = array($_POST['FirstName']) or sumsuch.
>
That's because $_POST, $_GET, $_REQUEST are arrays created by the PHP
interpreter (internally) when the script is executed.
> Also in VBA at the end of the code you always release the variables. Do you
> do that here as well?
>
>
PHP will destroy them (and even the mysql connection) upon exit, but it
doesn't hurt to do it explicitly (good programming practice).
--
Norman
Registered Linux user #461062
-Have you been to www.php.net yet?-