Welcome Guest ( Log In | Register)



 
Reply to this topicStart new topic
> Loop Through Form, How do you do it?
Tyssen
post Oct 21 2005, 06:48 AM
Post #1



***********

Group: Members
Posts: 1,161
Joined: 9-May 05
From: Brisbane, QLD
Member No.: 6,818



In ASP you'd do this:

CODE
For Each Field in Request.Form

So 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.
Go to the top of the page
 
+Quote Post
Unregistered 015
post Oct 21 2005, 12:52 PM
Post #2


Super Member
*********

Group: Members
Posts: 276
Joined: 4-August 05
Member No.: 10,273



Not sure, but I think it can be done like this:

CODE
if (!filled_out($HTTP_POST_VARS))
{
do something
}


And add this somewhere:

CODE
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.
Go to the top of the page
 
+Quote Post
truefusion
post Oct 21 2005, 01:09 PM
Post #3


Ephesians 6:10-17
Group Icon

Group: [MODERATOR]
Posts: 1,918
Joined: 22-June 05
From: The World of Gentoo
Member No.: 8,528



if and else statements are all that is needed no looping functions.

CODE

if(!$variable){
echo "you did not fill in the entire form";
}
else{
echo "thanks for your submittion!";
}
Go to the top of the page
 
+Quote Post
Spectre
post Oct 21 2005, 06:08 PM
Post #4


Privileged Member
*********

Group: Members
Posts: 873
Joined: 30-July 04
Member No.: 246



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):

CODE
$array_keys = array_keys($_POST);
for( $i=0;$i<count($array_keys);$i++ ) {
 if( $_POST[$array_keys[$i]] == '' ) {
    // Not all fields have been enetered.
 }
}
Go to the top of the page
 
+Quote Post
Tyssen
post Oct 23 2005, 10:23 PM
Post #5



***********

Group: Members
Posts: 1,161
Joined: 9-May 05
From: Brisbane, QLD
Member No.: 6,818



I found this on another forum:

CODE
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?
Go to the top of the page
 
+Quote Post
dul
post Oct 27 2005, 11:48 PM
Post #6


Member [Level 1]
****

Group: Members
Posts: 73
Joined: 21-September 05
Member No.: 12,113



Tysson's way is better. which is
CODE
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.


This post has been edited by BuffaloHELP: Nov 3 2005, 10:11 AM
Go to the top of the page
 
+Quote Post
HmmZ
post Oct 29 2005, 08:45 AM
Post #7


Super Member
*********

Group: Members
Posts: 362
Joined: 2-March 05
From: The Netherlands
Member No.: 4,097



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 tongue.gif

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):
CODE

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:

[code]
print "<input type=\"text\" value=\"Name\" name=\"username\" onFocus=\"if(this.value=='Name')this.value=''\">";
[code]

that provides a text field -> name insert
text that is in the textfield (not focussed on it) -> Name
when a user focusses on the text field, Name disappears and makes it an empty textfield, giving no trouble to the user smile.gif

Hope that helps happy.gif
Go to the top of the page
 
+Quote Post
gogoily
post Oct 30 2005, 09:38 AM
Post #8


Member [Level 3]
******

Group: Members
Posts: 99
Joined: 30-October 05
Member No.: 13,571



Maybe this can help:
CODE

foreach($_POST as $k=>$v)
   if(empty($v))
      echo "$k is null";
Go to the top of the page
 
+Quote Post
iGuest
post May 26 2008, 07:07 PM
Post #9


Hail Caesar!
*********************

Group: Members
Posts: 5,876
Joined: 21-September 07
Member No.: 50,369



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