Jump to content

Can't get form working


joe_ma
 Share

Recommended Posts

Hello

I am trying to setup a reservation form for a restaurant. As I want to use bootstrap I like to build the form accordingly. That means the form is just HTML markup. So I tried to use Ryan’s simple-form as modified by Soma.

I don’t get it to work properly. The template looks like this:

<?php
/**
 * basic-form.php - Example of a simple contact form in ProcessWire
 *
 */ 
include('./includes/header.inc');
?>

// here some HTML markup for leading Text etc.

<?php
$success_message = "<h2>Thank you, your message has been sent.</h2>"; 
$success = false; // we assume it and set to true if mail sent
$error = false;

// set and sanitize our form field values
$form = array(
	'datum' => $sanitizer->text($input->post->datum),
	'ankunftszeit' => $sanitizer->text($input->post->ankunftszeit),
	'anzahl_plaetze' => $sanitizer->text($input->post->anzahl_plaetze),
	'name' => $sanitizer->text($input->post->name),
	'adresse' => $sanitizer->text($input->post->adresse),
	'plz' => $sanitizer->text($input->post->plz),
	'ort' => $sanitizer->text($input->post->ort),
	'email' => $sanitizer->email($input->post->email),
	'tel' => $sanitizer->text($input->post->tel),
	'extras' => $sanitizer->textarea($input->post->extras),
	); 

print_r($form); 

// check if the form was submitted
if($input->post->submit) {

  // determine if any fields were ommitted or didn't validate
  foreach($form as $key => $value) {
    if( strlen(trim($value)) == 0 ) {
      $error_message = "<h2 class='error'>Please check that you have completed all fields.</h2>";
      $error = true;
      break;
    }
  }
  // if no errors, email the form results
  if(!$error) {
    $emailTo = 'me@me.com'; // of course I set the right email address ;-)
    if(empty($emailTo)) $emailTo = $users->get($config->superUserPageID)->email;
    $subject = "Contact Form";
    $message = '';
    foreach($form as $key => $value) $message .= "$key: $value\n";

    if(mail($emailTo, $subject, $message, "From: $form[email]")){
      $success = true;
    }
  }
}
?>
<?php if(!$success) : // : alternative version instead of {} ?>

<form class="form-horizontal" action="./" method="post">
<fieldset>
<!-- Text input-->
<div class="form-group">
  <label class="col-md-5 control-label" for="datum">Datum</label>  
  <div class="col-md-5">
  <input id="datum" name="datum" placeholder="Wann möchten Sie reservieren" class="form-control input-md" required="" type="text">
    
  </div>
</div>

<!-- Text input-->
<div class="form-group">
  <label class="col-md-5 control-label" for="zeit">Zeit</label>  
  <div class="col-md-5">
  <input id="zeit" name="zeit" placeholder="Wann werden Sie eintreffen?" class="form-control input-md" required="" type="text">
    
  </div>
</div>

<!-- Text input-->
<div class="form-group">
  <label class="col-md-5 control-label" for="anzahl_plaetze">Anzahl Plätze</label>  
  <div class="col-md-5">
  <input id="anzahl_plaetze" name="anzahl_plaetze" placeholder="Anzahl Personen" class="form-control input-md" required="" type="text">
    
  </div>
</div>

<!-- Text input-->
<div class="form-group">
  <label class="col-md-5 control-label" for="name">Name</label>  
  <div class="col-md-5">
  <input id="name" name="name" placeholder="Vorname Nachname" class="form-control input-md" required="" type="text">
    
  </div>
</div>

<!-- Text input-->
<div class="form-group">
  <label class="col-md-5 control-label" for="adresse">Adresse</label>  
  <div class="col-md-5">
  <input id="adresse" name="adresse" placeholder="Strasse, Nr." class="form-control input-md" required="" type="text">
    
  </div>
</div>

<!-- Text input-->
<div class="form-group">
  <label class="col-md-5 control-label" for="plz">PLZ</label>  
  <div class="col-md-5">
  <input id="plz" name="plz" placeholder="Postleitzahl" class="form-control input-md" required="" type="text">
    
  </div>
</div>

<!-- Text input-->
<div class="form-group">
  <label class="col-md-5 control-label" for="ort">Ort</label>  
  <div class="col-md-5">
  <input id="ort" name="ort" placeholder="Wohnort" class="form-control input-md" required="" type="text">
    
  </div>
</div>

<!-- Text input-->
<div class="form-group">
  <label class="col-md-5 control-label" for="email">Email</label>  
  <div class="col-md-5">
  <input id="email" name="email" placeholder="mail@domain.ch" class="form-control input-md" required="" type="text">
    
  </div>
</div>

<!-- Text input-->
<div class="form-group">
  <label class="col-md-5 control-label" for="tel">Tel.</label>  
  <div class="col-md-5">
  <input id="tel" name="tel" placeholder="031 999 99 99" class="form-control input-md" required="" type="text">
    
  </div>
