?
Solved

adding a data row to an html table every time a POST occurs

Posted on 2008-06-18
7
Medium Priority
?
194 Views
Last Modified: 2013-11-19
Hi, I have a form where the user enters the training courses they have taken. the scenario goes this way:
1. user enters info about course1
2. user submits the form
3. a data row is added to the table at the top of the page (displaying info about course1)
4. user enters info about course2
5. user submits form
6. another data row is added to the end of the table (displaying info about course2)
.... and so on

now, I have written code that will enable me to add the first row correctly. but, when I add the second row, it will overwrite the values on the first row.

I need a solution where the the second row will be appended to the table instead of rewriting it.

this is my code:
<table name="CoursesTable" border="1" cellpadding="5">
 
<tr style="font-weight:bold">
<td>'3E 'D/H1)</td>
<td>'DE-'61</td>
<td>9FH'F 'D/H1)</td>
<td>'D4G1</td>
<td>'D3F)</td>
</tr>
 
<tr>
 
<?php
if($_SERVER['REQUEST_METHOD'] == 'POST' && isset($_POST['process']) && $_POST['process'] == 1 && $ErrorMessage!='%F '3E 'D/H1) EC11')
 
{						
		$f = 0;
	foreach($_POST as $val) {
		$myinf[$f] = $val;
		$f++;
	}
 
 
for($i = 0;$i < count($myinf)-2;$i++) //-2 is to disregard the buttons and the hidden field
 {
echo <<<EOT
<td>$myinf[$i]</td> 
EOT;
}
}?>
 
</tr>
 
<!-- end of CoursesTable -->
</table>

Open in new window

0
Comment
Question by:ChefMaha
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 2
7 Comments
 
LVL 5

Accepted Solution

by:
mms_master earned 400 total points
ID: 21812222
Hi,

Your table is only displaying data it has received from a form via POST. Each time the form is submitted it is only sending the data in the form at the time, which is why the first course is being overwritten. You can solve this by:

a) Using sessions to store the courses that are sent via POST. (E.g. Form Submitted > course info from POST added to session > table data read from session)

b) Same as a, but use a database instead of sessions.

