Jump to content

Can variable values be passed more than once? can arrays be passed?


BigD
 Share

Recommended Posts

I have a form calling another form, the 2nd form calls a php code to process something. variables are passed between these 3 pieces of code. The 2nd form receives variables passed to it OK.The 2nd form creates 2 arrays and passes arrays and variables to the 3rd piece. It seems the 3rd piece PHP code can not get variables ans arrays passed from the 2nd form at all. 1st piece(doattendance):<h2>Attendance for : </h2><form action="enterattendance.php" method="post">//<input type="hidden" name="content" value="quarterly"><table width="100%" cellpadding="1" border="1"><?php echo "<tr><td>See all Classes</td>\n"; echo "<td><select name=\"cat\">\n"; $query="SELECT classid,classname from class"; $result=mysql_query($query); while($row=mysql_fetch_array($result,MYSQL_ASSOC)) { $classid = $row['classid']; $classname = $row['classname']; echo "<option value=\"$classid\">$classname</option>\n"; } echo "</select></td></tr>\n";?><tr><td>Enter lesson date (mm/dd/yyyy):</td><td><input type="text" name="ldate" size="10"></td></tr><tr><td>Enter course name (M28,APP17,K10,...):</td><td><input type="text" name="cname" size="8"></td></tr><tr><td>Enter Credit Hours (3.5 or less):</td><td><input type="text" name="chour" size="4" value="3.5"></td></tr></table><input type="submit" name="button" value="Enter Hours"></form> 2nd piece(enterattendance):<form action="addattn.php" method="post"><?php include("../mylibrary/login.php"); login(); $ldate = $_REQUEST['ldate']; $cname = $_REQUEST['cname']; echo "Attendance for $cname on $ldate\n"; echo "<table width=\"100%\" cellpadding=\"1\" border=\"1\">\n"; echo "<tr><td><b>Last Name</b></td><td><b>First Name</b></td><td><b>Attending Status</b></td></tr>\n"; $classid = $_REQUEST['cat']; $query = "SELECT lastname, firstname, stdid"; $query .= " FROM student"; $query .= " where classid = $classid"; $query .= " order by lastname"; $result = mysql_query($query); if (!$result) echo "<h2>SQL error</h2>\n". mysql_error(); $i = 0; while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) { $lastname = $row['lastname']; $firstname = $row['firstname']; $stdid = $row['stdid']; echo "<tr><td>$lastname</td><td>$firstname</td>"; $absent_status = 'unchecked'; $partiallyThere_status = 'unchecked'; $present_status = 'checked'; $hours= 3.5; $atnstd[$i] = $stdid; echo "<td><input type=\"radio\" name=\"group.$i\" value=\"1\" checked> Present</td>"; echo "<td><input type=\"radio\" name=\"group.$i\" value=\"Absent\" > Absent</td>"; echo "<td><input type=\"radio\" name=\"group.$i\" value=\"PartialPresent, enter attended hours:\"> Partially there, hours attended:</td>"; echo "<td><input type=\"text\" name=\"phour\" size=\"4\"></td>"; if (isset($_POST['phour'])) { $selected_radio = $_POST['gender']; if ($selected_radio == 'absent') {$hours = 0; $absent_status = 'checked'; } else if ($selected_radio == 'partiallyThere') {$hours = $phour; $partiallyThere_status = 'checked'; } else if ($selected_radio == 'present') {$hours= 3.5; $present_status = 'checked'; } } $atnhrs[$i] = $hours; $i = $i +1; print_r($atnhrs); } echo "</table>\n";?><input type="submit" name="button" value="update"></form> 3rd piece(addattn):<?php $ldate2 = $_REQUEST['ldate']; $cname = $_REQUEST['cname']; $atnstd = $_POST['atnstd']; $atnhrs = $_POST['atnhrs']; $classid = $_REQUEST['classid'];// $cname = $_POST['cname'];// $ldate = $_POST['ldate']; $stdid = array_shift($atnstd); $hours = array_shift($atnhrs);print_r ($atnhrs); print_r ($atnstd); if (trim($cname) =='') echo "<h2>Course Name can not be blank</h2?\n"; else { if (trim($ldate) =='') echo "<h2>Lesson Date can not be blank</h2?\n"; else { $query="INSERT INTO attendance( classid, stdid, attenddate, hoursearned) VALUES ('$classid', '$stdid', '$ldate', '$hours')"; $result=mysql_query($query);// echo "insert result is $result\n"; if (!$result) { echo "<h2>Problem adding hours</h2>\n"; }else echo "<h2>attending hours added</h2>\n"; } } //}?>

Link to comment
Share on other sites

