繁体   English   中英

PHP表具有循环并将表单数据传递到下一页

[英]PHP table with loop and passing form data to next page

我正在为朋友创建基于Web的联系人列表。 我已经完成了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>

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

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

听起来您需要很多表单,而不是具有很多字段的表单。

还有一个建议.. jqgrid可能是在这里使用的好东西。 如果有机会请查一下。

问题是您正在创建大量具有相同名称的元素...每行都有一个由email1,email2,phone,mobile等名称组成的输入。

当您提交表单时,它只使用具有给定名称的最后一个html元素的值。 因此它将始终给您最后一行。

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

但是,为了使其更简单...您已经将所有用户数据存储在数据库中,因此您无需将所有数据都传递到下一页。 只需将ID传递到下一页,然后在进入下一页时执行选择查询即可获取该用户的所有数据。

这样,您可以为ID提供1个隐藏的输入。 当用户选择要编辑的行时,请使用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...

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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