</div>

<!-- Textarea -->
<div class="form-group">
  <label class="col-md-5 control-label" for="extras">Spezielle Wünsche</label>
  <div class="col-md-5">                     
    <textarea class="form-control" id="extras" name="extras"></textarea>
  </div>
</div>

<!-- Button -->
<div class="form-group">
  <label class="col-md-5 control-label" for="submit"></label>
  <div class="col-md-5">
    <button id="submit" name="submit" class="btn btn-default">Abschicken</button>
  </div>
</div>

</fieldset>
</form>

<?php else: // if success ?>

  <?php echo $success_message; ?>

<?php endif; // end if success ?>

here some further HTML for sidebar etc.

<?php include('./includes/footer.inc'); ?>

I have set the correct email address to send the mail to. And the

print_r($form); 

prints the correct input values after submission.

I don't receive any mail and the success message is not being displayed but still the form.

Second question: If I want to include the values also in the success message, what would be the way to do it? Something like:

$success_message = "<h2>Thank you, your message has been sent.</h2>" . $message; 

Thanks for help.

Link to comment
Share on other sites

@joe_ma: email not getting sent and the fact that you're not getting success message both indicate that in your environment PHP's mail() doesn't work.

The reason for that can only be guessed, but to make sure I'd create a simple mail() call somewhere and see if you can actually send anything anywhere. If it turns out that mail() simply isn't working for you at all, first you'll want to check if your web host has any tips for this situation. This depends, of course, entirely on what kind of hosting setup you've got.

It would also make sense to take a look at your error logs (/site/assets/logs/errors.txt), Apache error logs if you've got access to those and try this with debug mode turned on in your /site/config.php. See if there's anything strange happening when you're trying to send this email. Note that you should only set debug mode on if this is not a live site, you really don't want that to be turned on in production environment!

(In case that you've got an SMTP server you can use for sending email, you could also try installing Wire Mail SMTP or Swift Mailer, but note that for either of these to work you'll need to use current dev branch of ProcessWire, which isn't necessarily a good idea for live site either!)

About your second question: you can do that (append them to $success_message) or simply output them right after / before success message. Doesn't really matter, other than if you're trying to use values user submitted, you'll want to do that after the "$form = array( ..." line and use them from that array, so that they're sanitized:

// set and sanitize our form field values
$form = array(
	'datum' => $sanitizer->text($input->post->datum),
	'ankunftszeit' => $sanitizer->text($input->post->ankunftszeit),
	'anzahl_plaetze' => $sanitizer->text($input->post->anzahl_plaetze),
	'name' => $sanitizer->text($input->post->name),
	'adresse' => $sanitizer->text($input->post->adresse),
	'plz' => $sanitizer->text($input->post->plz),
	'ort' => $sanitizer->text($input->post->ort),
	'email' => $sanitizer->email($input->post->email),
	'tel' => $sanitizer->text($input->post->tel),
	'extras' => $sanitizer->textarea($input->post->extras),
	);

// append values to success message
if ($form['datum']) $success_message .= "<br />Datum: " . $form['datum']; // etc.
  • Like 1
Link to comment
Share on other sites

Thanks Teppo

I made a simple test for mail(). It worked perfectly.

So mail() doesn't seem to be the problem. There is also no entry in the error log. And I don't get any feedbacks with debug mode on.

Link to comment
Share on other sites

Sorry for that. The problems seems to be quite different: in original code there was submit element with name "submit" and value "submit". You seem to have replaced that with button with right name but no value.

Try adding value="submit" to your button and see if that helps.

Link to comment
Share on other sites

Hmm …

I added value="submit". No result either.

I replaced the button with the original input-field (<input type="submit" name="submit" value="submit" id="submit">). No result either.

Edit:

I have just become aware, that I had different names for "zeit" in the $form array and the input field.

And I left out the following lines from Soma's original code:

        <?php 
  if($error) {
    echo $error_message; // or pull from a PW field
  } 
  ?>

Now I have corrected both mistakes. But still no result.

When I completely fill out the form I receive the error message 

Pleas check that you have completed all fields.

So, there must still be som kind of error.

Edit 2:

When I skip the $sanitizer->text(), the form is being submitted. Finally.

Thank you teppo for your precious help.

Link to comment
Share on other sites

As a quick hack, if you replace this row:

$error_message = "<h2 class='error'>Please check that you have completed all fields.</h2>";

With this:

$error_message = "<h2 class='error'>Please check that you have completed all fields: $key</h2>";

.. you can debug what field exactly is not being filled. You might also want to remove the "break" right before that and append each new error to the message instead (change "=" to ".=").

Edit: apparently you managed to get this working already, which is great. $sanitizer->text() clearing field(s) sounds strange, though, so you might want to take a closer look at why that was happening. Then again, whether that really matters depends a lot on how you're going to use this data.

Edited by teppo
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...