Jump to content

Recommended Posts

Posted

page 1 is a form

page 2 is a confirmation page so user can make sure data entered is correct

Form data is put in an array so I can carry it to multiple pages easily.

My array is $rvardata[]

 

My form has fields like:

<input type="text" name="rvardata[name]" size="20">

<input type="text" name="rvardata[address]" size="20">

<input name="rvardata" type="checkbox" value="1">

 

With only a couple of lines I can test or perform actions on all form data collected like:

 

foreach ($rvardata as $key_var =>$value_var) {

$rvardata[$key_var]=HTMLSpecialChars($rvardata[$key_var]);

 

}

 

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.

 

If I reset a value on another page like:

if($rvardata =="1") $rvardata = "brown";

 

Does this mean that $rvardata will move to the end of the array?

 

 

Hope this makes sense. Thanks.

Posted

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.

 

Approaching it from another angle, you can write your database insert to set fields specifically by their names:

 

INSERT INTO table SET field1 = 'value1', field2 = 'value2', . . .

 

In that case I don't think the 'order' of things matters as far as the database is concerned.

 

Hope that helps a bit.

Posted

Actually that is a good idea, I am going to try that for my database.

 

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.

 

This text file looks like this:

 

field 1, field 2, ...fieldn

field 1, field 2, ...fieldn

field 1, field 2, ...fieldn

 

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.

  • 2 weeks later...
Posted

Just a little tip: you don't need to put form information into an array since it is already returned in that way: $_POST :)

 

Also, since you're using the "long" version of foreach, you can access the values of the variables directly

><input type="text" name="name" size="20" />
<input type="text" name="address" size="20" />
<input name="color" type="checkbox" value="1" />

 

>foreach($_POST as $key => $value)
{
  $_POST[$key] = htmlspecialchars($value);
}

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...