Page 1 of 1

Output Buffer a form

Posted: Sun May 10, 2009 4:20 am
by becky-atlanta
I am trying to use output buffer to output the result of a form into a html file. However, the html file does not hold all the results. The input fields are fine, but the drop down boxes, radio buttons, checkboxes, and textareas do not hold the results. Am I doing it wrong? Any fix or work around?

Thank you for taking the time to check it out.

Code: Select all

<?php
ob_start();
?>
.
.
.
<fieldset>
          <legend>Position (preference may be given to counselors who can stay for longer)</legend>
          <table width="100%">
            <tr> 
              <td width="50%"> <label>Position applying for</label> <br>  
                <input type="radio" name="position" value="Counselor (entering 12th Grade and higher">
                <label>Counselor (entering 12th Grade and higher</label> <br>  
                <input type="radio" name="position" value="Junior Counselor (entering 11th Grade)">
                <label>Junior Counselor (entering 11th Grade)</label> <br> 
                <input type="radio" name="position" value="WSI">
                <label> WSI</label> <br> 
                <label> <input type="radio" name="position" value="Assistant Swimming Instructor">
                Assistant Swimming Instructor</label> </td>
              <td class="left-line-box"> 
                <label>Weeks applying for:</label> <br> 
                <input name="weeks[]" type="checkbox" id="weeks" value="June 22 - 26"> <label>June 22 - 26</label> <br>
                <input name="weeks[]" type="checkbox" id="weeks" value="June 29 - July 3">
                <label> June 29 - July 3</label> <br>       
                <input name="weeks[]" type="checkbox" id="weeks" value="July 6 - 10"> 
                <label>July 6 - 10</label> <br>     
                <input name="weeks[]" type="checkbox" id="weeks" value="July 13 - 17 "> 
                <label>July 13 - 17</label> <br>        
                <input name="weeks[]" type="checkbox" id="weeks" value="July 20 - 24"> 
                <label>July 20 - 24 </label><br>        
                <input name="weeks[]" type="checkbox" id="weeks" value="July 27 - 31 "> <label>July 27 - 31</p></label>
                </td>
            </tr>
          </table>
          </fieldset>
          <br>
          <fieldset>
          <legend>Experience &#8230;</legend>
          <label>Experience: (list all experience with children i.e. counselor 
          positions, babysitting, Pirchei groups, siblings)</label>
          <br>
          <textarea name="experience"  value="<?php echo $experience; ?>" cols="60" rows="3" id="experience"></textarea>
          <br>
          <label>Please list your interests, talent and hobbies: (i.e. tutoring, 
          sports, swimming, choir, art, etc&#8230;)</label>
          <br>
          <textarea name="interest"  value="<?php echo $interest; ?>" cols="60" rows="3" id="interest"></textarea>
          <br>
          <label>Please explain why you would like to be a counselor:</label>
          <br>
          <textarea name="why"  value="<?php echo $why; ?>" cols="60" rows="3" id="why"></textarea>
          <br>
          <label>What do you feel are your greatest assets as a counselor:</label>
          <br>
          <textarea name="asset"  value="<?php echo $asset; ?>" cols="60" rows="3" id="asset"></textarea>
          <br>
          <label>Please explain how you would handle the following hypothetical 
          incident - two boys start to argue about a call in the game and one 
          storms off the field and starts to walk off:</label>
          <br>
          <textarea name="hypothethical"  value="<?php echo $hypo; ?>" cols="60" rows="3" id="hypothetical"></textarea><br>
          </fieldset>
.
.
.
<?php
$buffer = ob_get_flush();
ob_end_flush();
file_put_contents($appfile, $buffer);
?>

Re: Output Buffer a form

Posted: Sun May 10, 2009 4:40 am
by Defiline

Code: Select all

<?php
 
error_reporting(E_ALL);
 
ob_start();
ob_implicit_flush(0);
 
 
// Variables Declaration
$name    = 'Apple';
$age     = 'Unknown';
$country = 'Estonia';
 
?>
 
Name: <b><?php echo $name; ?></b>
<!-- ... -->
 
<?php
 
// Getting of Content
$content = ob_get_contents();
 
ob_end_clean(); // This is inportant. DO NOT FLUSH
 
file_put_contents('file', $content);
 
?>

Re: Output Buffer a form

Posted: Sun May 10, 2009 5:27 am
by becky-atlanta
Defiline,

Thank you for your prompt response! I like the tips about using ob_end_clean().

However, that is not the problem as I am getting results in other fields. In fact, I just figured out the textarea field. It works like this:

Code: Select all

<textarea name="asset" cols="60" rows="3" id="asset">[color=#FF0000]<?php echo $asset; ?>[/color]</textarea>
I am still trying to figure out how to code results into the drop down boxes, radio buttons, and check boxes correctly.

Re: Output Buffer a form

Posted: Sun May 10, 2009 5:46 am
by Defiline
I feel sorry, but I cannot understand what does "result" mean?
Can you explain it to me, please?

Do you save the result which have been sent?
Can you not save the sent result with an array?
Are you trying to save the whole form with all data?

Re: Output Buffer a form

Posted: Sun May 10, 2009 6:02 am
by becky-atlanta
Sorry that I was not very clear.

It is a form for applicants to fill out their information and then submit it to the organization. So I want to save the completed form with data to a html file.

Re: Output Buffer a form

Posted: Sun May 10, 2009 6:38 am
by Defiline
I get it :)

1. We have submitted the form
2. We have $_POST array with all elements
3. We have our HTML code

I suggest you already know that if an user has not checked checkbox, its value will not be sent.
Therefore, the $_POST array does not know about these boxes (and their names).
You should manually check these values by iteration (foreach/for).

Of course, If I understand correctly.

Re: Output Buffer a form

Posted: Sat May 23, 2009 9:47 am
by becky-atlanta
Thanks for pointing me to the right direction, Defiline.

I finally sorted it out. I am posting the code here for others to reference.

Here is the form part for radio button selection. It works the same way for check boxes.

Code: Select all

<?php
<label>Position applying for</label>
<br>  
<input name="position" type="radio" value="Counselor (entering 12th Grade and higher)" <?php echo $post1; ?>>
<label>Counselor (entering 12th Grade and higher</label> <br>  
<input name="position" type="radio" value="Junior Counselor (entering 11th Grade)" <?php echo $post2; ?>>
<label>Junior Counselor (entering 11th Grade)</label> <br> 
<input name="position" type="radio" value="WSI" <?php echo $post3; ?>>
<label>WSI</label> <br> 
<input name="position" type="radio" value="Assistant Swimming Instructor" <?php echo $post4; ?>>
<label>Assistant Swimming Instructor</label>
?>
After the form is submitted to itself, this is the code to check and place the answer in the form:

Code: Select all

<?php
if ($fill == "filled") {
    switch ($position){
        case "Counselor (entering 12th Grade and higher)":
            $post1 = "checked";
            break;
        case "Junior Counselor (entering 11th Grade)":
            $post2 = "checked";
            break;
        case "WSI":
            $post3 = "checked";
            break;
        case "Assistant Swimming Instructor":
            $post4 = "checked";
            break;}
?>