Sorry my code it too long. Here is a simplified version: xxxxF -> xxxxA -> xxxx2xxxx2 got this error : Notice: Undefined index: grp1 in C:\wamp\www\mschool\admin\radioButton2.php on line 8 xxxxF code :<html><head><title>Radio Buttons</title></head><body><FORM NAME ="form1" METHOD ="POST" ACTION ="radioButtonA.php"><INPUT TYPE = 'Radio' Name ='grp1' value= 'male' >Male<INPUT TYPE = 'Radio' Name ='grp1' value= 'female' >Female<P><INPUT TYPE = "Submit" Name = "Submit1" VALUE = "Submit"></FORM></body></html> xxxxA code:<html><head><title>Radio Buttons action</title></head><FORM NAME ="form2" METHOD ="POST" ACTION ="radioButton2.php"><?PHP$grp1 = $_REQUEST['grp1'];?><INPUT TYPE = "Submit" Name = "Submit1" VALUE = "Next"></FORM></html> xxxx2 code:<html><head><title>Radio Buttons action2</title></head><?PHP$grp1 = $_REQUEST['grp1'];echo "value to next level is $grp1";?></html>

Link to comment
Share on other sites

In xxxxA, you've never defined a form field called "grp1", and therefore, by the time xxxx2 starts, there's no such index, hence the error.Make that:

<?php$grp1 = $_REQUEST['grp1'];?><input name="grp1" value="<?php echo $grp1; ?>" />

Link to comment
Share on other sites

Thanks Moderator, that works. I tried to pass arrays this way, got error: Notice: Undefined index: aray2 in C:\wamp\www\mschool\admin\radioButtonA.php on line 8 Call Stack Please see what is wrong. xxxxF:<html><head><title>Radio Buttons</title></head><FORM NAME ="form1" METHOD ="POST" ACTION ="radioButtonA.php"><INPUT TYPE = 'Radio' Name ='grp1' value= 'male' >Male<INPUT TYPE = 'Radio' Name ='grp1' value= 'female' >Female$aray2 = array("Tom", "Jan");<input name="aray2" value="<?php echo $aray2; ?>" /><INPUT TYPE = "Submit" Name = "Submit1" VALUE = "Submit"></FORM></html> xxxxA: <html><head><title>Radio Buttons action</title></head><FORM NAME ="form2" METHOD ="GET" ACTION ="radioButton2.php"><?PHP$aray2 = $_REQUEST['aray2'];$grp2 = $_REQUEST['grp1'];?><input name="grp2" value="<?php echo $grp2; ?>" /><input name="aray2" value="<?php echo $aray2; ?>" /><INPUT TYPE = "Submit" Name = "Submit1" VALUE = "Next"></FORM></html> xxxx2:<html><head><title>Radio Buttons action2</title></head><?PHP$aray3 = $_REQUEST['aray2'];$grp3 = $_REQUEST['grp2'];echo "value to next level is $grp3";?></html>

Link to comment
Share on other sites

I made a little progress and got rid of the error message. My array was not created correctly in xxxxF. I am still not getting array passed.Here are the codes: xxxxF:<html><head><title>Radio Buttons</title></head><FORM NAME ="form1" METHOD ="POST" ACTION ="radioButtonA.php"><INPUT TYPE = 'Radio' Name ='grp1' value= 'male' >Male<INPUT TYPE = 'Radio' Name ='grp1' value= 'female' >Female<?PHP $aray2 = array("Tom", "Jan"); ?><input name="aray2" value="<?php echo $aray2; ?>" /><?PHPprint_r($aray2);?><INPUT TYPE = "Submit" Name = "Submit1" VALUE = "Submit"></FORM></html> xxxxA:<html><head><title>Radio Buttons action</title></head><FORM NAME ="form2" METHOD ="PUT" ACTION ="radioButton2.php"><?PHP$aray2 = $_REQUEST['aray2'];$grp2 = $_REQUEST['grp1'];print_r($aray2);?><input name="grp2" value="<?php echo $grp2; ?>" /><input name="aray2" value="<?php echo $aray2; ?>" /><INPUT TYPE = "Submit" Name = "Submit1" VALUE = "Next"></FORM></html> xxxx2: I will post it once I got array passed to xxxxF correclty.

Link to comment
Share on other sites

When you print an array it prints the word "array". You should print a serialized version of the array in the form value, get that value on the other page, and use unserialize to turn it back into an array. Or, store it in the session instead and skip serialization. http://www.php.net/manual/en/function.serialize.phphttp://www.php.net/manual/en/function.unserialize.php

  • Like 1
Link to comment
Share on other sites

xxxxF: By Single Form

<html><head><title>Radio Buttons</title></head><FORM NAME ="form1" METHOD ="POST" ACTION ="radioButtonA.php"><?PHP $aray2 = array("Tom", "Jan");	foreach ($aray2 as $i => $value)	{		echo "<br/><input name=\"name".$i."\" value=\"".$value."\" />";		echo "<INPUT TYPE = \"Radio\" Name =\"grp".$i."\" value= \"male\" >Male";		echo "<INPUT TYPE = \"Radio\" Name =\"grp".$i."\" value= \"female\" >Female";	   $maxarray=$i;	}   echo "<br/><input type="hidden" name=\"maxarray"\" value=\"".$maxarray."\" />";//<-----Use Max array For Form xxxxA With out Session/* echo "<br/>";	print_r($aray2);	echo "<br/>"; */?><br/><INPUT TYPE = "Submit" Name = "Submit1" VALUE = "Submit"></FORM></html>

Edited by kanchatchai
Link to comment
Share on other sites

