Jump to content

[SOLVED] Running trough form fields without knowing name of the fields in advance


peddel

Recommended Posts

Well i got this form that has a variable amount of fields depending on what the user did in previous forms.

Thing is i now want to submit this form ONLY when all fields are filled in.

 

Is there a way to check all available form fields one by one without knowing the exact ID/name of that field.

 

So i can run trough the fields and just check if its filled or not, as long as its not filled i send a false.

When all fields are filled ill send a true.

 

I used some php to scroll trough the cells to send the info to some variables using this code,

maybe there's some code existing in javascript that does the same?

 

foreach ($_POST as $fld => $val) {
unset($_SESSION[$fld]);
$_SESSION[$fld] = $val;
}

If you give your form tag an ID, like so:

 

<form id="form1" method="post" action="">

 

Then you can use some javascript like so:

 

function checkFields(){
var fields = document.getElementById("form1").elements;

for(var i = 0; i < fields.length; i++){
	if(fields[i].value == ''){
		return false;
	}
}
return true;		
}

Archived

This topic is now archived and is closed to further replies.

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