[56K WARN] newbie here - Part management form

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
accident
Forum Newbie
Posts: 5
Joined: Tue Jul 25, 2006 10:08 pm

[56K WARN] newbie here - Part management form

Post by accident »

feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


Hello, I just started using PHP about 2 weeks ago, and while I don't know much of the advanced stuff, I am getting a lot of the basic stuff.

Anyways I ran into a snag in my one page.

I have a part management page, that lists all of the different parts in the database into a table structure (like a datagrid). 



The html form looks like this

Code: Select all

<hidden field> PartID  |  Part #      |  Part Description | Part Price | Part Cost | Threshold |
------------------------------------------------------------------------------------------
1  | 1111 | Part 1  | 100.00 | 78.0| 4    | Edit Delete
2  | 2222 | Part 2  | 50       | 39   | 10  | Edit Delete
etc etc

The edit, and delete are both seperate submit buttons (I will probably be changing them to hyperlinks, but not worrying about that right now).
These buttons takes the user to the php page called editParts.php using POST

The problem I have, Is I currently have a pretty bad way on how I am POSTing the partID
I currently have a loop, which writes a new row to the screen for each part, however I give each part its own form

So it basically looks like this

Code: Select all

for each part
{
<form action="editParts.php" method="post">
<input type="hidden" name="partID" value="<? echo  $part->GetPartID() ?>"/>
<td bgcolor="#999999"><? echo $part->getPartDescription() ?> </td>
etc etc
<input type="submit" name="Submit" value="Edit" id="Submit" />
<input type="submit" name="Submit" value="Delete" id="Submit" />
</form>
}
As you can see each part has its own form... which I don't think is a very effiecient way on doing it.

However I do not know how to only make it 1 form, and being able to figure out the partID based on which button they clicked.
I know It would be really easy if I used GET instead of POST, but I want to use POST.

If any of you could point me in the right direction that would be great. Currently the only way I can figure out how to do it is by using GET. But I dont want people typing in the address bar, and changing the part id to whatever they want.
Thanks


feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

If you are trying to get one form with the listed elements in your code IN that form, just move the opening and closing <form></form> tags outside the loop.
accident
Forum Newbie
Posts: 5
Joined: Tue Jul 25, 2006 10:08 pm

Post by accident »

yes I tried that, but it doesn't work that way.

If i move the form tags to outside the loop, the partID that will get posted will ALWAYS be the last on in the table (so in this case, partID of 2 would always be posted even if I hit the edit button on part id 1)

What i tried doing was add an onclick event to the edit button so like this

Code: Select all

<?php
$onclickEvent = "document.parts.partid.value='" . $part->GetPartID() . "'";
?>
<input type="submit" name="Submit" value="Edit" id="Submit" onClick="<?php echo $onclickEvent?>">
however this isnt working either, it says partid.value is undefined (however does say partID is an object

any other solutions?
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

Are there any methods in the $parts object that return a complete array? If you are only getting the last ID in the table the array set up may need some tweaking.

Can you print_r() the array that you are looping and post the return so I can see the structure?
accident
Forum Newbie
Posts: 5
Joined: Tue Jul 25, 2006 10:08 pm

Post by accident »

alright here is the result of the print_r

Code: Select all

Array ( 
	[0] => part Object ( 
		[partID:private] => 1111 
		[partDescription:private] => TV 
		[partPrice:private] => 500 
		[partCost:private] => 400 
		[partThreshold:private] => 2  
	)
	[1] => part Object ( 
		[partID:private] => 2222 
		[partDescription:private] => Computer 
		[partPrice:private] => 900 
		[partCost:private] => 800 
		[partThreshold:private] => 1  
	) 
)

there are more parts in the array but only put the first 2.

Also i am not quite sure you are understanding my problem (I am a horrible explainer) so I will try again.

I will provide some screenshots, which should help.

Note I am going to use the Company table and screen instead, since my part DB does contain some real data that I should probably not be revealing. THe company screen has all test data.
THe company screen works and looks EXACTLY as the part one, and I am having the same issues with it.

Company.php:
Image
now if i click on ANY of the edit buttons this is what comes up

editCompany.php:
Image

Note: At the very top in the second screenshot is a print_r of $_POST

so I will ALWAYS get the last companyID in the table that will get posted threw.

Now when I think about it, it makes sense why it is doing it.
Because in the loop on the first page, there is multiple

Code: Select all

<input type="hidden" name="companyID" value="<? echo  $part->getCompanyID() ?>">
So company ID would always be set on the last loop iteration.

Here is the code for the company.php page

Code: Select all

<body>

<p>Company Information </p>
<form  action="editCompany.php" method="post">
	<table width="585" border="0"
  	<tr>
    <th width="49" bgcolor="#CCCCCC" scope="col">Company Name  </th>
    <th width="124" bgcolor="#999999" scope="col"><div align="center">Company Abbreviation</div></th>
    <th width="78" bgcolor="#CCCCCC" scope="col">Average Wage</th>
    <th width="100" bgcolor="#999999" scope="col">&nbsp;</th>
  </tr>

<?

function __autoload($class_name)
{
	require_once("$class_name.class.php");
}



$allCompanies = company::getAll();
print_r($allCompanies);
for($i = 0 ; $i < count($allCompanies) ; $i++)
{

	$company = $allCompanies[$i];
?>
  	<tr>
   
    <input type="hidden" name="companyID" value="<?php echo $company->GetCompanyID()?>">
    <td bgcolor="#CCCCCC"> <?php echo $company->getCompanyFullName() ?> </td>
    <td bgcolor="#999999"><div align="center"> <?php echo $company->getCompanyAbbreviation() ?> </div></td>
    <td bgcolor="#CCCCCC"><div align="center"> <?php echo $company->getAverageWage() ?> </div></td>
    <td bgcolor="#999999">
    <input type="submit" name="Submit" value="Edit" id="Submit" />
    <input type="submit" name="Submit" value="Delete" id="Submit" /></td>  	
  	</tr>
<?php
 }

?>

</table>
</form>
<form action="addCompany.php" method=post>


<input type="submit" name="Submit" value="Add Company" id="Submit" />
<input type="button" name="Cancel" value="Cancel" id="Cancel" onclick="window.location='sysMngment.php'"/>

</form>

any solutions you guys can think of?
ANd thanks a ton for the help to
accident
Forum Newbie
Posts: 5
Joined: Tue Jul 25, 2006 10:08 pm

Post by accident »

alright looks like I am just going to have to use get for now as I need to get this done. But I am still interested on how to do it, because I will have time to modify it later.

thanks
Post Reply