Jump to content

[SOLVED] storing a form in an array


Derleek

Recommended Posts

so i have a input form where a user gets to input 10 different things from a drop down menu.

 

here is the page: www.the3guys.com/moto_game.php

 

username:test

password:4e138c

 

I want to store all of the 10 inputs into an array so i can check to make sure each one is unique (so a person doesn't pick rider4 twice).

 

now, i'm wondering if it would be better to store them straight into the MySQL database?  Maybe have a bool that lets the script know if it is valid or not?

 

anywho, i am basically searching for a nice way to validate the user input for the form above...

Link to comment
Share on other sites

Once the form is submitted, it's already stored in an array, temporarily ($_POST). I assume you're storing accounts in a MySQL db. If you are, then you query for any matching accounts and compare for validation. of course you'll want to also check for invalid characters, string length, and make sure you escape and scrub the input before using it in your query.

Link to comment
Share on other sites

well the way i am setting the DB up (yes its mysql), there's a drop down menu so everything is closed, don't have to do much scrubbing.

 

i'm not being clear enough here.

 

i need to store 10 sets of values, these will be the 10 different choices of a user.  Along with the value of each selection, i need to store the user id, and the order in which it was chosen along with several other variables(which aren't really relevant).

 

I was wondering how to go about storing these 10 choices (in order).  I tried doing a for loop with the form below but...

 

 

here is the form:

<form action="<?php echo $_SERVER['PHP_SELF']?>" method="post">
	<select name="choices1">
		<option value="null">---</option>
		<option value="rider1">Rider1</option>
		<option value="rider2">Rider2</option>
		<option value="rider3">Rider3</option>
		<option value="rider4">Rider4</option>
		<option value="rider5">Rider5</option>
		<option value="rider6">Rider6</option>
		<option value="rider7">Rider7</option>
		<option value="rider8">Rider8</option>
		<option value="rider9">Rider9</option>
		<option value="rider10">Rider10</option>
		</select>
	<br>
	<br>
	<select name="choices2">
	<option value="null">---</option>
		<option value="rider1">Rider1</option>
		<option value="rider2">Rider2</option>
		<option value="rider3">Rider3</option>
		<option value="rider4">Rider4</option>
		<option value="rider5">Rider5</option>
		<option value="rider6">Rider6</option>
		<option value="rider7">Rider7</option>
		<option value="rider8">Rider8</option>
		<option value="rider9">Rider9</option>
		<option value="rider10">Rider10</option>
	</select>
	<br>
	<br>

 

now, i saw somewhere that you can do it like this...

 

<form action="<?php echo $_SERVER['PHP_SELF']?>" method="post">
	<select name="/*change here*/CHOICES[]">
		<option value="null">---</option>
		<option value="rider1">Rider1</option>
		<option value="rider2">Rider2</option>
		<option value="rider3">Rider3</option>
		<option value="rider4">Rider4</option>
		<option value="rider5">Rider5</option>
		<option value="rider6">Rider6</option>
		<option value="rider7">Rider7</option>
		<option value="rider8">Rider8</option>
		<option value="rider9">Rider9</option>
		<option value="rider10">Rider10</option>
		</select>
	<br>
	<br>
	<select name="choices[]">
	<option value="null">---</option>
		<option value="rider1">Rider1</option>
		<option value="rider2">Rider2</option>
		<option value="rider3">Rider3</option>
		<option value="rider4">Rider4</option>
		<option value="rider5">Rider5</option>
		<option value="rider6">Rider6</option>
		<option value="rider7">Rider7</option>
		<option value="rider8">Rider8</option>
		<option value="rider9">Rider9</option>
		<option value="rider10">Rider10</option>
	</select>
	<br>
	<br>

 

but i can't figure out how to access the form using that method.

 

when i do:

 

for($i=1;$i<=10;$i++)
{
	echo "CHOICE #$i". $_POST['choice$i'];
}

or

for($i=1;$i<=10;$i++)
{
	echo "CHOICE #$i". $_POST['choice[$i]'];
}

 

i get nothing out... i'm really frustrated grr

Link to comment
Share on other sites

Name the select tags with "choices[]" (both lowercase, not CHOICES[] and choices[], that's two different things) and then access the choices this way:

 

<?php
$choices = $_POST['choices'];
foreach ($choices as $n => $choice) {
echo 'Choice #', ($n + 1), ': ', $choice, '<br />';
}
?>

Link to comment
Share on other sites

To check if the choices are unique, you can use this:

 

<?php
$choices = $_POST['choices'];
//remove possible "null" values
foreach ($choices as $n => $choice) {
if ($choice == 'null') {
	unset($choices[$n]);
}
}
if (count(array_unique($choices)) == 10) {
echo 'Unique riders, OK!';
} else {
echo 'No go.';
}
?>

Link to comment
Share on other sites

well the way i am setting the DB up (yes its mysql), there's a drop down menu so everything is closed, don't have to do much scrubbing.

 

No, everything is not closed. Remember that users can send anything they want to your processing page, so always sanitize user input.

Link to comment
Share on other sites

could you show me how your 'firebug' script works? that intrigues me

 

It's just an extension/plugin for Firefox. It gives you the ability to edit any source code behind the website you're browsing, but only locally of course - nothing is touched on the server. Neat tool to see real time changes, when editing in the code. Visit http://www.getfirebug.com/ for more info :)

 

Is there an easy one line command to store an array into a MySQL database w/o using a for loop?

 

You can use serialize() and unserialize(), or implode the array on insert, and explode it when taking it back out (given that a character is reserved for use between the elements, for the latter method).

Link to comment
Share on other sites

i read that the serialize function kindof defeats the purpose of using a database, seeing is how it hinders the ability to search and access the data directly.

 

i need to learn more about implode i think.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

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

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  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...

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.