Data From Form In A Php Array

liunx

Guest
page 1 is a form <br />page 2 is a confirmation page so user can make sure data entered is correct<br />Form data is put in an array so I can carry it to multiple pages easily. <br />My array is $rvardata[]<br /><br />My form has fields like:<br /><input type="text" name="rvardata[name]" size="20"><br /><input type="text" name="rvardata[address]" size="20"><br /><input name="rvardata[color]" type="checkbox" value="1"><br /><br />With only a couple of lines I can test or perform actions on all form data collected like:<br /><br />foreach ($rvardata as $key_var =>$value_var) {<br />$rvardata[$key_var]=HTMLSpecialChars($rvardata[$key_var]);<br /><br />}<br /><br />Now I have a function to write the array to a database on another page. My problem is that how can I predetermine the order of the data in the array. I can put an echo statement in the above code to see the order printed out in a page however I want to create a function that will insert data into a database automatically and my output is not in the same order as the order that the form fields appear on the page.<br /><br />If I reset a value on another page like:<br />if($rvardata[color] =="1") $rvardata[color] = "brown";<br /><br />Does this mean that $rvardata[color] will move to the end of the array?<br /><br /><br />Hope this makes sense. Thanks.<!--content-->
I'm not sure if this is what you're asking, but as far as I know changing the value of an array item doesn't change its position in the array unless you sort the array.<br /><br />Approaching it from another angle, you can write your database insert to set fields specifically by their names:<br /><br /> INSERT INTO table SET field1 = 'value1', field2 = 'value2', . . .<br /><br />In that case I don't think the 'order' of things matters as far as the database is concerned.<br /><br />Hope that helps a bit.<!--content-->
Actually that is a good idea, I am going to try that for my database.<br /><br />I also write to a text file as a backup in case I ever loose the database for some reason. My problem is that I have more than a hundred fields in the form. Most of them are check boxes that users can select. I have to write each field to a file irregardless if they check the item or not and I was hoping to figure out some kink of loop where I can cycle through the $_POST array to get all the fields written. <br /><br />This text file looks like this:<br /><br />field 1, field 2, ...fieldn<br />field 1, field 2, ...fieldn<br />field 1, field 2, ...fieldn<br /><br />I do not have the field names in the file just the data. When I tried the code above the field order was all messed up.<!--content-->
sort($array) would put it in alphabetical order by keyname.<!--content-->
Just a little tip: you don't need to put form information into an array since it is already returned in that way: $_POST <img src="http://www.totalchoicehosting.com/forums/style_emoticons/default/wink.gif" style="vertical-align:middle" emoid=";)" border="0" alt="wink.gif" /><br /><br />Also, since you're using the "long" version of <a href="http://php.net/foreach" target="_blank">foreach</a>, you can access the values of the variables directly<br /><!--c1--><div class='codetop'>CODE</div><div class='codemain'><!--ec1--><input type="text" name="name" size="20" /><br /><input type="text" name="address" size="20" /><br /><input name="color" type="checkbox" value="1" /><!--c2--></div><!--ec2--><br /><br /><!--c1--><div class='codetop'>CODE</div><div class='codemain'><!--ec1-->foreach($_POST as $key => $value)<br />{<br />   $_POST[$key] = htmlspecialchars($value);<br />}<!--c2--></div><!--ec2--><!--content-->
 
Back
Top