Tyssen 0 Report post Posted October 21, 2005 In ASP you'd do this: For Each Field in Request.FormSo how do you do it in PHP? I want to loop through a form, check to see if it has a value and if so, append to a string to send as the body of my email. Share this post Link to post Share on other sites
Unregistered 015 0 Report post Posted October 21, 2005 Not sure, but I think it can be done like this: if (!filled_out($HTTP_POST_VARS)){do something} And add this somewhere:function filled_out($form_vars){ // test that each variable has a value foreach ($form_vars as $key => $value) { if (!isset($key) || ($value == "")) return false; } return true;} Notice from BuffaloHELP: Codes, any type, need to be within the CODE tag. Refrain from making double posts. Last caution note. Merging. Share this post Link to post Share on other sites
truefusion 3 Report post Posted October 21, 2005 if and else statements are all that is needed no looping functions. if(!$variable){echo "you did not fill in the entire form";}else{echo "thanks for your submittion!";} Share this post Link to post Share on other sites
Spectre 0 Report post Posted October 21, 2005 ciroxyz, $HTTP_POST_VARS is generally considered 'obsolete' now. It may work (I think it may have been completely dropped from PHP 5x, but I don't know), but it's better to use the $_POST variable, in which all content submitted via the POST method is stored.And truefusion, that will not work for a couple of reasons - for one, the $_POST variable is an array, and for two, an index not existing does not return FALSE in itself (although there are functions to reach this state, such as isset()). Further more, some input elements in a form - such as a radio button - will not register themselves at all when submitting the data if not selected - so simply checking whether or not they are empty will not work.Anyway, Tyssen, here's a quick snippet that should work (will only check if entries are null): $array_keys = array_keys($_POST);for( $i=0;$i<count($array_keys);$i++ ) { if( $_POST[$array_keys[$i]] == '' ) { // Not all fields have been enetered. }} Share this post Link to post Share on other sites
Tyssen 0 Report post Posted October 23, 2005 I found this on another forum: foreach($_POST as $key => $data) { if($key != 'required') { if($data !='') { $message .= $key.": ".$data ."\n"; } }}That works fine except that my output looks like this: Name: My Name Nameinitvalue: Name Positioninitvalue: Position Addressinitvalue: Address Phone: 5555555555 Phoneinitvalue: Phone Faxinitvalue: Fax Email: myemail@email.com Emailinitvalue: Email Home_workinitvalue: Work Comments: Test Commentsinitvalue: Comments The ones in italics are the actual fields that were filled in so all the others shouldn't be there (ie the ones with initvalue in the field name). Anyone got any idea where they're coming from? Share this post Link to post Share on other sites
dul 0 Report post Posted October 27, 2005 (edited) Tysson's way is better. which is foreach($_POST as $key => $data) { if($key != 'required') { if($data !='') { $message .= $key.": ".$data ."\n"; } }} use array. come on. Notice from BuffaloHELP: It's bad enough that you did not use the CODE tag, that's all you had to say? Warning issued. Edited November 3, 2005 by BuffaloHELP (see edit history) Share this post Link to post Share on other sites
HmmZ 0 Report post Posted October 29, 2005 Tyssen, from what i see, i am thinking you used type="hidden" for the initvalues, but when you recall all $_POST from your form, it will also recall the hidden types, all types are recalled with an array, not just the ones you want I either suggest doing it without an array and do it manually, or use php to make the form more viable while using an array I am, however, unsure what you want to achieve with those initvalues, but i think (and i hope im right): if(empty($Name)){ $value = "Name"; } else { $value = ""; } with something like that you can achieve to give the field an initial value, but, there is another way i was thinking of:You just want a text-field (for example: name field) where the field is not empty initially, but states "name" and disappears once the user focusses on the text field, so it can type its own name, i think thats achieves similar to the following: print "<input type=\"text\" value=\"Name\" name=\"username\" onFocus=\"if(this.value=='Name')this.value=''\">";[code=auto:0] that provides a text field -> name inserttext that is in the textfield (not focussed on it) -> Namewhen a user focusses on the text field, Name disappears and makes it an empty textfield, giving no trouble to the user Hope that helps Share this post Link to post Share on other sites
gogoily 0 Report post Posted October 30, 2005 Maybe this can help: foreach($_POST as $k=>$v) if(empty($v)) echo "$k is null"; Share this post Link to post Share on other sites
iGuest 3 Report post Posted May 26, 2008 Extracting Text Field from Form using For Loop Loop Through Form Dear, I am developing a form in which I want the data repeatedly. For example: For ($x=0; $x > 3; $x++){ Exam Name <input name="ename[]" type="text" id="ename" /> # of Subjects <input name="totsubj[]" type="text" id="totsubj" /> } I want to get the individual values once the form is submitted. The form uses method post and call another file where I get my variable through $_POST. HELP PLEASEEEEEE -question by Khabi Khan Share this post Link to post Share on other sites
sonesay 7 Report post Posted February 28, 2009 The previous post your question is so vague I have no idea what you are trying to do. I have been using the $_POST array to loop through and string together my insert query. I have a very long list of text areas so it saved me a bit of typing by using the array instead of explicitly referencing each field name. I have how ever found out that if you include an input image type for the submit button this also gets added on to the array so you need to strip them off properly to be able to get the correct amount of array elements.It usually adds on two values image_x and image_y coordinates of where you clicked on the image to the end of the array. You cannot just trim it on the right as string length differs depending on where you clicked on the image button. Share this post Link to post Share on other sites