Inserting Dynamic amount of rows from Form into Table - php

Form:
<table width="100%" cellpadding="4" cellspacing="0" id="px">
<thead>
<tr class="tab_head">
<td width="25px" style="text-align:center; color:#FFFFFF; font-size:11px;">#</td>
<td class="form_label" style="color:#FFFFFF; font-size:11px;">First Name</td>
<td class="form_label" style="color:#FFFFFF; font-size:11px;">Last Name</td>
<td class="form_label" style="color:#FFFFFF; font-size:11px;">Wgt</td>
<td class="form_label" style="color:#FFFFFF; font-size:11px;">Address</td>
<td class="form_label" style="color:#FFFFFF; font-size:11px;">Phone</td>
<td class="form_label" style="color:#FFFFFF; font-size:11px;">Email</td>
<td></td>
</tr>
</thead>
<tbody id="tbl1">
<tr id="pass">
<td style="vertical-align:middle; text-align:center"><input type="text" name="pass_num[]" readonly="readonly" value="1" class="pass" style="font:Verdana; font-size:11px; border:none; background-color:transparent; width:25px; text-align:center; vertical-align:middle;" tabindex="-1"></td>
<td><input type="text" id="cfn1" name="cust_fname[]" class="form_g" /></td>
<td><input type="text" id="cln1" name="cust_lname[]" class="form_g" /></td>
<td><input type="text" name="cust_kg[]" class="form_a" /></td>
<td><input type="text" name="cust_addr[]" class="form_c" /></td>
<td><input type="text" name="cust_phone[]" class="form_g" /></td>
<td><input type="text" name="cust_email[]" class="form_b" /></td>
<td style="vertical-align:middle"><a class="removePax"><img src="images/remove_pax.png" /></a></td></tr>
</tbody>
</table>
Table Columns:
cust_id
cust_fname
cust_lname
cust_kg
cust_addr
cust_phone
cust_email
There is a snippet of JQuery that essentially clones the above table as required to allow the user to add another passenger etc. On submittinh, the data is posted to the next page fine, and I've tested numerous times that the data is coming through in Arrays (eg. $_POST[cust_fname] etc.).
The problem I'm having now is turning these into sets that I can insert into the table.
I tried a foreach loop and iterating but so far, I'm failing to come up with a solution. I have tried sorting the data into per passenger, but so far keeps clumping the data together.
JSFiddle

In your php code, you have to iterate with one for loop all the arrays in the same time and print them. Example :
cust_fname = $_POST["cust_fname"];
cust_lname = $_POST["cust_lname"];
for($i = 0; $i < count($cust_fname); $i++)
{
echo "Complete name : ".$cust_fname[$i]." ".$cust_lname[$i];
echo "<br/>";
}
I just put an example for two fields, you can do the rest :)
EDIT : I saw that you want to show them in a table, so you can change the code i gave you to :
cust_fname = $_POST["cust_fname"];
cust_lname = $_POST["cust_lname"];
echo "<table><tr><th>First name</th><th>Last Name</th></tr><tbody>";
for($i = 0; $i < count($cust_fname); $i++)
{
echo "<tr>";
echo "<td>".$cust_fname[$i]."</td><td> ".$cust_lname[$i]."</td>";
echo "</tr>";
}
echo "</tbody></table">;

Related

PHP input field instant update after typing in values

