Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Disable post items if empty 3

Status
Not open for further replies.

ZendDeveloper

Programmer
Jan 3, 2009
15
GB
Hi,

I have a form that posts quite a lot of data through to a php page. What I want to do is iterate through an array that is posted and if the array element has a blank value disable it before the form is actually posted so it doesn't get processed by the php script.

For example, I might have a form with these values in the post request:

products[001] = NULL
products[002] = NULL
products[003] = 1
products[004] = NULL
products[005] = 2

In this case, only products[003] and products[005] should actually be posted.

Can anyone point me in the right direction on this. I need a function that will strip the others from teh form when I call an onclick event from the form.

Thanks
 
Hi

And what if the user agent submits something like this ?
[tt]
products[002] = ZendDeveloper+should+not+trust+anything+received+from+client-side
[/tt]
You should implement the skipping of invalid values on server-side.

Feherke.
 
Hi Feherke,

I will indeed be checking the values server side so that in itself isn't a problem. I have been asked to strip them out client side before the post which is why I asked.

The serverside app is quite intensive and the form that will be posted here will be quite large hence why we would prefer to only post fields that have been populated.

Thanks anyway, always good to have someone point out the obvious but oh so easy to forget!
 
Hi

Neither processing by PHP itself or skipping by your script should not be significant. But as you wish.

Anyway, so far you told us nothing about your [tt]form[/tt]. What kind of [tt]form[/tt] elements are whose products ?

Generally speaking, I would start with something like this :
Code:
[b]function[/b] [COLOR=darkgoldenrod]makeskip[/color][teal]([/teal]formref[teal])[/teal]
[teal]{[/teal]
  [b]for[/b] [teal]([/teal][b]var[/b] i[teal]=[/teal][purple]0[/purple][teal],[/teal]l[teal]=[/teal]formref[teal].[/teal]elements[teal].[/teal]length[teal];[/teal]i[teal]<[/teal]l[teal];[/teal]i[teal]++)[/teal] [teal]{[/teal]
    [b]if[/b] [teal]([/teal]formref[teal].[/teal]elements[teal][[/teal]i[teal]].[/teal]value[teal]==[/teal][green][i]''[/i][/green][teal])[/teal] formref[teal].[/teal]elements[teal][[/teal]i[teal]].[/teal]name[teal]=[/teal][green][i]''[/i][/green]
[teal]}[/teal]
The condition may need to be changed based on the [tt]form[/tt] element types. But the base idea is simple : [tt]form[/tt] elements with empty [tt]name[/tt] are not submitted.

Feherke.
 
Hi again,

The form values are quantities, purely numeric.

I'm trying your code and will let you know how I got on.

Thanks again
 
Sorry, don't know why I misunderstood you there!

They are input boxes.
 
If you use the format for array indices that you showed in your first post (products[001]), you may have problems when you come to products[008]. In Javascript, any number that starts with a zero is handled as octal, and 08 and 09 will crash a script and prevent anything beyond that point from being processed.

Lee
 
Actually, I better clarify exactly what is being passed - I'm not having much luck and that's probably because my data was misleading.

Ok, so here is some actual data that is being passed through the form:

Code:
<input name="super_group[1951][740]" value="0" class="input-text qty" type="text">
<input name="super_group[1951][842]" value="0" class="input-text qty" type="text">
<input name="super_group[1951][866]" value="1" class="input-text qty" type="text">
<input name="super_group[1952][866]" value="0" class="input-text qty" type="text">

As you can see, this is a multi dimensional array. The first array dimension is a range pointer for a product catalogue and the second is a group pointer for the product.

When the value is not 0, I want this to be included in the post, if it is a 0 then I want it to be stripped out from the POST data.

Don't know how easy this is but any assistance would be gratefully received.

Thanks
 
Try something like this:

Code:
function validate(formref){
var element_count=formref.elements.length;
for(var i=0; i<=element_count; i++){
if(formref.elements[i].value==0){
formref.elements[i].disabled=true;
}
}
return true;
}

and call it in the onSubmit event of your form.

Code:
<form ... onSubmit="validate(this);">

This will just run through any and all elements of the form, if their value is 0 it will disable them, so the browser won't send them when it POSTS.





----------------------------------
Phil AKA Vacunita
----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.
 
Thanks Vacunita,

How would I do this so it only goes through the super_group post items rather than all post elements in the form?

Also, with the multidimensional array of form posts I am using (or array of arrays as it is in reality), I presume I would just need to add a nested loop within the one you gave me to use?
 
You can check that the name of the element being compares includes the string "super_group'

Code:
if((formref.elements[i].value==0)&&(formref.elements[i].name.indexOf('super_group')!=-1)){


Your multidimensional array is irrelevant right now. Its only for PHP to use. Javascript will just iterate through all elements.


----------------------------------
Phil AKA Vacunita
----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.
 
That's brilliant thanks.

I'll try this out tomorrow when I am back at work.

Much appreciated.
 
Just an update to say thanks again,

Vacunita's post nailed it. I had to change the loop from
Code:
for(var i=0; i<=element_count; i++){
to
Code:
for(var i=0; i<element_count; i++){
and trigger the javascript from an onclick event but now it filters nicely and only sends over products that have a value.

Thanks to everyone.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top