c) Use hidden values in your form, then use a while loop to check how many courses have been submitted in order to read the data. (E.g. Each time a course is submitted add a set of hidden inputs containing its data. Then do something like:

$i = 1;

while (isset($_POST["CourseName" . $i])) {
    //Display course info

    //Increment $i to check for the next course
    $i++;
}

//NOTE: The form inputs will have to have a number appended to the end to show which course they are for. (E.g first submitted course name would be <input name='CoursName1'> second submitted course name would be <input name='CourseName2'> and so on)

mms_master
0
 

Author Comment

by:ChefMaha
ID: 21812499
ok thanks pal

just give me some time to test and I'll get back to your after the weekend.

take care
0
 

Author Comment

by:ChefMaha
ID: 21836823
I got this error:
Parse error: syntax error, unexpected '+', expecting ']' in /home/adhd/public_html/traininginfo.php on line 211
<?php
 
require_once('connection.php'); 
 
/**
  * This function can be used to check the sanity of variables
  *
  * @access private
  *
  * @param string $type  The type of variable can be bool, float, numeric, string, array, or object
  * @param string $string The variable name you would like to check
  * @param string $length The maximum length of the variable
  *
  * return bool
  */
 
  function sanityCheck($string, $type, $maxlength, $minlength){
 
  // assign the type
  $type = 'is_'.$type;
 
  $string = trim($string);
  
  if(!$type($string))
    {
    return FALSE;
    }
  // now we see if there is anything in the string
  elseif(empty($string))
    {
    return FALSE;
    }
  // then we check how long the string is
  elseif(strlen($string) > $maxlength || strlen($string) < $minlength)
    {
    return FALSE;
    }
  else
    {
     return true;
 
    }
}
 
 
 // checks that all required fields are filled
 
  function checkSet(){
  return (isset($_POST['CourseName'], $_POST['CourseInstructor']) && $_POST['CourseMonth'] != 'Month' &&  $_POST['CourseYear'] != 'Year');
}
 
 
//if form is submitted (not first time for page to load)
if($_SERVER['REQUEST_METHOD'] == 'POST' && isset($_POST['process']) && $_POST['process'] == 1)
 
{
$Email = $_SESSION['session_email'];
 
$CourseName = $_POST['CourseDetail'];
$CourseInstructor = $_POST['CourseDetail_2'];
$CourseTitle = $_POST['CourseDetail_3'];
 $CourseMonth = $_POST['CourseDetail_4'];
 $CourseYear = $_POST['CourseDetail_5'];
 
 $Counter = 0; 
 	foreach($_POST as $val) {
	
	if($Counter<5)
	{
	 $Courses[$NumberOfPosts][$Counter] = $val;
		$Counter++;
	}
	}
 
 
 
//if(checkSet())
//{
 
 if(sanityCheck($_POST['CourseName'], 'string', 70, 2) )
{
	if(sanityCheck($_POST['CourseInstructor'], 'string',  100, 3))
	{
		        if(!sanityCheck($_POST['CourseTitle'], 'string', 70, 3))
					{
						$CourseTitle='';
					}
					
					$Flag = 1;
					$NumberOfPosts++;
															$strSQL = "INSERT INTO VOLUNTEER_TRAINING Values ('" . $Email 															. "','" . $CourseName . "','" .$CourseInstructor . "', '" .																$CourseTitle . "', '" .$CourseMonth . "', '" .$CourseYear . "')" ; 
 
															mysql_query("set names 'utf8'");
															
														$strResult = mysql_query($strSQL) or 
														$ErrorMessage='%F '3E 'D/H1) EC11';
									}
								else
									{
										$ErrorMessage = '%F 'DE-'61 :J1 5-J-';
									}
								}
									
									
						else
					{
		
						$ErrorMessage ='%F '3E 'D/H1) :J1 5-J-';
					}
          //}
	
// else
//			{
					//$ErrorMessage = "A6D'K 'ED# ,EJ9 'D-BHD 'D61H1J)";
//					$ErrorMessage = $Email;
//			} 
	}
	
	//if first time page loaded
	if($_POST['process'] != 1)
	{
		$NumberOfPosts = 0;
		
		$Courses = array();
		
		$Flag = 0; //boolean flag to make sure the CoursesTable displays only the rows inserted in the db (0 = no db record inserted; 1 = db record inserted successfully)
	}
	
?>
 
<?php require_once('header.php'); ?>
 
<!-- cell part of outer table -->
<td dir="rtl" valign="top">
 
<!-- beginning of table containing all body contents -->
<table align="center" cellpadding="10" name="BodyTable">
 
<tr>
 
<!-- beginning of first cell inside BodyTable -->
<td valign="top">
 
<!-- beginning of links table -->
<table cellpadding="5" align="right" name = "LinksTable">
<tr width="250">
<td align="right">
<a href="personalinfo.php" style="color: #E2E2E2">E9DHE'* 4.5J)</a>
</td>
</tr>
 
<tr width="200px">
<td align="right">
<a href="traininginfo.php" style="color: #D75A27">'D/H1'* HH14 'D9ED</a>
</td>
</tr>
 
<tr width="200px">
<td align="right">
<a href="languages.php" style="color: #E2E2E2">D:'*</a>
</td>
</tr>
 
<tr width="200px">
<td align="right">
<a href="preferredwork.php" style="color: #E2E2E2">'D#9E'D 'D*7H9J) 'DEA6D)</a>
</td>
</tr>
 
<tr width="200px">
<td align="right">
<a href="skills.php" style="color: #E2E2E2">EG'1'*</a>
</td>
</tr>
 
<!-- end of links table -->
</table>
 
<!-- end of first cell inside BodyTable -->
</td>
 
<!-- beginning of second cell inside BodyTable -->
<td>
 
<table name="CoursesTable" border="1" cellpadding="5">
 
<tr style="font-weight:bold">
<td>'3E 'D/H1)</td>
<td>'DE-'61</td>
<td>9FH'F 'D/H1)</td>
<td>'D4G1</td>
<td>'D3F)</td>
</tr>
 
 
<?php
 
if($_SERVER['REQUEST_METHOD'] == 'POST' && isset($_POST['process']) && $_POST['process'] == 1 && $ErrorMessage!='%F '3E 'D/H1) EC11')
 
{						 
	foreach($_POST as $val) {
		if(!empty($val)) {
		$myinf[] = $val;
		}
	}
  
for($i = 0;$i < count($myinf)-2;$i+=3) //-2 is to disregard the buttons and the hidden field
 {
echo <<<EOT
<tr><td>$myinf[$i]</td><td>$myinf[$i+1]</td><td>$myinf[$i+2]</td></tr> 
EOT;
}
 
session_register("myinf") ;
 
}
 
/*
//if($_POST['process'] == 1 && $ErrorMessage!='%F '3E 'D/H1) EC11' && $Flag == 1)
 
//{						
//for($i = 0;$i < $NumberOfPosts; $i++)
// {
?>
<tr>
 <?php
 
 for($j=0; $j<5; $j++)
 {
echo 
<<<EOT
<td>$Courses[$i][$j]</td>
EOT;
}
$Flag = 0;
?>
 
</tr>
 
<?php
//}
//} */
?>
 
 
 
