Jump to content

[SOLVED] how to send dynamic value using hidden fields


phplearner2008

Recommended Posts

Hi,

 

I have a php code which outputs data from database based on a search condition which is as follows:

 

<?php

 

echo '<form name="form1" method="post" action="mypage.php">';

 

while($row = mysql_fetch_array($result))

{

        echo $row['ID'];

   

                    echo $row['NAME'];

              echo $row['AGE'];

            echo $row['HEIGHT'];

            echo $row['EDUCATION'];

echo '<input type="submit" name="Submit" value = "submit">';

   

}

echo '</form>';

mysql_close($con);

?>

 

I want to send the ID to be sent to mypage.php whenever Submit button is clicked.

Should I use hidden field? If so, how do I send the ID of each person in the value attribute of hidden field?

 

I want $row['ID'] value to be send to the next page. Can anyone please help me?

You can use different methods for this. One should be sending the id via get in the form action and the other using hidden inputs:

 

Via GET if you have only one variable to send

<?php
//query and stuff
$values = mysql_fetch_array($results);
echo '<form name="form1" id="form1" method="post" action="mypage.php?id=' . $values['id'] . '">';
//other form elements
?>

 

And you can retrieve that id using $_GET['id'] and the other elements with a regular $_POST. In the other hand, if you want to pass multiple ids, use a hidden input as you stated:

<?php
while($row = mysql_fetch_array($result)){
     echo '<input type="hidden" name="id[]" value = "' . $row['id'] . '">';           
}
?>

 

It will generate as many hidden inputs you want, with the name "id[]". The square paranthesis makes the hidden inputs an array, so you can easily call them in mypage.php by using:

<?php
$ids = $_POST['id'];
print_r($ids); //will print the ids as an array, which you can easily loop using foreach()
?>

forget all that use sessions as it safer then seeing any info in the url...........

 

unless your going to use mod_rewrite with $_GET['']..........

 

 

TIP....

 

never use hidden info in the form if it going to insert / update / search anythink to a database so that a NO...........

 

 

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.