just trying to make a little calculator for myself in php and i wonder if its possible to make the answer instant update after i type in the values?
Thank you.
<?php
if (isset($_POST['valuea'])) $valuea = $_POST['valuea'];
if (isset($_POST['valueb'])) $valueb = $_POST['valueb'];
if (isset($_POST['check1'])) {
$answer = (($valuea * $valueb) * 10)*2;
} else {
$answer = ($valuea * $valueb) * 10;
}
echo <<<_END
<form method='post' action=''>
<table border='0' width='500px' cellpadding='3' cellspacing='1' class="table">
<tr class="calcrow">
<td>Lenght:</td>
<td align="center"><input type='text' name='valuea' value="$valuea"/></td>
</tr>
<tr class="calcrow2">
<td>Width:</td>
<td align="center"><input type='text' name='valueb' value="$valueb"/></td>
</tr>
<tr class="calcrow2">
<td>2x test</td>
<td align="center"><input type="checkbox" name="check1"></td>
</tr>
<tr class="submit"><td colspan="2"><input type='submit' value='Calculate'/></td></tr>
_END;
?>
<tr class="calcrow">
<td><i>Answer</td>
<td align="center"><input type="text" value="<?php echo round($answer)?>"></td></i>
</tr>
</table>
</form>
You are looking for a thing called Jquery.
Which can fetch current Values of your form inputs or any other elements from your webpage. Then you can then send that data to your php file where you can perform the calculation and return the result which can again be displayed wherever you want .
All of this happens in real time.
This is the way you could do it if you wanted to do it in php
Else you could always go for plain JavaScript or a mixture of jquery + javascript, to avoid using php and ajax.

Mysql query to store employee attendance by month

