簡體   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