Click to See Complete Forum and Search --> : array complete form


kproc
12-27-2006, 11:43 AM
Hi
I have a form which displays as many forms as the user states when they select a value for a drop down menu. I'm trying to gte it so that when the user submits the form it will add the information for each form into my data dase.

Here is where I'm having my problem. I can post information into the table but all values are set to equal 'array'

how do I set the array value to equal the user imput?


<select name="sex[]">
<options>Male</option>
<option>Female</option>
</select>

<input name="name[]" type="text">

NightShift58
12-27-2006, 12:29 PM
You need to refer to your arrays (which is what you're creating when you use "name[]") with key values, i.e. name[0], sex[1]. You could use a FOREACH loop or a WHILE (list(...)) combination to cycle through the various arrays and retrieve the values that you want to insert/update in your table.

kproc
12-27-2006, 12:35 PM
I'm using foreach

If I change name[] to name[0] it should work ??

NightShift58
12-27-2006, 12:54 PM
If you use FOREACH, you should use something like:FOREACH ($name as $key => $val) :
$NAME_field = $name[$key];
$SEX_field = $sex[$key];
... etc ...
ENDFOREACH;

kproc
12-27-2006, 02:17 PM
what sets the value of $name within the foreach statement

NightShift58
12-27-2006, 08:08 PM
Good question.... Since the data comes from a form, the FOREACH loop should be based on $_POST['name'] and not $name.FOREACH ($_POST['name'] as $key => $val) :
$NAME_field = $_POST['name'][$key];
$SEX_field = $_POST['sex'][$key];
... etc ...
ENDFOREACH;You can assign $name = $_POST['name']; right before the loop as well and loop through $name instead of $_POST['name'].

kproc
12-28-2006, 07:40 PM
Below is the code that I'm trying to use to load the inforamtion from more then one form. for some reason its nbot working any help is great


<?php
/* Check User Script */
session_start(); // Start Session
include '../connections/db.php';

// Define post fields into simple variables
$childfirstname = $_POST['childfirstname'];
$childlastname = $_POST['childlastname'];
$childsex = $_POST['childsex'];
$user_id = $_SESSION['user_id'];
$dobY = $_POST['dobY'];
$dobM = $_POST['dobM'];
$dobD = $_POST['dobD'];

$childfirstname = stripslashes($childfirstname);
$childlastname = stripslashes($childlastname);
$childdob = stripslashes($childdob);
$childsex = stripslashes($childsex);



/* Let's strip some slashes in case the user entered
any escaped characters. */



if((!$childfirstname) || (!$childlastname) || (!$childdob) || (!$childsex)) {
$msg .= '<div style="width:325px" id= "formmessage">';

if(!$childfirstname){
$msg .= "Enter child's first name.<br />";
}
if(!$childlastname){
$msg .= "Enter child's lastname.<br />";
}
if(!$childdob){
$msg .= "Enter child's Date of Birth. <br />";
}
if(!$childsex){
$msg .= "Select child's sex.<br />";

}

$msg .= '</div>';

urlencode($msg);
header("location: ../addchild.php?msg=$msg");

exit(); // if the error checking has failed, we'll exit the script!
}

/* checking and ensure that the has not been added */

foreach ($child AS $key=>$value):
$childfirstname = $childfirstname[$key];
$childlastname = $childlastname[$key];
$childsex = $childsex[$key];
$childdob = $dobY[$key].'-'.$dobM[$key].'-'.$dobD[$key];


$sql_child_check = mysql_query("SELECT childlastname, childfirstname, childdob, child_id

FROM children WHERE childlastname = '$childlastname' AND
childfirstname = '$childfirstname' AND
childdob = '$childdob'");

$child_check = mysql_num_rows($sql_child_check);

$row = mysql_fetch_assoc($sql_child_check);

$child_id = $row['child_id'];


if(($child_check > 0)){

$msg .= '<div style="width:325px" id= "formmessage">';
$msg .= $childfirstname. ' '. $childlastname. " was found in database: <br />";
$msg .= "<a href='codeAddchildLink.php?id=$user_id&childid=$child_id'>Create Link</a>";
$msg .= '</div>';


urlencode($msg);
header("location: ../addchild.php?msg=$msg"); // Show the form again!
exit(); // exit the script so that we do not create this account!
}
// Enter info into the Database.
$sql = mysql_query("INSERT INTO children (childfirstname,
childlastname, childdob, childsex, owner_id, add_date)
VALUES('$childfirstname', '$childlastname', '$childdob', '$childsex', '$user_id' , now())")
or die (mysql_error());

if(!$sql){

}

$child_id=mysql_insert_id();

mysql_query("INSERT INTO Links (user_id, child_id, add_date) VALUES ('$user_id', '$child_id', now())")or die (mysql_error());

$msg .='<div style="width:325px" id= "formmessage">';
$msg .= $childfirstname. ' '. $childlastname. " has been added.";
$msg .= '</div>';

urlencode($msg);
header("location: ../addchild.php?msg=$msg");

ENDFOREACH;
?>

NightShift58
12-28-2006, 11:19 PM
I took me a little while, but I think I understand what you're trying to accomplish. However, I think that redirecting, i.e. using header() is probably making your life needlessly complicated and your debugging much harder than it really has to be.

In each case, regardless of the outcome, you end up doing a header() to addchild.php. I think it would be easier if the code for addchild.php would be part of the page you posted, rather than heading() to it.

That way, you would do your checking and depending on the outcome of the checks, at the end of the page, you would print whatever had to be printed.

As a first step, leaving a single header() at the end of the script, my first move would be to restructure the script so that it only needs a single call to header(). Once you do that, you'll see how you don't really need it and your code will be cleaner.

kproc
12-30-2006, 09:30 PM
I merged the two files and I know get error


Warning: Invalid argument supplied for foreach() in C:\Program Files\xampp\htdocs\mysite\addchild.php on line 50

line 50 = foreach ($child AS $key=>$value):


<?php
/* Check User Script */
session_start(); // Start Session
include ('connections/db.php');

// Define post fields into simple variables
$childfirstname = $_POST['childfirstname'];
$childlastname = $_POST['childlastname'];
$childsex = $_POST['childsex'];
$user_id = $_SESSION['user_id'];
$dobY = $_POST['dobY'];
$dobM = $_POST['dobM'];
$dobD = $_POST['dobD'];

$childfirstname = stripslashes($childfirstname);
$childlastname = stripslashes($childlastname);
$childdob = stripslashes($childdob);
$childsex = stripslashes($childsex);



/* Let's strip some slashes in case the user entered
any escaped characters. */



if((!$childfirstname) || (!$childlastname) || (!$childdob) || (!$childsex)) {
$msg .= '<div style="width:325px" id= "formmessage">';

if(!$childfirstname){
$msg .= "Enter child's first name.<br />";
}
if(!$childlastname){
$msg .= "Enter child's lastname.<br />";
}
if(!$childdob){
$msg .= "Enter child's Date of Birth. <br />";
}
if(!$childsex){
$msg .= "Select child's sex.<br />";

}

$msg .= '</div>';

}

/* checking and ensure that the has not been added */

foreach ($child AS $key=>$value):

$childfirstname = $childfirstname[$key];
$childlastname = $childlastname[$key];
$childsex = $childsex[$key];
$childdob = $dobY[$key].'-'.$dobM[$key].'-'.$dobD[$key];


$sql_child_check = mysql_query("SELECT childlastname, childfirstname, childdob, child_id

FROM children WHERE childlastname = '$childlastname' AND
childfirstname = '$childfirstname' AND
childdob = '$childdob'");

$child_check = mysql_num_rows($sql_child_check);

$row = mysql_fetch_assoc($sql_child_check);

$child_id = $row['child_id'];


if(($child_check > 0)){

$msg .= '<div style="width:325px" id= "formmessage">';
$msg .= $childfirstname. ' '. $childlastname. " was found in database: <br />";
$msg .= "<a href='codeAddchildLink.php?id=$user_id&childid=$child_id'>Create Link</a>";
$msg .= '</div>';

}
// Enter info into the Database.
$sql = mysql_query("INSERT INTO children (childfirstname,
childlastname, childdob, childsex, owner_id, add_date)
VALUES('$childfirstname', '$childlastname', '$childdob', '$childsex', '$user_id' , now())")
or die (mysql_error());

if(!$sql){

}

$child_id=mysql_insert_id();

mysql_query("INSERT INTO Links (user_id, child_id, add_date) VALUES ('$user_id', '$child_id', now())")or die (mysql_error());

$msg .='<div style="width:325px" id= "formmessage">';
$msg .= $childfirstname. ' '. $childlastname. " has been added.";
$msg .= '</div>';

ENDFOREACH;
?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Member Login Home</title>

<link rel="stylesheet" type="text/css" href="design/niftyCorners.css">
<link rel="stylesheet" type="text/css" href="design/niftyPrint.css" media="print">
<script type="text/javascript" src="design/nifty.js"></script>

<link href="design/main.css" rel="stylesheet" type="text/css" />

<script type="text/javascript">
window.onload=function(){
if(!NiftyCheck())
return;
<?php include('design/round.php'); ?>
}
</script>
</head>

<body>


<div id="outer">
<?php include('design/banner.php'); ?>
<p style="background-color:#F1F1F1; font-style:italic; color:#4B4B4B; font-weight:bold; font-size:12px; margin-top:-10px; margin-bottom:10px;">Welcome <? echo $_SESSION['first_name'] ." ". $_SESSION['last_name']; ?></p>
<div id="sub">
<div id="center">
<?php $msg = isset($_GET['msg']) && !empty($_GET['msg']) ? urldecode($msg) : '';
echo $msg; ?>
<?php $year = date("Y"); ?>



<form action="" method="post">
<p>Number of Children to add:
<select name="numChildren" style="width:75px;" onchange="this.form.submit();return true;">
<option value="00">choose </option>
<option value="00">00</option>
<option value="01">01</option>
<option value="02">02</option>
<option value="03">03</option>
<option value="04">04</option>
<option value="05">05</option>
</select>
</form>

<?php for($x = 1; $x <= $_POST['numChildren']; $x++){ ?>
<form name="addchild" method="post" action="script/codeAddchild.php">
<table class='columntable'>
<tr>
<th class='tableheader' colspan='2'>Add Children</th>
</tr>

<tr>
<td class="rowLeft" scope="row"><div align="left"><strong>Child's First Name</strong></div></td>
<td class="rowRight"><div align="left">
<input name="childfirstname[]" type="text" id="childfirstname" size="20">
</div></td>
</tr>
<tr>
<td class="rowLeft" scope="row"><div align="left"><strong>Child's Last Name</strong></div></td>
<td class="rowRight"><div align="left">
<input name="childlastname[]" type="text" id="childlastname" size="20" >
</div></td>
</tr>
<tr>
<td class="rowLeft" scope="row"><div align="left"><strong>Child's DOB</strong></div></td>
<td class="rowRight"><div align="left">
<select name="dobY[]" class="dobYSize">
<option value=""></option>
<?php
for($yr = $year; $yr >= 1900; $yr--)
{
echo "<option value='$yr'>$yr</option>\n";
}
?>
</select>
<select name="dobM[]">
<option value=""></option>
<option value="01">Jan</option>
<option value="02">Feb</option>
<option value="03">Mar</option>
<option value="04">Apr</option>
<option value="05">May</option>
<option value="06">Jun</option>
<option value="07">Jul</option>
<option value="08">Aug</option>
<option value="09">Sep</option>
<option value="10">Oct</option>
<option value="11">Nov</option>
<option value="12">Dec</option>
</select>
<select name="dobD[]">

<option value=""></option>
<?php
for($mth = 1; $mth <= 31; $mth++)
{
echo "<option value='$mth'>$mth</option>\n";
}
?>
</select>
</div></td>
</tr>
<tr>
<td class="rowLeft" scope="row"><div align="left"><strong>Child's Sex</strong></div></td>
<td class="rowRight"><div align="left">
<select name="childsex[]" id="childsex">
<option></option>
<option>Male</option>
<option>Female</option>
</select>
</div></td>
</tr>
<tr>
<th class="rowRight"colspan="2" scope="row"><div align="left">


</div></tr>
</table>

<?php }?>

<?php if(($_POST['numChildren']) >= 1){?>

<p>
<input class="submit" type="submit" name="addchild" value="Add Children">
</p>
</form>
<?php }?>
</div>
<div id="left">

<?php include('design/leftlinks.php'); ?>
</div>

</div>

<div id="right"></div>
<div id="footer">
<?php include('design/footer.php'); ?>
</div>
</div>

</body>
</html>

NightShift58
12-30-2006, 10:23 PM
You use a new variable, $child, but it isn't defined. Where does it come from? And is it an array?

Also, the following code will never work:foreach ($child AS $key=>$value):
$childfirstname = $childfirstname[$key];
$childlastname = $childlastname[$key];
$childsex = $childsex[$key];
...Each time you assign the value of $childfirstname[$key] to $childfirstname, you are destroying the array $childfirstname[]. During the next pass of the loop, the array will no longer exist as it will now be a string.

You need to take a hard look at your script. There's a lot of cleanup to do.

kproc
12-30-2006, 11:08 PM
the form lets the user enter there childs information, I'm offering a select menue to let the user choose the number of child they have and can enter each one on a seperate form then submit the info to the data base.


How do I get it to add each child to the table. I'm thinking that I need a for each of all form fields but this seems like lots of code.

any help is great

NightShift58
12-31-2006, 09:30 AM
100 years ago would have been more of a problem, but today's families are smaller. So that'll help...

Why don't you change the program flow to give the option of adding one child at a time? What if the user had 3 children last month and a new one was born today? Would the user be able to add the newborn?

For each child, you would have a simpler form and when that form is submitted, the user can go back and add one more, etc.

You can, of course, generate a form with all the children at once. You'll have to define each input field as an array element. When you validate the form, you'll have to cycle through those elements. It's feasible but I'm not sure you'd be doing the user or yourself a favor.

kproc
12-31-2006, 09:43 AM
the way I have it set up know is the user adds one child at a time. I purpose of the loop was so that only one submit button has to be clicked to send all the information.

This is causing me more grief then I wanted I'll leave it as is and tackel this problem another time. thank you for your help and support