<!-- end of CoursesTable -->
</table>
 
<?php
$ErrorMessage = $Email;
 if(ErrorMessage != "") 
{ echo '<p style="color: #D75A27; text-align: center; font-weight:bold">' .  htmlspecialchars($ErrorMessage) . '</p>'; } ?> 
 
 
 
<form name="thisForm" id="thisForm" accept-charset="utf-8"; method="post"; action="<?php echo $PHP_SELF; ?>" ; enctype="multipart/form-data: charset=utf-8 " > 
 
<FIELDSET style="width:300">
<LEGEND style="color:296566"><b>'D/H1'* H H14 'D9ED 'DE,*'2)</b></LEGEND>
 
<br>
 
<!-- beginning of FormTable -->
<table cellpadding=3px class="form" name="FormTable" align="right">
<tr>
<td width=150px>
'3E 'D/H1)
</td>
<td>
<input type="text" name="CourseDetail" value="<?php echo $CourseName ?>" />
</td>
</tr>
 
 
<tr>
<td>
&#1575;&#1604;&#1605;&#1581;&#1575;&#1590;&#1585;
</td>
<td>
<input type="text" name="CourseDetail_2" value="<?php echo $CourseInstructor ?>" />
</td>
</tr>
 
<tr>
<td>
9FH'FG'
</td>
<td>
<input type="text" name="CourseDetail_3" value="<?php echo $CourseTitle ?>" />
</td>
</tr>
 
<tr>
<td>
*'1J. 'F9B'/G'
</td>
<td>
<select name="CourseDetail_4">
<option value="Month">4G1</option>
<option value="01"<?php if($CourseMonth == "01"){echo " SELECTED";}?>>01</option>
<option value="02"<?php if($CourseMonth == "02"){echo " SELECTED";}?>>02</option>
<option value="03"<?php if($CourseMonth == "03"){echo " SELECTED";}?>>03</option>
<option value="04"<?php if($CourseMonth == "04"){echo " SELECTED";}?>>04</option>
<option value="05"<?php if($CourseMonth== "05"){echo " SELECTED";}?>>05</option>
<option value="06"<?php if($CourseMonth == "06"){echo " SELECTED";}?>>06</option>
<option value="07"<?php if($CourseMonth == "07"){echo " SELECTED";}?>>07</option>
<option value="08"<?php if($CourseMonth == "08"){echo " SELECTED";}?>>08</option>
<option value="09"<?php if($CourseMonth== "09"){echo " SELECTED";}?>>09</option>
<option value="10"<?php if($CourseMonth== "10"){echo " SELECTED";}?>>10</option>
<option value="11"<?php if($CourseMonth == "11"){echo " SELECTED";}?>>11</option>
<option value="12"<?php if($CourseMonth == "12"){echo " SELECTED";}?>>12</option>
</select>
 
