Jump to content

getting the _POST array to distinguish which button was clicked


Bird2819

Recommended Posts

This will be a fleet tracking/maintenance program.  First, the user see's a table of all the equipment and vehicles with send buttons at the end of each record.  The send buttons take the user to a form where he can enter maintenance data.  The form will have the vehicle's name field already filled in to avoid misspelling/data redundancy for maintenance. - I have this working, except..  each send button isn't assigned to their respective vehicle name.  In other words, when I click send, the same vehicle name is filled in for the name field on the form (the last vehicle name in the table). I'm thinking I may need a two dim array and a nested loop to individualize the send buttons, but there must be some tweaking to do with the _POST array in the other file. 

So I'm working with three files - one that displays the table with buttons (assets.php), one that displays a maintenance form with the name value already filled in (add_repair.php), and one that inserts the repair data into the repair table of my database.  Here they are:

 

//assets.php
<?php

//database connection.
mysql_connect("localhost", "root", "xxxxxx") or die(mysql_error());
mysql_select_db("as_maintenance") or die(mysql_error());


//form out of loop

echo '<form action="add_repair.php" method="post">';

$sql="SELECT * FROM asset";

$sql_res=mysql_query($sql)or die(mysql_error());

// loop all from select statement

while($row=mysql_fetch_assoc($sql_res)){
   //echo all the names.
   echo "<input type='text' name='a_name' value='{$row["a_name"]}'>";
   //echo the send buttons
   echo"<input type='submit' name='submit' value='send'>"; 
  }
?> 
////////////////////////////////////////////////////////////////////////////
//add_repair.php
<?php

echo '<form action="repair.php" method="post">';

echo'<fieldset>';
echo '<p><label for="a_name">Asset Name</label> </p>';
echo "<input type='text' name='a_name' value='{$_POST['a_name']}'>";
//echo '<input type="text" name="a_name"/>';
echo '<p><label for="employee_name">Mechanic</label> <input type="text" name="employee_name"/></p>';
echo '<p><label for="repair_date">Date</label> <input type="text" name="repair_date"/></p>';
echo '<p><label for="miles_hours">Miles/Hours</label> <input type="text" name="miles_hours"/></p>';
echo '<p><label for="notes">Notes</label> <input type="text" name="notes"/></p>';
echo '<p><label for="ser_or_vin">Serial-VIN</label> <input type="text" name="ser_or_vin"></p>';
echo '<p class="submit"><input type="submit" value="Submit" /></p>';
echo '</fieldset>';
echo '</form>';

mysql_query("INSERT INTO repair
(a_name,employee_name,repair_date,miles_hours,notes,ser_or_vin)VALUES('$a_name','$employee_name','$repair_date','$miles_hours','$notes',$ser_or_vin')") or die(mysql_error());
echo "Data Inserted!  ";
echo "<a href=\"sa2-05.php\" target=\"_self\">back</a>";

?>
//////////////////////////////////////////////////////////////////////////////////
//repair.php
<?php
mysql_connect("localhost", "root", "xxxxxx") or die(mysql_error());
mysql_select_db("as_maintenance") or die(mysql_error());
echo "Connected!";
// recieving data values from the form repair_form.html
$a_name = $_POST['a_name'];
$employee_name = $_POST['employee_name'];
$repair_date = $_POST['repair_date'];
$miles_hours = $_POST['miles_hours'];
$notes = $_POST['notes'];
$ser_or_vin = $_POST['ser_or_vin'];
// now make the query to input the data to the db
mysql_query("INSERT INTO repair
(a_name,employee_name,repair_date,miles_hours,notes,ser_or_vin)
VALUES('$a_name','$employee_name','$repair_date','$miles_hours','$notes','$ser_or_vin')")
or die (mysql_error());
echo "Data Inserted!  ";
?>   

///////////////////////////////////////////////////////////////////////////////

I'll take any suggestions anyone has.  Thanks very much.

Link to comment
Share on other sites

  each send button isn't assigned to their respective vehicle name.  In other words, when I click send, the same vehicle name is filled in for the name field on the form (the last vehicle name in the table).
i don't take the word same vehicle name

 

1. plz add ENTER to your topic..

 

2. instead using button.. try link

while($row=mysql_fetch_assoc($sql_res)){
   //echo all the names.
   echo "<a href=add_repair.php?name=$row["a_id"]>$row["a_name"]</a>";
   //echo the send buttons
    
  }

 

btw.. i kinda confuse.. can you zip your php work and database (export it)? so we can analyze

Link to comment
Share on other sites

Another option if you need to use a form....put your form buttons into an array...or name them using your database query...use the primary key as the name or something like that.

 

The way you have it now will always default to the first vehicle because the submit button has the same name....you will need a different name when it is contained within the same form tag.

 

That's the only suggestion I have looking at your code.  BTW...you should contain your code in the code tags.

Link to comment
Share on other sites

Thanks for the advice.

This does change the name of each button, but they all still act the same:

 

while($row=mysql_fetch_assoc($sql_res)){
   echo "<input type='text' name='a_name' value='{$row["a_name"]}'>";
   echo"<input type='submit' name='{$row["a_id"]}' value='send'>";  
}

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.