简体   繁体   English

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

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

I am working on a web based contact list for a friend. 我正在为朋友创建基于Web的联系人列表。 I have the html portion all done and working on the PHP scripts and such. 我已经完成了html部分,并且正在处理PHP脚本等。 I have the main page as a table in a while loop enclosed in form tags. 我将主页面作为表格包含在表单标记中的while循环中。 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. 首先,每一行必须有两个提交按钮,一个用于编辑,另一个用于详细信息,并继承全局$ _POST中的值。

Second, the list will be about 300 rows, so i am using a while loop to create the table. 其次,列表将大约有300行,因此我正在使用while循环来创建表。

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. 还有一个建议.. jqgrid可能是在这里使用的好东西。 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. 问题是您正在创建大量具有相同名称的元素...每行都有一个由email1,email2,phone,mobile等名称组成的输入。

when you submit the form it just takes the value of the last html element with the given name. 当您提交表单时,它只使用具有给定名称的最后一个html元素的值。 So it will alwyas give you the last row. 因此它将始终给您最后一行。

What you can do is only have 1 hidden input for each attribute. 您可以做的就是每个属性只有1个隐藏的输入。 Then when you select a row, you can set the values for the hidden inputs using javascript. 然后,当您选择一行时,可以使用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. 只需将ID传递到下一页,然后在进入下一页时执行选择查询即可获取该用户的所有数据。

This way you can have 1 hidden input for the id. 这样,您可以为ID提供1个隐藏的输入。 When the user selects the row they want to edit use Javascript to set the value of that input. 当用户选择要编辑的行时,请使用Javascript设置该输入的值。

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

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

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