<select name="CourseDetail_5">
<option value="Year">3F)</option>
<option value="08"<?php if($CourseYear == "08"){echo " SELECTED";}?>>08</option>
<option value="07"<?php if($CourseYear == "07"){echo " SELECTED";}?>>07</option>
<option value="06"<?php if($CourseYear == "06"){echo " SELECTED";}?>>06</option>
<option value="05"<?php if($CourseYear == "05"){echo " SELECTED";}?>>05</option>
<option value="04"<?php if($CourseYear == "04"){echo " SELECTED";}?>>04</option>
<option value="03"<?php if($CourseYear == "03"){echo " SELECTED";}?>>03</option>
<option value="02"<?php if($CourseYear == "02"){echo " SELECTED";}?>>02</option>
<option value="01"<?php if($CourseYear == "01"){echo " SELECTED";}?>>01</option>
<option value="00"<?php if($CourseYear == "00"){echo " SELECTED";}?>>00</option>
<option value="99"<?php if($CourseYear == "99"){echo " SELECTED";}?>>99</option>
<option value="98"<?php if($CourseYear == "98"){echo " SELECTED";}?>>98</option>
<option value="97"<?php if($CourseYear == "97"){echo " SELECTED";}?>>97</option>
<option value="96"<?php if($CourseYear == "96"){echo " SELECTED";}?>>96</option>
<option value="95"<?php if($CourseYear == "95"){echo " SELECTED";}?>>95</option>
<option value="94"<?php if($CourseYear == "94"){echo " SELECTED";}?>>94</option>
<option value="93"<?php if($CourseYear == "93"){echo " SELECTED";}?>>93</option>
<option value="92"<?php if($CourseYear == "92"){echo " SELECTED";}?>>92</option>
<option value="91"<?php if($CourseYear == "91"){echo " SELECTED";}?>>91</option>
<option value="90"<?php if($CourseYear == "90"){echo " SELECTED";}?>>90</option>
<option value="89"<?php if($CourseYear == "89"){echo " SELECTED";}?>>89</option>
<option value="88"<?php if($CourseYear == "88"){echo " SELECTED";}?>>88</option>
<option value="87"<?php if($CourseYear == "87"){echo " SELECTED";}?>>87</option>
<option value="86"<?php if($CourseYear == "86"){echo " SELECTED";}?>>86</option>
<option value="85"<?php if($CourseYear == "85"){echo " SELECTED";}?>>85</option>
<option value="84"<?php if($CourseYear == "84"){echo " SELECTED";}?>>84</option>
<option value="83"<?php if($CourseYear == "83"){echo " SELECTED";}?>>83</option>
<option value="82"<?php if($CourseYear == "82"){echo " SELECTED";}?>>82</option>
<option value="81"<?php if($CourseYear == "81"){echo " SELECTED";}?>>81</option>
<option value="80"<?php if($CourseYear == "80"){echo " SELECTED";}?>>80</option>
<option value="79"<?php if($CourseYear == "79"){echo " SELECTED";}?>>79</option>
<option value="78"<?php if($CourseYear == "78"){echo " SELECTED";}?>>78</option>
<option value="77"<?php if($CourseYear == "77"){echo " SELECTED";}?>>77</option>
<option value="76"<?php if($CourseYear == "76"){echo " SELECTED";}?>>76</option>
<option value="75"<?php if($CourseYear == "75"){echo " SELECTED";}?>>75</option>
</select>
 
</td>
</tr>
 
<tr ><td align="center" colspan="2">
<input type="hidden" name="process" value="1">
<br>
<input type="submit" name="Save" size="10" value="-A8" />
 
</td></tr> 
</table>
 
</FIELDSET>
 
<br>
 
<a href="languages.php" style="color: #106E6A" align="left" ><b>'D*'DJ >> </b></a>
 
<!-- end of second cell inside BodyTable -->
</td>
 
</tr>
 
<!-- end of BodyTable -->
</table>
</form>
 
<!-- end of mid cell in outer table -->
</td>
 
 
<td width="155" bgcolor="#296566" valign="top"><img src="images/swirl.jpg" /></td>
 
</tr>
 
<?php require_once('footer.php'); ?>

Open in new window

0
WordPress Tutorial 2: Terminology

An important part of learning any new piece of software is understanding the terminology it uses. Thankfully WordPress uses fairly simple names for everything that make it easy to start using the software.

 

Author Comment

by:ChefMaha
ID: 21836829
I'm sorry the previous post was a mistake. it was meant to be posted for another question.

I'm going to test your code and let you know

thanks
0
 

Author Comment

by:ChefMaha
ID: 21837145
Hi,

I think I like option b the best. Can you further explain it to me with some code?

thank you
0
 
LVL 5

Expert Comment

by:mms_master
ID: 21844523
Hi,

Im a bit confused... ChefMaha was waiting for me to give some example code. Which I was going to attempt to get done today. (I was unable to do it over the weekend). I recevied an email this morning saying that the asker has requested that this question be deleted. Two minutes later a received another email saying that my answer has been accepted.

So what happens now? Is the question going to be deleted? If it is, will the points I was awarded be removed? and ChefMaha I am guessing that you no longer want/need the code example?

mms_master
0
 

Author Comment

by:ChefMaha
ID: 21844590
Hi mms_master,

I really appreciate your help. Your answer contained many different methods of doing one thing. However, since you were late in providing me with the code example and I needed to get things done quickly, I had to look for the solution elsewhere.

now, thanks to my Lord, I have got my problem solved. and I was able to achieve the task with your second option (retrieve from database). So, you gave me part of the solution. the other part (which is the code), I got it from other solutions.

that's why I accepted your solution as an "incomplete solution". So, I guess your solution is accepted now. (anyway even if i did delete the question, it won't be deleted automatically - it'll give the experts a last chance to reply)

thanks for your help mms_master. I appreciate it.
0

Featured Post

Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

SASS allows you to treat your CSS code in a more OOP way. Let's have a look on how you can structure your code in order for it to be easily maintained and reused.
This article discusses how to implement server side field validation and display customized error messages to the client.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.
Suggested Courses

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question