HELP ME WITH THIS FORM VALIDATION PLEASE

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
User avatar
rax369
Forum Commoner
Posts: 56
Joined: Sun Oct 06, 2002 8:50 pm

HELP ME WITH THIS FORM VALIDATION PLEASE

Post by rax369 »

I appreciate all the help, I have had from all of u guys... volka, phpScott, hob_goblin, takuma, avatar, mydimension ... etc. etc... and from all of u that I didnt mention 'coz I forgot ur nicks.

But since I continue stuck tryin to do a validation to my form, and what I'm receiving sometimes to the other side, once the form has been submitted... is trash :evil: !... (I'm using the POST method ok ?)

And I said sometimes 'coz when u do not choose a product and its respective quantity what I receive to the other side, the page that process the previous information ... is trash :!: (empty fields)

Look how is my form to get the idea what I'm dealing with:
Image

Now, let me explain u for a while what I a wished to show u w/the above image

... I have named the drop down menus as products[0], products[1]... and so on until products[9] 'coz in the page receiving the info coming from this form (I gonna refer to this page as the 2nd page) I entered all the information coming from the drop down menust in one array called products, and I introduced all the info. coming from the text fields into another array called products_cant. That's why I pointed to the drop down menus and to the combo boxes with red lines in the image, indicating their respective names.

They (the drop down menus and text fields) actually have different names than products and products_cant. But to make the validation easier in the 2nd. page, I introduced all the info into 2 arrays with easy names (products and product_cant, both are arrays as I said).

Now, what I wanna achieve is allow the user to go to the 2nd. page (the page that shows, the product (s) requested, with its/their respective price(s) and quantity(s) ) only if the user chose a product and chose its respective quantity to request. So the 2nd. page could be only accessed if u used the page as is supposted to be used... u cannot choose a product and leave its quantity blank... u cannot choose a product especify its quantity and choose another and leave its quantity field blank, or u cannot especify the quantity of one product and not specify the product by leaving the default message/value of the drop down menu (Choose Product / Remove Product)... got it :?:

If u try to submit the form making one of the mistakes I described above, the page shows u a message saying somethin like "please choose ur product with its respective quantity" or "do not leave blank fields in this form to request ur product"... I dont know... something like that... the point is indicate to the user that s/he left in blank or s/he did something wrong and tried to submit the form with wrong information. (Inside the text boxes, u only can enter digits, thanks to a script volka passed me... I'm gonna try ur script as well phpScott, dont think I didnt appreciate that 'more elegant' code)

The 2nd. page is a confirmation of the products that are being requested in the 1st page. (This is a very rudimentary page I now, but since this is my 1st. "serious" application using PHP/MySQL I dont question myself why I did this or that :lol: ... I know, I know... I have a lot to learn yet, but w/ur help, I'm learning a little of PHP day after day :!: :( )

ufffffff... finally I finished this, hopefully u got what I wanna make w/my 1st. application.

Thx for ur help guys, and to have taken the time to read all this dissertation :oops: :D
User avatar
mydimension
Moderator
Posts: 531
Joined: Tue Apr 23, 2002 6:00 pm
Location: Lowell, MA USA
Contact:

Post by mydimension »

it sounds like you just want to make sure that no product is left without a specifired quantity and vice versa. if that is the case, here is a possible solution:

Code: Select all

for ($i = 1; $i <= 9; $++) {
    if (($productsї$i] == /* default select value */) && ($products_cantї$i] != "")) {
        /* a product field was left empty but the corresponding quantity field had a value */
    }
    if (($productsї$i] != /* default select value */) && ($products_cantї$i] == "")) {
        /* a product field was selected but no quantity was given */
    }
}
note: where you see & replace it with an &

keep in mind this untested (typed it straight into the browser) but the logic should work.
User avatar
rax369
Forum Commoner
Posts: 56
Joined: Sun Oct 06, 2002 8:50 pm

Post by rax369 »

yea, exactly my dimension, I wanna make sure that no product is left without a specifired quantity and vice versa.

Specificly, dont wanna let the user, choose a product without specifing its quantity to be requested and viceversa.

Where should I try that code u gave me mydimension :?: inside of a conditional sentence like if ( ) :?:

