Aug 8, 2008

Loop Through Form - How do you do it?

Free Web Hosting, No Ads > CONTRIBUTE > Computers > Programming Languages > PHP Programming

free web hosting

Loop Through Form - How do you do it?

Tyssen
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.

Reply

Unregistered 015
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.

 

 

 


Reply

truefusion
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!";
}

Reply

Spectre
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.
 }
}

Reply

Tyssen
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?

Reply

dul
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.

Reply

HmmZ
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

Reply

gogoily
Maybe this can help:
CODE

foreach($_POST as $k=>$v)
   if(empty($v))
      echo "$k is null";

Reply

iGuest
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

Reply



Got an Opinion! Express your Views! (no registration):-
Add your Reply/ Opinion/ Views/ Comments/ Suggestion/ Questions/ Queries etc.
Posts with decent grammar & English will be accepted and please refrain from profanities.
For asking a Question, We recommend you to sign-up (for free) so that you can track the topic easily.

Nature of your Post*: Opinion/ Reply/ Comments
Question/Query
Feedback to us.
       
Name   Email
Title/Question*

(Maximum characters: 10,000)
You have characters left.
Confirm Code:

Recent Queries:-
  1. php loop through post variables - 4.11 hr back. (2)
  2. loop through post variables - 6.24 hr back. (1)
  3. asp loop through fields in a form - 6.85 hr back. (1)
  4. php loop through form - 7.19 hr back. (1)
  5. looping through variables php - 7.98 hr back. (1)
  6. php iterate through infinite items - 8.33 hr back. (1)
  7. php foreach loop form field - 9.00 hr back. (1)
  8. php looping through $_post variables - 9.06 hr back. (1)
  9. php loop through form elements - 11.21 hr back. (1)
  10. php looping through form post - 12.16 hr back. (2)
  11. looping in php form - 13.46 hr back. (1)
  12. loop through - 15.55 hr back. (1)
  13. textfields in array through name php - 16.95 hr back. (1)
  14. how we can give text field using loop in php - 18.09 hr back. (1)
Similar Topics

Keywords : loop, form

  1. Php Date() Problem [resolved]
    date() seems to loop same ninute. (4)
  2. Help Needed With Directory/file Listing Code Infinite Loop
    Made an infinite loop but why is this so? (5)
    Hi all ive got a small and simple (for the moment atleast /unsure.gif"
    style="vertical-align:middle" emoid=":unsure:" border="0" alt="unsure.gif" /> )file and directory
    listing script in php as follows CODE $dir = "."; $num = 0; $file =
    scandir($dir); while($file = scandir($dir)){     echo
    $file[$num];     echo "<BR>";     $num = $num + 1;     
    }; the concept is simple enough, the directory to start with is the current one, so scan this
    directory and wh....
  3. For Loop
    for loop problem (5)
    i'd like to make for loop which dependes on a $_GET variable: here is my tries: CODE
    if (isset($_GET['date']) {
    switch($_GET['date'])     {         case 'asc': $exp1
    = "$x>0"; $exp2 = "$x=$total";             $condition =
    '$x++';             break;         case 'desc'; $exp1 =
    "$x=$total"; $exp2 = "$x>0";             $condition =
    '$x--';             bre....
  4. For Each Loop Help
    (1)
    I have a page where I list a number of records from my db. Under each record I have a checkbox and a
    text field. At the bottom of the list I have a submit button. I want to be able to check various
    boxes and edit the text field then click the button to update all of the records. I currenlty have
    the following and it updates the records ok (checkbox, I haven't messed with the text field yet)
    when I first check the boxes but it will not update the db when I uncheck the boxes. My form: PHP
    Code: CODE <form name="form1" method="post" action=my....
  5. While Loop
    how to stop the timeout (9)
    I have written a piece of code that is to make a forum which contains a table consiting of two
    colums and a determind number of rows with a while loop, however when I test the page it goes and
    goes, and then returns an error stating that the maximum 30 second limit has been reached, I
    understand what that means I just don't know why it's looping forever: CODE while
    ($weightnum > 0); { echo "<tr>";    echo  
    '<td><input name="wname"' . $appendval . ' type="text"
    id="wname....

    1. Looking for loop, form

Searching Video's for loop, form
advertisement



Loop Through Form - How do you do it?



 

 

 

 

ADD REPLY / Got an Opinion! Remove these ADs! RAPID SEARCH! Free Web Hosting [X]
Express your Opinions, Thoughts or Contribute more info. to help others.
Ask your Doubts & Queries to get answers, So that "Together We can help others!"
Register FREE for AD-FREE forum, Create your own topics, Ask Questions, track topics, setup subscriptions & notifications and Get a Free Website w/ Email and FTP.
500MB Space *No Ads*, CPanel, FTP, PHP, MySQL, EMails - 100% FREE