How do I insert all employee attendance(Monthly once) in mysql database
Here is my db tables
att_employee_id | att_year | att_month | att_present_days | att_absent_days | att_workingdays
Date.prototype.daysInThisMonthOfThisYear=function() {
return new Date(this.getFullYear(),this.getMonth()+1,0).getDate();
}
var days_inmonth = new Date().daysInThisMonthOfThisYear();
$(document).ready(function() {
$( ".absent_days" ).keyup(function() {
var days = $(this).val();
$(this).closest("tr").find(".present_days" ).val( days_inmonth - days );
})
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<form name="emp_att" action="" method="post">
<table>
<thead>
<tr>
<th class="head0" style="width:5%">S.No</th>
<th class="head1" style="width:15%">Employee ID</th>
<th class="head0 no-sort" style="width:15%">No.of days present</th>
<th class="head1 no-sort" style="width:15%">No.of days absent</th>
</tr>
</thead>
<tbody>
<tr>
<td>1</td>
<td>ETO0001</td>
<td><input type="text" class="present_days" name="present_days" value=""></td>
<td><input type="number" name="absent_days" class="absent_days" min="0" max="<?php echo date('t'); ?>" onChange="absentdays()" style="width:100%"></td>
</tr>
<tr>
<td>2</td>
<td>ETO0002</td>
<td><input type="text" class="present_days" name="present_days" value=""></td>
<td><input type="number" name="absent_days" class="absent_days" min="0" max="<?php echo date('t'); ?>" onChange="absentdays()" style="width:100%"></td>
</tr>
<tr>
<td>3</td>
<td>ETO0003</td>
<td><input type="text" class="present_days" name="present_days" value=""></td>
<td><input type="number" name="absent_days" class="absent_days" min="0" max="<?php echo date('t'); ?>" onChange="absentdays()" style="width:100%"></td>
</tr>
<tr>
<td>4</td>
<td>ETO0004</td>
<td><input type="text" class="present_days" name="present_days" value="" readonly></td>
<td><input type="number" name="absent_days" class="absent_days" min="0" max="<?php echo date('t'); ?>" onChange="absentdays()" style="width:100%"></td>
</tr>
</tbody>
</table>
<input type="submit" name="submit" value="Submit">
</form>
Here is my query to get all the employees
<form name="emp_att" action="" method="post">
<table>
<thead>
<tr>
<th class="head0" style="width:5%">S.No</th>
<th class="head1" style="width:15%">Employee ID</th>
<th class="head0 no-sort" style="width:15%">No.of days present</th>
<th class="head1 no-sort" style="width:15%">No.of days absent</th>
</tr>
</thead>
<tbody>
<?php
$allemp= mysqli_query($con,"select * from t_emp e");
$all_emp = mysqli_num_rows($allemp);
if ($all_emp > 0)
{
$i=1;
while($all_emp = mysqli_fetch_assoc($allemp))
{
echo "<tr>";
echo "<td>".$i."<input type='hidden' name='serialno' value='".$i++."'></td>
<td> ".$all_emp["emp_id"]."</td>
<td><input type='text' class='present_days' name='present_days' value=''></td>";
echo "<td><input type='number' name='absent_days' class='absent_days' min='0' max= date('t') onChange='absentdays()' style='width:100%'></a></td>";
echo "</tr>";
}
}
else
{
echo "0 Results";
}
?>
</tbody>
</table>
<input type="submit" name="submit" value="Submit">
</form>
Here I am getting all the n number of employees in the above form. when entered all employees "No.of days absent" and then hit on submit I am able to send only last record to the database.
Can any one help me to send all the employees attendance to database
Here is my form submission query
<?php
if(isset($_POST['submit']))
{
$serialno=$_post['serialno'];
$employee_id=$_post['emp_id'];
$att_year = date('Y');
$att_month = date('m');
for($i=1; $i<= $serialno;$i++)
{
$present_days=$_POST['present_days'];
$absent_days=$_POST['absent_days'];
}
$query=mysqli_query($con,"INSERT INTO t_emp_attendance(att_employee_id,att_year,att_month,att_present_days,att_absent_days)values('$employee_id','$att_year','$att_month','$present_days','$absent_days')");
}
?>
any suggestions will be appreciated, I need to store all the employees attendance to database
Check that name of input fields are created. It might be overwritten with the latest values. Send it as an array format like "absent_days[]" it will solve your problem
Check you spelling for INSERT. And also you have missed to pass the connection object for inserting
First parameter should be the connection object in mysqli_*
mysqli_query(connectionObject,your Query);
$query=mysqli_query("INSERT INTO t_emp_attendance(att_employee_id,att_year,att_month,att_present_days,att_absent_days)values('$employee_id','$att_year','$att_month','$present_days','$absent_days')");
for($i=1; $i<= $serialno;$i++)
{
$present_days=$_POST['present_days'][$i];
$absent_days=$_POST['absent_days'][$i];
$query=mysqli_query($con,"INSERT INTO t_emp_attendance(att_employee_id,att_year,att_month,att_present_days,att_absent_days)values('$employee_id','$att_year','$att_month','$present_days','$absent_days')");
}
Your query should be inside the loop. And as suggested by parag, make text field names as arrays []
Soomething like this,
<input type="text" class="present_days" name="present_days[]" value="">
^ ^
You shud do for all the text boxes.

Hidden Default Form Values

I've got a simple form that I would like to be able to add a hidden value that is only used in the event the user doesn't enter a value. The values are for simple text input fields and can use HTML, PHP, JavaScript, JQuery, or any combination thereof. Let me explain further that the value attributes are already set to contain the values in the DB for that form currently. However, in the event there is no current value in the DB for that specific field and the user does not enter a value themselves, I would like a default value to be passed in the POST.
I've done some research and found examples of custom attributes, etc... but these will not work for me. As I'm using an array builder that passes all of the fields in the form as a single array to a new function. Any help would be greatly appreciated!
The code shown below is the creation of the form... As you can see two separate fields are created redundantly for however many rounds exist within the tournament. I've also provided output HTML code of a two round form. In the event the DB has NULL values for the field in question, and if the user doesn't enter a value of his/her own, I would like to pass the value of "false".
$rchkpos = array();
$rchkscore = array();
for ($i=0; $i<=$tournament['numRounds']; $i++)
{
$rchkthis = array("$i" => "round".$i."pos");
$rchkthis2 = array("$i" => "round".$i."score");
$rchkpos = array_merge($rchkpos, $rchkthis);
$rchkscore = array_merge($rchkscore, $rchkthis2);
if ($i > 0)
{
$roundpos = $rchkpos[$i];
$roundscore = $rchkscore[$i];
if (!is_null($stats2[$roundpos])){
$roundposvar = "$stats2[$roundpos]";
}else{
$roundposvar = false;
}
if (!is_null($stats2[$roundscore])){
$roundscorevar = "$stats2[$roundscore]";
}else{
$roundscorevar = false;
}
$team_stats.="
<tr valign='top'>
<td align='center'>" . LANG_MAN_ROUND . " $i " . LANG_MAN_POSITION . "</td>
<td class='alt1' align='center'>
<input type='text' name='round[".$rchkpos[$i]."]' value='$stats2[$roundpos]' size='40' maxlength='5' />
</td>
</tr>
<tr valign='top'>
<td align='center'>" . LANG_MAN_ROUND . " $i " . LANG_MAN_SCORE . "</td>
<td class='alt1' align='center'>
<input type='text' name='round[".$rchkscore[$i]."]' value='$stats2[$roundscore]' size='40' maxlength='5' />
</td>
</tr>";
}
}
Output HTML Example:
<tbody><tr valign="top">
<td align="center">Round 1 Position</td>
<td align="center" class="alt1">
<input type="text" maxlength="5" size="40" value="2" name="round[round1pos]">
</td>
</tr>
<tr valign="top">
<td align="center">Round 1 Score</td>
<td align="center" class="alt1">
<input type="text" maxlength="5" size="40" value="false" name="round[round1score]">
</td>
</tr>
<tr valign="top">
<td align="center">Round 2 Position</td>
<td align="center" class="alt1">
<input type="text" maxlength="5" size="40" value="false" name="round[round2pos]">
</td>
</tr>
<tr valign="top">
<td align="center">Round 2 Score</td>
<td align="center" class="alt1">
<input type="text" maxlength="5" size="40" value="false" name="round[round2score]">
</td>
</tr>
</tbody>
You can possibly do it like this:
HTML
<input type="text" name="name" />
<input type="hidden" name="name_default" value="name_default" />
PHP (after form submit)
if(!isset($_POST['name'])) {
$_POST['name'] = $_POST['name_default'];
}
Or make an aray like this:
$array_check = array('name', 'telephone', 'gender');
foreach($array_check AS $key => $value) {
if(!isset($_POST[$value])) {
$_POST[$value] = $_POST[$value."_default"];
}
}
Keep in mind that users can edit the values of the hidden inputs (with developer tools like Firebug), always check the input!
Other possible approach is to set the default values in a dedicated property in each html input and then replace the value of the field before the post using jQuery:
// before post
$('input').each(function () {
if ($(this).val() == '')
$(this).val($(this).attr('defaultvalue'));
});

when i add a comment i get this warning

hello guys i have a problem with this code
when id add a comment and comment insert in database
and when i make refresh on the same page i get this warning :
One of the fields are still empty,
i think the problem in the first code
<?php
if ($_POST['add'] and $_POST['add']=='comm'){
$comm_name =strip_tags($_POST['comm_name']);
$comm_country =strip_tags(mysql_real_escape_string($_POST['comm_country']));
$c =strip_tags(mysql_real_escape_string($_POST['comm']));
$comm_thread =strip_tags(mysql_real_escape_string($_POST['comm_thread']));
$status =$_POST['status'];
$getidtopic=$_GET['id_topic'];
$post_code=$_POST['post_code'];
if ($comm_name=='' or $comm_country=='' or $c=='' or $post_code=='' ){
echo "<script>alert(\"One of the fields are still empty
\");</script>";
}else if ($_POST['post_code']==$_SESSION['code']) {
$insertcomm=mysql_query("insert into comments values('','$comm_name','$comm_country','$comm','$comm_thread','$status') ")or die (mysql_error);
echo "<script>alert(\"your comment has been adding\");</script>";
}
}
?>
and this is the comment's form
<form action='' method='post' >
<table class='rightcol' width='100%' cellpadding='0' cellspacing='5'>
<tr>
<td colspan='3' id='addcomm'>add comm</td>
</tr>
<tr>
<td width='15%' ><div id='title_comm' value=''>name : </div></td>
<td ><input type='text' name='comm_name' value='<?if (!$insertcomm){
echo $comm_name;
}?>'/></td>
</tr>
<tr>
<td width='15%' ><div id='title_comm'>country </div></td>
<td ><input type='text' name='comm_country'
value='<?if (!$insertcomm){echo $comm_country;}?>'/>
</td>
</tr>
<tr>
<td valign='top' width='15%'><div id='title_comm'>comment : </div></td>
<td width='50%'>
<textarea cols='55' rows='12' name='comm'>
<?if (!$insertcomm){echo $c;}?>
</textarea></td>
<td valign='top' ><div id='note_comm'>
your comment will not insert if you try to use some thing bad
</div></td>
</tr>
<tr>
<td width='15%' ><div id='title_comm'><span style='color:red'>code : <br/>write these codes </span></div></td>
<td ><input type='text' name='post_code'/></td>
</tr>
<tr>
<td ><div id='code'>
<?php
$text=rand(400,80000);
echo $_SESSION['code']=$text;
?>
</div></td>
</tr>
<td colspan='4' ><input type='submit' name='addcomm' id='add' value='add comm'/></td>
</table>
<input type='hidden' name='comm_thread' value='<?php echo $getidtopic;?>' />
<input type='hidden' name='add' value='comm'/>
<input type='hidden' name='status' value='2'/>
</form>
that is because, when you refresh the page, the $_POST fields are reset, and the fields get empty, so when the page executes its PHP line if ($comm_name=='' or $comm_country=='' or $c=='' or $post_code=='' or $post_code=='' ){ it will find them to be empty.
You need to show us the table definition to better figure out what the issue is exactly. My guess is that your SQL statement is not properly prepared.

PHP table with loop and passing form data to next page

I am working on a web based contact list for a friend. I have the html portion all done and working on the PHP scripts and such. I have the main page as a table in a while loop enclosed in form tags. I need two things to happen but not sure how to get this accomplished.
First, each row has to have two submit buttons, which one goes to edit and the other to details, and carries over the values in the global $_POST.
Second, the list will be about 300 rows, so i am using a while loop to create the table.
I have the form working and passing the data but it is always passing the last row of the table. Here is my main page with the table:
<?php
if
(!isset ($_SESSION['username']))
{
session_start();
}
?>
<html>
<head>
<title>Client Contact List</title>
</head>
<?php
$user1 = implode(',',$_SESSION);
//DB information
require_once('/includes/db.php');
//Declaring edit and details
$edit = "<INPUT type='image' src='/addressbook/images/edit.png' onclick='\addressbook\edit.php'>";
$details = "<INPUT type='image' src='/addressbook/images/contact.gif' name='details' onclick='f1.action='\addressbook\contact_details.php'>";
//Table declarations and such
mysql_connect("$host", "$username", "$password") or die(mysql_error());
mysql_select_db("$db_name")or die("cannot select DB");
$result = mysql_query("SELECT * FROM contacts") or die(mysql_error());
$num=mysql_numrows($result);
$user1 = implode(',',$_SESSION);
$userresults = "SELECT first FROM i_user where userid IN $user1";
$user = mysql_query($userresults);
// print_r ($_SESSION);
// print_r ($_POST);
?>
<body style="background-image: url('Images/background_login.jpg');">
<br><br><br><br><br><br>
<table>
<br><br>
<tr><td width="500">Welcome Back, <?php echo $user; ?></td><td width="500"></td><td width="300"><form name="search" method="post" id="searchform" action="<?php echo $_SERVER['PHP_SELF'];?>"><label for="searchtext">Search: </label><input type="text" name="name" /> <input type="submit" name="submit" value="Search" /></form>
</td></tr>
</table>
<br>
<form name="f1" method="post" action="/addressbook/edit.php">
<table border="1">
<tr>
<?php
echo "<table border='1'>";
echo "<tr>
<th>First</th>
<th>Last</th>
<th>Company</th>
<th>Primary Email</th>
<th>Secondary Email</th>
<th>Primary Phone</th>
<th>Second Phone</th>
<th>Action</th>
</tr>";
$i=0;
while ($i<$num) {
$id = mysql_result($result,$i,"id");
$first = mysql_result($result, $i, "first");
$last = mysql_result($result,$i, "last");
$company = mysql_result($result, $i, "company");
$email1 = mysql_result($result,$i, "email1");
$email2 = mysql_result($result,$i, "email2");
$phone = mysql_result($result,$i, "phone");
$mobile = mysql_result($result,$i, "mobile");
// Print out the contents of each row into a table
echo "<tr><td width = '100'><center><input type='hidden' value='$first' name='first'>";
echo $first;
echo "</center></td><td width = '100'><center><input type='hidden' value='$last' name='last'>";
echo $last;
echo "</center></td><td width = '100'><center><input type='hidden' value='$company' name='company'>";
echo $company;
echo "</center></td><td width = '100'><center><input type='hidden' value='$email1' name='email1'>";
echo $email1;
echo "</center></td><td width = '100'><center><input type='hidden' value='$email2' name='email2'>";
echo $email2;
echo "</center></td><td width = '100'><center><input type='hidden' value='$phone' name='phone'>";
echo $phone;
echo "</center></td><td width = '100'><center><input type='hidden' value='$mobile name='mobile'>";
echo $mobile;
echo "</center></td><td width = '100'><center>";
echo $edit;
echo " &nbsp&nbsp ";
echo $details;
echo "</td></center></tr>";
echo "<input type='hidden' value='$id name='id'></td>";
$i++;
}
?>
</tr>
</table>
</form>
</body>
</html>
This get directed to either the details or edit page. Below is the edit page....
<?php
if
(!isset ($_SESSION['username']))
{
session_start();
}
?>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Edit Contact Information</title>
</head>
<?php
//DB information
require_once('/includes/db.php');
mysql_connect("$host", "$username", "$password") or die(mysql_error());
mysql_select_db("$db_name")or die("cannot select DB");
$id = $_POST['id'];
$first = $_POST['first'];
$last = $_POST['last'];
$company = $_POST['company'];
$email1 = $_POST['email1'];
$email2 = $_POST['email2'];
$phone = $_POST['phone'];
$mobile = $_POST['mobile'];
//pulling the record id from the main login page.
$first=$_POST['first'];
$query="SELECT * FROM contacts where last=$last";
$result=mysql_query($query);
print_r($_POST);
?>
<html xmlns="http://www.w3.org/1999/xhtml">
<body style="background-image: url('Images/background_login.jpg');">
<br><br><br><br><br>
<!-- First Table with the back and search option but disabled for now -->
<table>
<br>
<tr>
<td width="500">
<input type='button' value='Back' onClick='history.go(-1)'>
</td>
<td width="500"></td>
<td width="300">
<!-- <form name="search" method="post" id="searchform" action="<?php echo $_SERVER['PHP_SELF'];?>">
<label for="searchtext">Search: </label>
<input type="text" name="name" /> <input type="submit" name="submit" value="Search" />
</form> -->
</td>
</tr>
</table>
<br><br>
<center>
<!-- Second Table with form data pulled out for Identify -->
<table>
<tr>
<th>
<table>
<tr>
<td bgcolor="silver" colspan="4"><center>Identify</center></td>
</tr>
<tr>
<td width="100"><center><b>Title</b></center></td>
<td width="100"></td>
<td width="150"><center><b>Company Name</b></center></td>
</tr>
<tr>
<td width="100"><input value="Title"></td>
<td width="100"></td>
<td width="100"><? echo $company ?></td>
</tr>
<tr><td colspan="4"></td></tr>
<tr>
<td width="100"><center><b>First Name</b></center></td>
<td width="100"></td>
<td width="100"><center><b>Last Name</b></center></td>
</tr>
<tr>
<td width="100"><input value="Test"></td>
<td width="100"></td>
<td width="100"><input value="Test"></td>
</tr>
</table>
</th> <!-- Space between the contact info and Indenty -->
<td width="100">
</td>
<th>
<td> <!-- Third Table with form data pulled out -->
<table>
<tr>
<td bgcolor="silver" colspan="4"><center>Contact Information</center></td>
</tr>
<tr>
<td width="100"><center><b>Office Phone</b></center></td>
<td width="100"></td>
<td width="150"><center><b>Mobile Name</b></center></td>
</tr>
<tr>
<td width="100"><input value="Test"></td>
<td width="100"></td>
<td width="100"><input value="Test"></td>
</tr>
<tr>
<td colspan="4"></td>
</tr>
<tr>
<td width="100"><b>Primary Email</b></td>
<td width="100"></td>
<td width="150"><b>Secondary Email</b></td>
</tr>
<tr>
<td width="100"><input value="Test"></td>
<td width="100"></td>
<td width="100"><input value="Test"></td>
</tr>
</table>
</td>
</th>
<tr height="100"> <td colspan="9"></td> </tr>
<th class="style2">
<table>
<tr width="400"></tr>
<tr>
<td bgcolor="silver" colspan="4"><center>Applications Used</center></td>
</tr>
<tr>
<td width="100"></td>
</tr>
<tr>
<td colspan="4"></td>
</tr>
<tr>
<td width="100"></td>
</tr>
<tr>
<td width="100"></td>
<td width="100"></td>
<td width="100"></td>
<td width="100"></td>
</tr>
</table>
</th>
<td width="200"></td>
<td>
<th class="style2">
<table>
<tr>
<td bgcolor="silver" colspan="4"><center>Internal Information</center></td>
</tr>
<tr>
<td width="100"><center><b>Account Mgr</b></center></td>
<td width="100"></td>
<td width="150"><center><b>Client Relations</b></center></td>
</tr>
<tr>
<td width="100"><input value="Test"></td>
<td width="100"></td>
<td width="100"><input value="Test"></td>
</tr>
<tr><td colspan="4"></td></tr>
<tr>
<td width="200"><center><b>Acct Development</b></center></td>
<td width="100"></td>
<td width="100"><center><b>Project Mgr</b></center></td>
</tr>
<tr>
<td width="100"><input value="Test"></td>
<td width="100"></td>
<td width="100"><input value="Test"></td>
</tr>
</table>
</th>
</td>
</table>
</center>
</body>
</html>
Any thoughts on how to get this done?
Put the form tag inside the loop. And place the submit button inside the form tag.
It sounds like you need a lot of form, instead of a form with a lot of field.
Yet another suggestion.. jqgrid may be a good thing to use here. look it up if you got a chance.
The problem is you are creating a ton of elements that have the same name... Every row has a input by the name of email1, email2, phone, mobile, etc.
when you submit the form it just takes the value of the last html element with the given name. So it will alwyas give you the last row.
What you can do is only have 1 hidden input for each attribute. Then when you select a row, you can set the values for the hidden inputs using javascript.
HOWEVER, to make it more simple... You are already storing all of the users data in a database so you don't need to pass it all to the next page. Just pass the id to the next page, then when you get to the next page perform a select query to get all of that user's data.
This way you can have 1 hidden input for the id. When the user selects the row they want to edit use Javascript to set the value of that input.
Each button should look something like this:
echo "<input type=\"button\" onclick=\"document.form.id=$id\">";
The input should look something like this:
echo "<input type=\"hidden\" name=\"id\"/>";
Then on the edit page use :
$id = $_POST['id'];
$query = "SELECT id, first, last, company, email1, email2, phone, mobile
FROM contacts WHERE ID=$id"
$result = mysql_query($query) or die(mysql_error());
$row = mysql_fetch_array($result);
$id = $row['id'];
$first = $row['first'];
etc...

Categories