Store in hidden form fileds?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
ianhull
Forum Contributor
Posts: 310
Joined: Tue Jun 14, 2005 10:04 am
Location: Hull England UK

Store in hidden form fileds?

Post by ianhull »

Does anyone know how to use php to store all the values from these selects in hidden form fields?

SELECT companyname, address, telephone, fax, email, website, contactname FROM Clients ORDER BY companyname

Thanks
User avatar
Skara
Forum Regular
Posts: 703
Joined: Sat Mar 12, 2005 7:13 pm
Location: US

Post by Skara »

Code: Select all

mysql_connect(host,user,pass);
mysql_select_db(database);
$res = mysql_query("SELECT companyname, address, telephone, fax, email, website, contactname FROM Clients ORDER BY companyname");
mysql_close();
for ($i=0; $data = mysql_fetch_array($res); $i++) {
  foreach ($data as $field => $value) {
    print("<input type='hidden' name='{$field}[$i]' value='{$value}' />");
  }
}
untested
ianhull
Forum Contributor
Posts: 310
Joined: Tue Jun 14, 2005 10:04 am
Location: Hull England UK

Post by ianhull »

Thank you very much Skara,

Really appreciate your help.

Kind regards
ianhull
Forum Contributor
Posts: 310
Joined: Tue Jun 14, 2005 10:04 am
Location: Hull England UK

Post by ianhull »

This works great but can anyone help in retrieving just one row.

I have a form on my first page which has one select box like this

Code: Select all

<form method=&quote;post&quote; action=&quote;page2.php&quote;>
<select name=&quote;companyname&quote;>
<option>company1</option>
<option>company2</option>
</select>
<input type=&quote;submit&quote; value=&quote;Submit&quote;>
</form>
page2.php is where the SELECT from db starts after retrieving the value from my post companyname //hopefully :)

Does anyone have any advise on me trying to store all the row contents in hidden form fields? is this a good way to do things to pass the values?

Thanks in advance

JCART | Please use

Code: Select all

tags when posting html code. Review [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url][/color]
dreamline
Forum Contributor
Posts: 158
Joined: Fri May 28, 2004 2:37 am

Post by dreamline »

Well i'm using hidden fields in the opposite way.. I set them through javascript if a user clicks something and after that i just submit the form so those variables are usable in my next PHP script. I never did fill hidden fields from a query, however i do fill variabels in sessions from a query... :)

Like i said i have used hidden fields to pass date to another page, but came to the conclusion that URL sending those variables is much easier to keep track of what you are doing, but then again using hidden fields to pass date keeps your url quite clean... :)

To get just one row add a where clause in your sqlquery and change the printing of your result to:

Code: Select all

$row=mysql_fetch_array($res); //I always use fetch_array since it's easier to maintain
$value=$row['nameoffield'];
print("<input type='hidden' name='xxx' value='$value'>");
You don't need a loop if you just want to display one row, changing your sql query made sure of that. Ofcourse only if you used a proper where clause that could only return one row.. :) And if it didn't then your echoing will still print only 1 row (the last retrieved) since you are not using a loop to print all rows..

Hope this helps too.. :)

JCART | Please use

Code: Select all

tags when posting php code. Review [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url][/color]
Post Reply