No puedo hacer funcionar mi formulario php, envía un correo pero no ejecuta el código en la página de redirección

This question gets asked all the time, but I can't figure out why mine isn't working. I have a form that redirects to itself. If PHP decides it is submitted, there is a success/failure message and it displays the user input as the default value and disables the fields: using phpinfo I can see that the form is being submitted, but this first conditional doesn't work. I've tried a couple of versions, but no luck. It's weird because it sends the email

Specifically, the result and disable functions don't display their code after the form has been sent.

<?php
function clean($data) {
    $data = trim(stripslashes(strip_tags($data)));
    return $data;
}
function result(){
    if($sent) echo $result;
}
function disable($field){
    if($sent){
        if($field != null){
            $ret .= $field . '", disabled, placeholder!="';
        }
        $ret .= '", disabled, placeholder!="';
        echo $ret;
    }
}
function option($item){
        $ret = "<option>";
    if($sent){
        if($eventType == $item){
            $ret = "<option selected>";
        }
    }
    $ret .= $item . "</option>";
    echo $ret;
}
if(isset($_POST['name'])){
    $sent = TRUE;
    $result = null;
    $name = $_POST['name'];
    $phone = $_POST['phone'];
    $email = $_POST['email'];
    $eventDate = $_POST['eventDate'];
    $eventTime = $_POST['eventTime'];
    $eventLength = $_POST['eventLength'];
    $eventLocation = $_POST['eventLocation'];
    $eventType = $_POST['eventType'];
    $message = $_POST['message'];

    $recipient = "";
    $subject = " Form Submission";
    $mailheader = "From:  \r\n";
    $formcontents = "You received this e-mail message through your website: \n\n";
    $formcontents .= "Name: " . clean($name) . "\r\n";
    $formcontents .= "Phone: " . clean($phone) . "\r\n";
    $formcontents .= "Email: " . clean($email) . "\r\n";
    $formcontents .= "Event Date: " . clean($eventDate) . "\r\n";
    $formcontents .= "Event Time: " . clean($eventTime) . "\r\n";
    $formcontents .= "Event Length: " . clean($eventLength) . "\r\n";
    $formcontents .= "Event Location: " . clean($eventLocation) . "\r\n";
    $formcontents .= "Event Type: " . clean($eventType) . "\r\n";
    $formcontents .= "Message: " . clean($message) . "\r\n";
    $formcontents .= "\r\n";
    $formcontents .= 'IP: '.$_SERVER['REMOTE_ADDR']."\r\n";
    $formcontents .= 'Browser: '.$_SERVER['HTTP_USER_AGENT']."\r\n";

// Send mail
    if(mail($recipient, $subject, $formcontents, $mailheader)){;
        $result = '<h3 class="alert alert-success"> Thank you, your form was successfully sent and I will contact you shortly.</h3>';
    } else {
        $result = '<h3 class="alert alert-error"> Your mail could not be sent at this time.</h3>';
    }
}
?>

<form action="contact.php" method="POST" class="form-horizontal span4">
  <fieldset>
    <legend>
      <h2>Or send me a message. </h2>
    </legend>
    <p class="help-block">None of the fields are required, but the more information I have about your event, the more detailed I can be in my response.</p>
    <legend class="help-block">Your Details</legend>
    <div class="control-group">
      <label for="name" class="control-label">Your Name</label>
      <div class="controls">
        <input id="name" type="text" name="name" placeholder="<?php disable($name); ?>" class="input-xlarge"/>
      </div>
    </div>
    <div class="control-group">
      <label for="phone" class="control-label">Your Contact Number</label>
      <div class="controls">
        <input id="phone" type="tel" name="phone" placeholder="<?php disable($phone); ?>" class="input-xlarge"/>
      </div>
    </div>
    <div class="control-group">
      <label for="email" class="control-label">Your Email</label>
      <div class="controls">
        <input id="email" type="email" name="email" placeholder="<?php disable($email); ?>" class="input-xlarge"/>
      </div>
    </div>
    <legend class="help-block">Your Event </legend>
    <div class="control-group">
      <label for="eventDate" class="control-label">Your Event's Date</label>
      <div class="controls">
        <input id="eventDate" type="date" name="eventDate" placeholder="<?php disable($eventDate); ?>" class="input-xlarge"/>
      </div>
    </div>
    <div class="control-group">
      <label for="eventTime" class="control-label">Your Event's Start Time</label>
      <div class="controls">
        <input id="eventTime" type="time" name="eventTime" placeholder="<?php disable($eventTime); ?>" class="input-xlarge"/>
      </div>
    </div>
    <div class="control-group">
      <label for="eventLength" class="control-label">Your Event's Length</label>
      <div class="controls">
        <input id="eventLength" type="text" name="eventLength" placeholder="<?php disable($eventLength); ?>" class="input-xlarge"/>
      </div>
    </div>
    <div class="control-group">
      <label for="eventLocation" class="control-label">Your Event's Location</label>
      <div class="controls">
        <input id="eventLocation" type="text" name="eventLocation" placeholder="<?php disable($eventLocation); ?>" class="input-xlarge"/>
      </div>
    </div>
    <div class="control-group">
      <label for="eventType" class="control-label">What Kind of Event</label>
      <div class="controls">
        <select id="eventType" name="eventType" placeholder="<?php disable($eventType); ?>"><?php option("Charity Event"); option("Expo/Trade Show"); option("Personal Event"); option("Other"); ?></select>
      </div>
    </div>
    <div class="control-group">
      <label for="message" class="control-label">Other comments or the best time to reach you.</label>
      <div class="controls">
        <textarea id="message" name="message" rows="10" placeholder="<?php disable($message); ?>" class="input-xxlarge"></textarea>
      </div>
    </div>
    <div class="form-actions">
      <button type="submit" name="submit" placeholder="<?php disable(null); ?>" class="btn btn-primary">Send Message</button>
    </div>
  </fieldset>
</form>

preguntado el 05 de mayo de 13 a las 14:05

If it's sends the email that means that the first condition if(isset($_POST['name'])) does work cause it's get into the code. now - what is not working exactly ? -

2 Respuestas

You have to import your global variables into function scope, like:

function result(){
    global $sent, $result;
    if($sent) echo $result;
}

..in functions disable() and option(), too.

contestado el 05 de mayo de 13 a las 14:05

if(isset($_POST['name'])){

debiera ser

if(isset($_POST['submit'])){

contestado el 05 de mayo de 13 a las 14:05

That doesn't fix it, I changed it to name while I was debugging. I thought submit being empty was the problem. - Lochlán

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.