Help with forms

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
raffagapro
Forum Newbie
Posts: 1
Joined: Tue Jul 28, 2009 12:14 pm

Help with forms

Post by raffagapro »

Hi, I am pretty new to coding with PHP, but I am trying to make a form and I have php validate client input, but something is happening when posting info from a checkbox group. The problem I have is, I think, in line 11-13, if I ommit that, the array from the checkbox group passes normally and I get the info at my mail, but the requested code becomes useless. It doesn´t treat the array "sexyGril" as requested, and even if i don´t select an option it lets it go thru.

In the other hand when I enable code from 11-13, it always treats the array "sexyGirl" as missing, and the info won´t get post and mail to me, even if I selected values. If I removed "sexyGirl" from the $required and take off the code for cheking if it has been requested, the info gets posted and send it to my email, but the array "sexyGirl" is empty, even when I selected values for it.

Here are some snippets of the code involved. If i need to post the whole page let me know and i´ll edit the post.

Code: Select all

 
This is at the head tag
 
//list expected fields
    $expected = array('name', 'email', 'comments', 'sexyGirl');
    //set required fields
    $required = array('name', 'comments', 'sexyGirl');
    //create empty array for missing fields
    $missing = array();
    //set default values for variables that might not exist
    if (!isset($_POST['sexyGril'])) {
        $_POST['sexyGirl'] = array();
    }
 
...
 
//process the $_POST variables
    foreach ($_POST as $key => $value) {
        //assign to temporary variable and strip whitespace if not an array
        $temp = is_array($value) ? $value : trim($value);
        //if empty and required, add to $missing array
        if(empty($temp) && in_array($key, $required)) {
            array_push($missing, $key);
        } elseif (in_array($key, $expected)) {
            // otherwise, assign to variable of the same name as $key
            ${$key} = $temp;
        }
    }
 
This is in the body

Code: Select all

 
<body>
<h1>Contact Us</h1>
<?php
if ($_POST && isset($missing) && !empty($missing)) {
    ?>
    <p class="warning">Please complete the missing item(s) indicated.</p>
    <?php
} elseif ($_POST && !$mailSent) {
    ?>
    <p class="warning">Sorry, there was a problem sending your message. Please try again later.</p>
    <?php
} elseif ($_POST && $mailSent) {
    ?>
    <p><strong>Your message has been sent. Thank you for your feedback.</strong></p>
    <?php } ?>
    <p>We welcome feedback from visitors...<P>
<form id="form1" name="form1" method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
 <p>Who is the sexiest girl alive?</p>
    <?php if (isset($missing) && in_array('sexyGirl', $missing)) { ?>
    <span class="warning">Please choose at leas one option.</span><?php } ?>
  <p class="chkRad">
    <label>
      <input type="checkbox" name="sexyGirl[]" value="Alba" id="sexyGirl_0" 
      <?php 
      if (isset($missing) && in_array('Alba', $_POST['sexyGirl'])) {
          echo 'checked="checked"';
      } ?>
      />
      Jessica Alba</label>
    <br />
    <label>
      <input type="checkbox" name="sexyGirl[]" value="Chobot" id="sexyGirl_1"
      <?php 
      if (isset($missing) && in_array('Chobot', $_POST['sexyGirl'])) {
          echo 'checked="checked"';
      } ?>
      />
      Jessica Chobot</label>
    <br />
    <label>
      <input type="checkbox" name="sexyGirl[]" value="Montage" id="sexyGirl_2"
      <?php 
      if (isset($missing) && in_array('Montage', $_POST['sexyGirl'])) {
          echo 'checked="checked"';
      } ?>
      />
      Heidi Montage</label>
    <p/><p class="chkRad">
    <label>
      <input type="checkbox" name="sexyGirl[]" value="Shakira" id="sexyGirl_3"
      <?php 
      if (isset($missing) && in_array('Shakira', $_POST['sexyGirl'])) {
          echo 'checked="checked"';
      } ?>
      />
      Shakira</label>
    <br />
    <label>
      <input type="checkbox" name="sexyGirl[]" value="Fox" id="sexyGirl_4"
      <?php 
      if (isset($missing) && in_array('Fox', $_POST['sexyGirl'])) {
          echo 'checked="checked"';
      } ?>
      />
      Megan Fox</label>
    <br />
  </p>
  <p class="clearIt">
    <input type="submit" name="send" id="send" value="Send comments" />
  </p>
</form>
</body>
</html>
Thanks so much!

Update: I found the error thanks a lot. it was a typo. I had to print the code and revised it line by line
Post Reply