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;
}

Link to comment
Share on other sites

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;		
}

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.