I tried specifing without succes this:

Code: Select all

if ( ($_POSTїsubmit] == "Send!")
	AND
	( ($productsї0] != "Choose Product / Remove Product") OR ($productsї1] != "Choose Product / Remove Product") OR ($productsї2] != "Choose Product / Remove Product") OR
	  ($productsї3] != "Choose Product / Remove Product") OR 	($productsї4] != "Choose Product / Remove Product") OR ($productsї5] != "Choose Product / Remove Product") OR 
	  ($productsї6] != "Choose Product / Remove Product") OR ($productsї7] != "Choose Product / Remove Product") OR ($productsї8] != "Choose Product / Remove Product") OR 
	  ($productsї9] != "Choose Product / Remove Product")
	)
	AND
	( !empty($products_cantї0]) OR !empty($products_cantї1]) OR !empty($products_cantї2]) OR !empty($products_cantї3]) OR 
	  !empty($products_cantї4]) OR !empty($products_cantї5]) OR !empty($products_cantї6]) OR !empty($products_cantї7]) OR 
	  !empty($products_cantї8]) OR !empty($products_cantї9])
    )
   )
{
but even doing that long condition, I'm leaving 'holes' where the user could send blank fields...

so let me know where should I try ur script, coz' that must be used as a condition right ? I mean, inside a conditional sentence like if () :?:

thx :D
User avatar
mydimension
Moderator
Posts: 531
Joined: Tue Apr 23, 2002 6:00 pm
Location: Lowell, MA USA
Contact:

Post by mydimension »

well, first off your if statement dosen't pair up the fileds like mine does so i would reccomend you use something similar to mine.

probably the best place for it is somewhere at the top of page 2 and then if something is found empty just send them back to the first page.
when i do something like this i use a session to hold the error data and in the first i look for that error data. if i find it then i display an error message.
User avatar
rax369
Forum Commoner
Posts: 56
Joined: Sun Oct 06, 2002 8:50 pm

Post by rax369 »

Thks man, it appears that I solved the problem using ur code, I did this to validate the entrance to my 'if( )' function.

Code: Select all

$GO = 1;
for ($i = 1; $i <= 9; $i++)
&#123; 
	if (($products&#1111;$i] == "Choose Product / Remove Product") && ($products_cant&#1111;$i] != ""))
	&#123; // no product - qty assigned
		$GO = 0;
	&#125; 

	if (($products&#1111;$i] != "Choose Product / Remove Product") && ($products_cant&#1111;$i] == ""))
	&#123; // product - no qty
		$GO = 0;
	&#125; 
&#125;

// condition submit / products / prods_qty

if ( $_POST&#1111;submit] == "Send!" AND $GO == 1)
&#123;

// process the requested products
...
&#125;
just a question that came to my mind... doing the validation in that way u r not testing the first elements of the arrays... $products[0] neither $products_cant[0] 'coz ur for loop began with 1, not with 0 right ?

and since all the variables which are arrays, begin to store its values in [0], this piece of script, is not validating this option

or am I wrong ?
User avatar
mydimension
Moderator
Posts: 531
Joined: Tue Apr 23, 2002 6:00 pm
Location: Lowell, MA USA
Contact:

Post by mydimension »

that was slip-up on my part ( :oops: ). for this situation your for loop should look like this:

Code: Select all

for ($i = 0; $i <= 9; $i++)
User avatar
rax369
Forum Commoner
Posts: 56
Joined: Sun Oct 06, 2002 8:50 pm

Post by rax369 »

yea ! that's what I did. :D

thx a lot mydimension to help me solving that validation, I tried a lot of ways to do that, but I dont know, may be I'm gonna be a bad programmer coz' my brain, sometimes, doesnt think the way it should do... :oops:

anyway, any problem or doubt I have, I'm gonna ask u for ur help guys.

thx :wink:
User avatar
mydimension
Moderator
Posts: 531
Joined: Tue Apr 23, 2002 6:00 pm
Location: Lowell, MA USA
Contact:

Post by mydimension »

don't worry about making mistakes. ive been programming/scripting for ~4 years and i still make mistakes. just never be afraid to ask for help and you should do fine.
Post Reply