Or USExxxxF: By Multi Form

<html><head><title>Radio Buttons</title></head><body><?PHP$aray2 = array("Tom", "Jan");    foreach ($aray2 as $i => $value)    {?><p><FORM NAME ="form<?PHP echo $i;?>" METHOD ="POST" ACTION ="radioButtonA.php"><?PHP        echo "<input name=\"name".$i."\" value=\"".$value."\" />";        echo "<INPUT TYPE = \"Radio\" Name =\"grp".$i."\" value= \"male\" >Male";        echo "<INPUT TYPE = \"Radio\" Name =\"grp".$i."\" value= \"female\" >Female";        echo "<INPUT TYPE = \"Submit\" Name = \"Submit".$i."\" VALUE = \"Submit\">";/* echo "<br/>";    print_r($aray2);    echo "<br/>"; */?></FORM></p><?PHP    }?></html>

Link to comment
Share on other sites

May Be: Change

  echo "<INPUT TYPE = \"Submit\" Name = \"Submit".$i."\" VALUE = \"Submit\">"; 

to

  echo "<INPUT TYPE = \"Submit\" Name = \"Submit\" VALUE = \"Submit\">"; 

and Use new variable for verification.

<html><head><title>Radio Buttons</title></head><body><?PHP$aray2 = array("Tom", "Jan");    foreach ($aray2 as $i => $value)    {?><p><FORM NAME ="form<?PHP echo $i;?>" METHOD ="POST" ACTION ="radioButtonA.php"><?PHP	    echo "<input name=\"name".$i."\" value=\"".$value."\" />";	    echo "<INPUT TYPE = \"Radio\" Name =\"grp".$i."\" value= \"male\" >Male";	    echo "<INPUT TYPE = \"Radio\" Name =\"grp".$i."\" value= \"female\" >Female";	    echo "ID: <input name=\"id\" value=\"".$i."\" />";	    echo "<INPUT TYPE = \"Submit\" Name = \"Submit\" VALUE = \"Submit\">";/* echo "<br/>";    print_r($aray2);    echo "<br/>"; */?></FORM></p><?PHP    }?></html>

Link to comment
Share on other sites

I am able to pass array now. Thanks to every one who posted. Here is what I changed: xxxxF:<html><head><title>Radio Buttons</title></head><FORM NAME ="form1" METHOD ="POST" ACTION ="radioButtonA.php"><INPUT TYPE = 'Radio' Name ='grp1' value= 'male' >Male<INPUT TYPE = 'Radio' Name ='grp1' value= 'female' >Female<?PHP$aray2 = array("Tom", "Jan");foreach ($aray2 as $key => $value){echo '<input type=hidden name="aray2[]" value="'.htmlspecialchars($value).'">';}//$grp1 = $_REQUEST['grp1'];//<input name="grp1" value="<?php echo $grp1; ?>" />print_r($aray2);?><INPUT TYPE = "Submit" Name = "Submit1" VALUE = "Submit"></FORM></html>

Link to comment
Share on other sites

Do you want like this.xxxF

<html><head><title>Radio Buttons</title></head><FORM NAME ="form1" METHOD ="POST" ACTION ="xxxxa.php"><?PHP $aray2 = array("Tom", "Jan");	    foreach ($aray2 as $i => $value)	    {			    echo "<br/><input name=\"name".$i."\" value=\"".$value."\" />";			    echo "<INPUT TYPE = \"Radio\" Name =\"grp".$i."\" value= \"male\" >Male";			    echo "<INPUT TYPE = \"Radio\" Name =\"grp".$i."\" value= \"female\" >Female";		   $maxarray=$i;	    }   echo "<br/><input type=\"hidden\" name=\"maxarray\"  value=\"".$maxarray."\" />";/* echo "<br/>";	    print_r($aray2);	    echo "<br/>"; */?><br/><INPUT TYPE = "Submit" Name = "Submit1" VALUE = "Submit"></FORM></html>

xxxA.php

<html><head><title>Radio Buttons action</title></head><FORM NAME ="form2" METHOD ="PUT" ACTION ="radioButton2.php"><?PHPif (isset($_REQUEST["maxarray"])){for ($i = 0; $i <= $_REQUEST["maxarray"]; $i++){    echo "<br/><input name=\"name".$i."\" value=\"".$_REQUEST["name".$i]."\" />";    if ($_REQUEST["grp".$i]=="male")    {    echo "<INPUT TYPE = \"Radio\" Name =\"grp".$i."\" value= \"male\" checked=\"checked\">Male";    echo "<INPUT TYPE = \"Radio\" Name =\"grp".$i."\" value= \"female\">Female";    }    else    {    echo "<INPUT TYPE = \"Radio\" Name =\"grp".$i."\" value= \"male\" >Male";    echo "<INPUT TYPE = \"Radio\" Name =\"grp".$i."\" value= \"female\" checked=\"checked\">Female";        }     //echo $_REQUEST["grp".$i];}}//print_r($aray2);?><br/><INPUT TYPE = "Submit" Name = "Submit1" VALUE = "Next"></FORM></html>

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...