Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Print / Echo tables with submit buttons

Status
Not open for further replies.

Bobsmall

Programmer
Jul 25, 2007
9
TT
Hi Guys,
I'm fairly new at PHP and have been faced with a problem, I am doing a website for jobseekers to post resume details and employers to post vacancies. I am using MySQL database and using the adodb wrapper.
The problem is as follows:
the jobseeker can add Work Experience via a link that goes to a form that inserts (SQL insert) the necessary information on the work experience table, this form loops on itself and allows multiple instances of work experiences. Once the user is finished he/she can view the information which is displayed in another form that uses a foreach loop to print tables with input boxes populated by the database corresponding to the multiple work experiences. Up to this point it seems to be working, however each table has an update button and a delete button which should essentially update the particular record based on the input boxes or delete the record. So far I have just coded the update button and when it is clicked, it re-writes all the records that match the logged-in user id to the value that was in the input box for ALL records... The code thus far is:
<?php
session_start();
require('pconn.php');
$usr_id = $_SESSION[user_id];

$sqlwk = $conn->Execute("SELECT F_NAME, EMPLOYER, POSITION, GEN_INFO, JOB_ID FROM jobseeker, work_experience WHERE jobseeker.USER_ID = work_experience.USER_ID AND work_experience.USER_ID = '".$usr_id."'");

include_once('main.php');
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "<html xmlns="<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Work Experience</title>
</head>

<body>
<div class="label">
<h1>Edit/Delete Work Experience</h1>
<h4>Click <a href="wrkexp.php">HERE</a> to ADD to the list OR Click <a href="jobseeker.php">HERE</a> to return to your PROFILE</h4>
<h4 style="color:#FF0000">* Required Fields</h4>
</div>
<div class="content">
<?php

foreach ($sqlwk as $value=>$row){



if (isset($_POST['update'])) {
$employer = trim($_POST['employer']);
$position = trim($_POST['position']);
$description = trim($_POST['description']);
$jb_id = $row['JOB_ID'];
//To test if update is successful
$conn->Execute("UPDATE work_experience SET EMPLOYER='$employer' WHERE USER_ID = '$usr_id' AND JOB_ID = '$jb_id'") or die(mysql_error());
echo $jb_id;


}
print(

"<form action='' method=post name=update >
<table width=441 border=0>


<tr>
<td width=21 align=center valign=bottom style=color:red>*</td>
<td width=189>Employer</td>
<td width=177><input name=employer type=text id=employer size=30 value = '".$row['EMPLOYER']."'> </td>
</tr>
<tr>
<td align=center valign=bottom style=color:red>*</td>
<td>Position</td>
<td><input name=position type=text id=position size=30 value = '".$row['POSITION']."'></td>
</tr>


<tr>
<td align=center valign=bottom>&nbsp;</td>
<td valign=top>Description</td>
<td><textarea name=description cols=30 rows=5 id=description>".$row['GEN_INFO']."</textarea></td>
</tr>
<tr>
<td align=center valign=bottom><div align=center><span class=style1><span class=style2></span></span></div></td>
<td>&nbsp;</td>
<td>&nbsp;</td>
</tr>
<tr align=center valign=bottom>
<td colspan=3>
<div align=center class=style1 style2>

<div align=right>
<input name=update type=submit value=Update>
<input name=delete type=submit value=Delete>
</div>
</div><span class=style2>
</label>
</span>
<div align=center class=style1 style2></label>
</div></td>
</tr>
<tr>
<td colspan=3 align=center valign=bottom style=border-top:thick solid black>&nbsp;</td>
</tr>

</table>
</form>"
);
}
?>
</div>
</body>
</html>


Any help or tips would be greatly appreciated.

Thanks
 
include a hidden field in the form with each submit button that uniquely identifies the *record* and use that hidden field to process the delete command.
 
Thanks for the prompt response, but can you give me an example to illustrate this?
 
Bobsmall,

What I see is u missed one important part, which is what action you want taken on the submit. Ur code reads:
Code:
<form action='' method=post name=update >
Should read something like:
Code:
<form method=POST action=$_SERVER['PHP_SELF']>
If you are calling the code within your parent .php file or:
Code:
<form method=POST action='./actionfile.php'>
If you are calling an external file to process your submit.

NOTE:
PHP_SELF is a little tricky and works differently on different machines based on configuration. Run this simple test inside your code to make sure you have the right syntax:
Code:
<?php phpinfo(); ?>
or
phpinfo();
inside your php code section.

Hope this helps!

YMR

 
Bobsmall,

I forgot one thing. Since ur code has the "<form>" cmd in the HTML body, not assigned to a var in the PHP section, u'll need to use the syntax:
Code:
<form method=POST action="<?php echo $_SERVER['PHP_SELF']; ?>">
You need this for proper substitution of the "PHP_SELF" variable.

YMR
 
Thanks Guys,
I believe I was able to get it to work, this is what i did:

I included a variable to act as a counter (e.g. $i) that increments with each loop of the foreach ($i = ++$i) and subsequently concatenated the name of the input boxes, submit buttons, etc. with the variable so that on each loop the boxes and buttons have a different name and can therefore be updated individually.

Currently, i'm trying to get the page to 'Refresh' on itself because even though the database is updated the same form is displayed with the previous values until it is refreshed. The header function is not working cause I have the main.php included and am getting the 'headers already sent error'
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top