doukuo9116 2012-07-24 06:56 采纳率: 100%
浏览 82
已采纳

从动态生成的列表中在PHP网页之间传递数据

I have a PHP code which generates a dynamic list inside a form like the following, note that the list is built dynamically from database:

echo '<form name="List" action="checkList.php" method="post">';
while($rows=mysqli_fetch_array($sql))
{
echo "<input type='password' name='code' id='code'>";
echo "<input type='hidden' name='SessionID' id='SessionID' value='$rows[0]' />";
echo "<input type='submit' value='Take Survey'>";
}

What I need is to POST the data corresponding to the user choice when he clicks on the button for that row to another page. If we use hyperlinks with query strings there will be no problem as I'll receive the data from the other page using a GET request and the hyperlinks would be static when showed to the user. Also I need to obtain the user input from a textbox which is only possible with POST request.

Simply from the other page (checkList.php) I need these data for further processing:

$SessionID=$_POST['SessionID'];
$Code=$_POST['code'];

As I have a while loop that generates the fields, I always receive the last entry form the database and not the one corresponding to the line (row) that the user chosed from the LIST.

  • 写回答

5条回答 默认 最新

  • dongpu1908 2012-07-24 08:19
    关注

    I'm going to recommend that you clean up the names of variables so that your code can at least tell us what it's supposed to do. It should be rare that someone looks at your code and has a lot of trouble trying to see what you're trying to accomplish :P, ESPECIALLY when you need help with something ;]. I'm going to try some things and hope that it makes doing what you want easier to comprehend and perhaps get you your answer.

    It's good to try your best to not echo large amounts of HTML unnecessarily within a script , so firstly I'm going to remove the echos from where they are not necessary.

    Secondly, I'm going to use a mysql function that returns an easier to process result. $user = mysqli_fetch_assoc($sql)

    Third, I don't know if form having a name actually does anything for the backend or frontend of php, so I'm just going to remove some of the extra crust that you have floating around that is either invalid HTML or just doesn't add any value to what you're trying to do as you've presented it to us.

    And yes, we "note" that you're building something from the database because the code looks like it does =P.

    I'm also sooo sad seeing no recommendations from the other answers in regard to coding style or anything in regard to echoing html like this :(.

    <?php while($user = mysqli_fetch_assoc($sql)): ?>
    <form action="checkList.php" method="post">
        <input type='password' name='code' value='<?php echo $user['code'] ?>' />
        <input type='hidden' name='SessionID'  value='<?php echo $user['id'] //Whatever you named the field that goes here ?>' />
        <input type='submit' value='Take Survey' />
    </form>
    <?php endwhile; ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(4条)

报告相同问题?