Jump to content

php loops and forms help


jbwebdesign

Recommended Posts

For future reference, can you try to be clearer about exactly what you are trying to achieve? Any possible answers I give you are a bit of a shot in the dark, trying to figure out what you are trying to do.

 

This might get you closer:

 

>



Untitled Document



   if (isset($_POST['submit'])) {
       $a = $_POST['apples'];
       $o = $_POST['oranges'];
       $count = 1;
       $monster = $a;

       if(isset($a)){
       while($count            echo ++$count . " apples are left ";
           }
       }
       if(isset($o)){
       while($count            echo ++$count . " oranges are left ";
           }
       }
   }
   ?>

</pre>
<form action="<?php%20%24_SERVER%5B'PHP_SELF'%5D;%20?>" method="post" name="testing">
       apples: 

       oranges:


</form>
<br><br><form action="" name="test">
       message:

</form>
<br><br><br

 

You can't immediately check for the $_POST values -- you need to make sure the form has been submitted first. I usually do that by assigning the submit button a name, and then checking if it has a value. If so, you can go ahead and grab the values.

 

FYI - unless you assign the checkbox a value, it will either return "on" or "".

Link to comment
Share on other sites

first of all I would like to thank you very very much for your help! :D

 

what I am trying to do is...... I want the value inside the while loop to be returned inside the text box.....

with the code the way it is now, it returns the following......

 

2 apples are left 3 apples are left 4 apples are left 5 apples are left 6 apples are left 7 apples are left 8 apples are left 9 apples are left 10 apples are left 11 apples are left

 

 

 

 

I want that to be inside the text box......what am I doing wrong? how come it just says on inside the text box instead of returning the correct value??

Link to comment
Share on other sites

I want that to be inside the text box......what am I doing wrong? how come it just says on inside the text box instead of returning the correct value??

 

Based on the way it is currently coded, "on" is actually the correct value.

 

-- You check the checkbox, which gives the checkbox a value of "on", and click submit

-- PHP grabs the values of the checkboxes for apples and oranges. the $a variable holds the value "on"

-- two lines down, the $monster variable gets the value of a, so it too holds the value "on."

 

That's the last change you do to the $monster variable, so that's what it will echo out in the form.

 

If you want to include the "2 apples are left 3 apples are left 4 apples are left 5 apples are left 6 apples are left 7 apples are left 8 apples are left 9 apples are left 10 apples are left 11 apples are left" text inside the form field, you will need to add it to the $monster variable, rather than echoing it out.

 

In that case, you'd want something along these lines:

>




Untitled Document



   if (isset($_POST['submit'])) {
       $a = $_POST['apples'];
       $o = $_POST['oranges'];

       $count = 1;

       if(isset($a)){
           while($count                $monster = $monster . $count . " apples are left ";
               $count++;
           }
       }
       if(isset($o)){
       while($count                $monster = $monster . $count . " oranges are left ";
               $count++;
           }
       }
   }
   ?>

</pre>
<form action="<?php%20%24_SERVER%5B'PHP_SELF'%5D;%20?>" method="post" name="testing">
       apples: 

       oranges:


</form>
<br><br><form action="" name="test">
       message:

</form>
<br><br><br

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  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.

Loading...
×
×
  • Create New...