Page 1 of 1

Store in hidden form fileds?

Posted: Wed Jun 15, 2005 9:42 am
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

Posted: Wed Jun 15, 2005 10:51 am
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

Posted: Wed Jun 15, 2005 1:28 pm
by ianhull
Thank you very much Skara,

Really appreciate your help.

Kind regards

Posted: Wed Jun 15, 2005 3:36 pm
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]

Posted: Sat Jun 18, 2005 10:37 am
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]