user1483799
user1483799

Reputation: 409

Passing data between PHP webpages from a dynamically generated list

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.

Upvotes: 1

Views: 617

Answers (5)

Anther
Anther

Reputation: 1844

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

Upvotes: 2

Alexander Larikov
Alexander Larikov

Reputation: 2339

$form = 1;
while($rows=mysqli_fetch_array($sql))
{

echo '<form name="List_$form" action="checkList.php" method="post">';
echo "<input type='password' name='code' id='code_$form'>";
echo "<input type='hidden' name='SessionID' id='SessionID_$form' value='$rows[0]' />";
echo "<input type='submit' value='Take Survey'>";
echo '</form>';
$form++;
}

Upvotes: 0

user1423506
user1423506

Reputation:

i not sure this is correct

echo '<form name="List" method="post">';
while($rows=mysqli_fetch_array($result))
{
echo "<input type='password' name='code' id='code'>";
echo "<input type='button' value='Take Survey' onclick=show($rows[0])>";
echo "<br>";
}

and javascript

<script>
function show(id)
{
alert(id);
window.location="checkList.php?id="+id;
}
</script>

On checkList.php

$id=$_GET['id'];
echo $id;

Upvotes: 2

Kode Plus
Kode Plus

Reputation: 706

You can try something like this:

while($rows=mysqli_fetch_array($sql))
{
$index = 1;
echo "<input type='password' name='code' id='code'>";
//Attach $index with SessionID
echo "<input type='hidden' name='SessionID_$index' id='SessionID' value='$rows[0]' />";
echo "<input type='submit' value='Take Survey'>";
}

On checkList.php

 <?php 
  $num = explode('_', $_POST['SessionID']);
  $num = $num[1];
  //in $num you will get the number of row where you can perform action

 ?>

Upvotes: 1

dpitkevics
dpitkevics

Reputation: 1258

You can just check in checkList.php whether $_POST['code'] exists and if exists retrieve $_POST['SessionID'] which will be generated from database. But one thing, if You have all hidden fields in one form, they all will be sent, so You need to think how to correct that - maybe seperate forms for each hidden field, submit button and maybe other POST fields. And afterwards, You will be able to get data in the way You need - $SessionID=$_POST['SessionID']; I suppose it is the easiest way to solve that.

Upvotes: 1

Related Questions