简体   繁体   中英

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:&nbsp; </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:&nbsp; </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...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM