0

我正在为一个朋友制作一个基于网络的联系人列表。我已经完成了 html 部分并处理了 PHP 脚本等。我将主页作为表格标记在while循环中。我需要做两件事,但不知道如何完成。

首先,每一行必须有两个提交按钮,一个用于编辑,另一个用于详细信息,并传递全局 $_POST 中的值。

其次,列表大约有 300 行,所以我使用 while 循环来创建表。

我有表单工作并传递数据,但它总是传递表格的最后一行。这是我的带有表格的主页:

<?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>

这将被定向到详细信息或编辑页面。下面是编辑页面......

<?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>

关于如何完成这项工作的任何想法?

4

2 回答 2

1

将表单标签放在循环内。并将提交按钮放在表单标签内。

听起来您需要很多表格,而不是包含很多字段的表格。

还有另一个建议.. jqgrid在这里使用可能是一件好事。如果有机会,请查一下。

于 2012-08-31T23:54:30.690 回答
0

问题是您正在创建大量具有相同名称的元素......每一行都有一个名称为 email1、email2、电话、手机等的输入。

当您提交表单时,它只采用具有给定名称的最后一个 html 元素的值。所以它总是会给你最后一行。

您可以做的是每个属性只有 1 个隐藏输入。然后,当您选择一行时,您可以使用 javascript 设置隐藏输入的值。

但是,为了更简单...您已经将所有用户数据存储在数据库中,因此您无需将其全部传递到下一页。只需将 id 传递到下一页,然后当您到达下一页时执行选择查询以获取该用户的所有数据。

这样你就可以有 1 个隐藏的 id 输入。当用户选择他们想要编辑的行时,使用 Javascript 设置该输入的值。

每个按钮应如下所示:

echo "<input type=\"button\" onclick=\"document.form.id=$id\">";

输入应如下所示:

echo "<input type=\"hidden\" name=\"id\"/>";

然后在编辑页面上使用:

$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...
于 2012-08-31T23:53:02.953 回答