I'm making a contact form with specific input details and am trying to make it so that each submission creates a node for permanent records. The code (posted below) works but the node is generated 3 times in a row. Does the EVAL() function get called three times? Has anyone else had this problem and/or know how to fix it?

Thanks in advance for serious replies!

<?php
if(!empty($_POST["submit"]) && !empty($_POST["fullname"]) && !empty($_POST["email"]) && !empty($_POST["question"])) {
  $node = new StdClass();
  //creating a bare node

  $node->type = 'contact_request';
  //giving it type

  $node->status = 1;
  //give it a published status

  $node->title = "Contact: ".$_POST['fullname']."-".$_POST["email"];
  //gives title

  $node->body = "<fieldset><legend>Question</legend>";
  $node->body .= $_POST["question"];
  $node->body .= "</fieldset>";

  node_save($node);
  //save it and give it the rest of the attributes

  echo "<h2>Question Submitted</h2>";
} else {
  if(!empty($_POST["submit"])){
    $missing_fields = 0;
    $output = "";

    if(empty($_POST["fullname"])){ 
      $missing_fields = 1;
      $output .= " Name ";
    }

    if(empty($_POST["email"])){ 
      $missing_fields = 1;
      $output .= " Email ";
    }

    if(empty($_POST["phone"])){ 
      $missing_fields = 1;
      $output .= " Phone ";
    }

    if($missing_fields == 1){
      $output = "Required Fields: ".$output;
      echo "<h2>".$output."</h2>";
    }
  }//end input check
?>
<form id="form-main" action="/contact-request" method="post">
    <fieldset><legend>How do we contact you?</legend>
    <div>&nbsp;</div>
    <span class="form-left"><label for="fullname">Name:</label></span>
    <span class="form-right">
    <input type="text" id="fullname" name="fullname" class="midb-grey" size="22" />
    </span>
    <div id="errorreport">&nbsp;</div>
    <span class="form-left"><label for="email">Email:</label></span>
    <span class="form-right">
    <input type="text" id="email" name="email" class="midb-grey" size="22" />
    </span>
    <div>&nbsp;</div>

    <fieldset><legend>What's your question?</legend> 
    <cite>Please describe what information you need.</cite>
    <textarea id="question" name="question" rows="10" cols="60"></textarea>
    </fieldset>
    <span class="form-right"><input type="submit" id="submit" name="submit" value="Submit" /></span>
</form>
<?php } ?>

Comments

matt blaine’s picture

Have you tried http://drupal.org/project/webform I now use that for all my custom contact forms as all the sites I do are very specific on what fields they want. It stores a copy and can email them also

nuvious’s picture

The problem with webform is it doesn't allow you to add cck fields (especially node reference or custom cck fields) to a webform. Also I want the response to be an actual node that can be held in an archival sense and commented on by those handling the contact issue. The code I submitted in this question is a much more simplified version of what my contact form actually contains.

matt blaine’s picture

I've had to do similar before and did notice that sometimes (or always) code looped through more than once.

you could unset($_POST['flag_id']) on the first pass and then just don't insert if that flag is not set.

nuvious’s picture

That did it. Thanks!

matt blaine’s picture

as a side note I'd use $input = check_plain($_POST['input']); as you should never trust the user input for inserting into database or display

nuvious’s picture

Thanks for the note!