Jump to content


Photo

Creating own form, getting started

form input

  • Please log in to reply
22 replies to this topic

#1 onjegolders

onjegolders

    Hero Member

  • Members
  • PipPipPipPipPip
  • 799 posts
  • 207

  • LocationMidlands, UK

Posted 10 April 2012 - 05:41 AM

Am trying to get my own simple contact form up and running and I would prefer to have a very simple one but understand the code that makes it work before trying to do anything complicated.

I've created a contact form with contact template and have made action="form.php" method="POST"

I've tried setting up a template for form but I keep getting the error that the template does not exist when I submit.

On my form.php I want to start really simply just to test that the $input method is working so I just have:

<?php
$name = $input->post->name;
echo $name;
?>

Would appreciate any guidance just to get me on my way.

#2 netcarver

netcarver

    Sr. Member

  • Members
  • PipPipPipPip
  • 428 posts
  • 341

  • LocationUK

Posted 10 April 2012 - 08:34 AM

I think Ryan has a module for this already. Have you tried out FormTemplateProcessor?
Steve ☧

#3 vitor

vitor

    Jr. Member

  • Members
  • PipPip
  • 25 posts
  • 6

  • LocationPortugal

Posted 10 April 2012 - 08:36 AM

Hi onjegolders,

You can find several ideas here on how to process/create your own forms: http://processwire.c...t-form-builder/

#4 onjegolders

onjegolders

    Hero Member

  • Members
  • PipPipPipPipPip
  • 799 posts
  • 207

  • LocationMidlands, UK

Posted 10 April 2012 - 08:42 AM

Thanks guys,

This is more for users creating their own forms though isn't it?
Or can it be used in templates as well?

Ideally I'd like to create my form markup, perhaps allowing them some editable fields which I can call myself within the form and then to use the API to process the form.
But if that's too complicated, I'm open to other ideas.

#5 vitor

vitor

    Jr. Member

  • Members
  • PipPip
  • 25 posts
  • 6

  • LocationPortugal

Posted 10 April 2012 - 08:53 AM

Ryan as posted a sample contact form to use as a simple contact form here: http://processwire.c...ndpost__p__8457

It can be useful for what you are trying to do.

#6 onjegolders

onjegolders

    Hero Member

  • Members
  • PipPipPipPipPip
  • 799 posts
  • 207

  • LocationMidlands, UK

Posted 10 April 2012 - 09:00 AM

Ryan as posted a sample contact form to use as a simple contact form here: http://processwire.c...ndpost__p__8457

It can be useful for what you are trying to do.


Thanks Vitor, I had seen that and think I will try and get that working or something similar.

#7 onjegolders

onjegolders

    Hero Member

  • Members
  • PipPipPipPipPip
  • 799 posts
  • 207

  • LocationMidlands, UK

Posted 10 April 2012 - 09:49 AM

Can't seem to get this form sending, have slightly modified Ryan's code but not working, code is below. Any one have any ideas?

<?php

// set this to the email address you want to send to (or pull from a PW field)
$emailTo = 'me@me.com';
// or if not set, we'll just email the default superuser
if(empty($emailTo)) { $emailTo = $users->get($config->superUserPageID)->email; }
// set and sanitize our form field values
$form = array(
'name' => $sanitizer->text($input->post->name),
'email' => $sanitizer->email($input->post->email),
'phone' => $sanitizer->text($input->post->phone),
'subject' => $sanitizer->text($input->post->subject),
'level' => $sanitizer->text($input->post->level),
'hours' => $sanitizer->text($input->post->hours),
'message' => $sanitizer->textarea($input->post->message),
);
// initialize runtime vars
$sent = false;
$error = '';
// 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(empty($value)) $error = "<h2 class='error'>Please check that you have completed all fields.</h2>";
}
// if no errors, email the form results
if(!$error) {
  $subject = "Contact Form";
  $message = '';
  foreach($form as $key => $value) $message .= "$key: $value\n";
  mail($emailTo, $subject, $message, "From: $form[email]");
  $sent = true;
}
}
if($sent) {
echo "<h2>Thank you, your message has been sent.</h2>"; // or pull from a PW field
} else {
echo "<h2>Sorry, that didn't work.</h2>";
}

Thanks

#8 apeisa

apeisa

    Hero Member

  • Moderators
  • 2,526 posts
  • 854

  • LocationVihti, Finland

Posted 10 April 2012 - 09:52 AM

Please, do always provide all the error messages and describe "how it doesn't work"?

#9 onjegolders

onjegolders

    Hero Member

  • Members
  • PipPipPipPipPip
  • 799 posts
  • 207

  • LocationMidlands, UK

Posted 10 April 2012 - 09:54 AM

Please, do always provide all the error messages and describe "how it doesn't work"?


Sorry Apeisa you're absolutely right.

Upon submit, it just outputs the 'else' statement ("Sorry that didn't work")

Edit: so I'm guessing that means - no errors, but didn't get sent?

Edited by onjegolders, 10 April 2012 - 09:55 AM.


#10 apeisa

apeisa

    Hero Member

  • Moderators
  • 2,526 posts
  • 854

  • LocationVihti, Finland

Posted 10 April 2012 - 10:32 AM

It does contain errors. Just echo $error.

Also, can we see the form markup?


#11 onjegolders

onjegolders

    Hero Member

  • Members
  • PipPipPipPipPip
  • 799 posts
  • 207

  • LocationMidlands, UK

Posted 10 April 2012 - 10:44 AM

At what point should can I echo $error? I echoed it in the final else statement but didn't see anything.

Here is whole contact page markup

<?php include("./header.inc"); ?>

<p class="italic_header">If you'd like to get in touch regarding tuition, please use the form below.</p>
<form action="" method="post" id="contact_form">

  <fieldset class="first">
   <h6>About yourself: </h6>
  
   <label for="name">Your name: </label>
   <input type="text" name="name" autofocus>
  
   <label for="email">Your email: </label>
   <input type="email" name="email">
  
   <label for="phone">Your phone number: </label>
   <input type="phone" name="phone">
  </fieldset>
  <fieldset>
   <h6 class="mt60">Your tuition: </h6>
	
	  <label for="subject">Subject: </label>
	  <select name="subject" id="subject" />
	  <?php
	  foreach ($page->subject_options as $subject) {
	   echo "<option>$subject->subject_option</option>";
	  }
	 ?>
	  </select>
	
	  <label for="level">Level: </label>
	  <select name="level" id="level" />
	  <?php
	  foreach ($page->level_options as $level) {
	   echo "<option>$level->level_option</option>";
	  }
	 ?>
	  </select>
	  
	  <label for="hours">Hours per week: </label>
	  <input type="number" min="1" max="40" value="2" name="hours">
  </fieldset>
  <div class="clear"></div><!-- /.clear -->
  
   <fieldset class="full">
	<label for="message">Additional information: </label>
	<textarea name="message" cols="30" rows="10"></textarea>
	<input type="submit" value="submit" id="submit">
   </fieldset>
</form>
<div id="contact_side">

  <h6>Or you can find me, here: </h6>
  <ul>
   <li>Skype: <?php echo $page->skype_name; ?></li>
   <li><a href="<?php echo $page->fb_url; ?>" target="_blank">Facebook</a></li>
   <?php if ($page->twitter_url) {
	echo "<li><a href='$page->twitter_url' target='_blank'>Twitter</a></li>";
   } ?>
  </ul>

<?php

// set this to the email address you want to send to (or pull from a PW field)
$emailTo = 'me@me.com';
// or if not set, we'll just email the default superuser
if(empty($emailTo)) { $emailTo = $users->get($config->superUserPageID)->email; }
// set and sanitize our form field values
$form = array(
'name' => $input->post->name,
'email' => $input->post->email,
'phone' => $input->post->phone,
'subject' => $input->post->subject,
'level' => $input->post->level,
'hours' => $input->post->hours,
'message' => $input->post->message,
);
// initialize runtime vars
$sent = false;
$error = '';
// 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(empty($value)) $error = "<h2 class='error'>Please check that you have completed all fields.</h2>";
}
// if no errors, email the form results
if(!$error) {
  $subject = "Contact Form";
  $message = '';
  foreach($form as $key => $value) $message .= "$key: $value\n";
  mail($emailTo, $subject, $message, "From: $form[email]");
  $sent = true;
}
}
if($sent) {
echo "<h2>Thank you, your message has been sent.</h2>"; // or pull from a PW field
} else {
echo "<h2>Sorry, that didn't work.</h2>";

}
?>

</div> <!-- end contact side -->
<?php include("./footer.inc"); ?>


Thanks for the help.

#12 onjegolders

onjegolders

    Hero Member

  • Members
  • PipPipPipPipPip
  • 799 posts
  • 207

  • LocationMidlands, UK

Posted 10 April 2012 - 12:20 PM

sorry, I removed $sanitizer from this just to check in case that was what was failing.
I've been looking at this code for hours and still no clue as to what's wrong!

Think I may have to fall back to a simple PHP form I've used before otherwise, except it has no validation or sanitizer :(

#13 Soma

Soma

    Hero Member

  • Moderators
  • 3,191 posts
  • 1745

  • LocationSH, Switzerland

Posted 10 April 2012 - 01:36 PM

The problem seems that you're checking for $input->post->submit, yet the submit button has no name="submit", so it never enters the code in the if.

I restructured a little your code. There's still a lot to improve, but you get the idea:



<?php

include("./head.inc");

$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(
  'name' => $input->post->name,
  'email' => $input->post->email,
  'phone' => $input->post->phone,
  'subject' => $input->post->subject,
  'level' => $input->post->level,
  'hours' => $input->post->hours,
  'message' => $input->post->message
);


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';
    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 {} ?>

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

  <p class="italic_header">If you'd like to get in touch regarding tuition, please use the form below.</p>
  <form action="./" method="post" id="contact_form">

    <fieldset class="first">
     <h6>About yourself: </h6>

     <label for="name">Your name: </label>
     <input type="text" name="name" autofocus>

     <label for="email">Your email: </label>
     <input type="email" name="email">

     <label for="phone">Your phone number: </label>
     <input type="phone" name="phone">
    </fieldset>
    <fieldset>
     <h6 class="mt60">Your tuition: </h6>

            <label for="subject">Subject: </label>
            <select name="subject" id="subject" />
            <?php
            foreach ($page->subject_options as $subject) {
             echo "<option>$subject->subject_option</option>";
            }
           ?>
            </select>

            <label for="level">Level: </label>
            <select name="level" id="level" />
            <?php
            foreach ($page->level_options as $level) {
             echo "<option>$level->level_option</option>";
            }
           ?>
            </select>

            <label for="hours">Hours per week: </label>
            <input type="number" min="1" max="40" value="2" name="hours">
    </fieldset>
    <div class="clear"></div><!-- /.clear -->

     <fieldset class="full">
          <label for="message">Additional information: </label>
          <textarea name="message" cols="30" rows="10"></textarea>
          <input type="submit" name="submit" value="submit" id="submit">
     </fieldset>
  </form>
  <div id="contact_side">

<?php else: // if success ?>

  <?php echo $success_message; ?>

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

  <h6>Or you can find me, here: </h6>
  <ul>
   <li>Skype: <?php echo $page->skype_name; ?></li>
   <li><a href="<?php echo $page->fb_url; ?>" target="_blank">Facebook</a></li>
   <?php if ($page->twitter_url) {
        echo "<li><a href='$page->twitter_url' target='_blank'>Twitter</a></li>";
   } ?>
  </ul>

</div> <!-- end contact side -->

<?php include("./foot.inc");

@somartist | modules created | support me, flattr my work flattr.com


#14 onjegolders

onjegolders

    Hero Member

  • Members
  • PipPipPipPipPip
  • 799 posts
  • 207

  • LocationMidlands, UK

Posted 10 April 2012 - 01:47 PM

Wow Soma, I can only thank you for putting so much time in to help me out. I've tested your code "as is" and the email gets sent perfectly.

I am a bit confused however about the structure. What exactly is happening before the form HTML gets called through?
Can i remove the print_r?
Also you mention alternative version at one point, is this duplicate code, should I be using one part or another?

Sorry for the questions and thanks again for taking the time to write.

#15 Soma

Soma

    Hero Member

  • Moderators
  • 3,191 posts
  • 1745

  • LocationSH, Switzerland

Posted 10 April 2012 - 02:17 PM

You're welcome, it's just a matter of debugging it right. You'll get more experienced the more you try to.

The print_r will just output the array in a readable format. It's a method to bebug, see it somethings there. You can remove it.

The alternative : is for if and foreach's in php you can do this to wrap instead of the curly brackets {}. It's easier if you're mixing html and php and instead of the closing } you write "endif;" or "endforeach;" - If you want an else in a if condition you write "else:".

<?php foreach($a as $<img src='http://processwire.com/talk/public/style_emoticons/<#EMO_DIR#>/cool.png' class='bbc_emoticon' alt='B)' /> : ?>
   <p><?php echo $b</p>
<?php endforeach; ?>


<?php if($a == $<img src='http://processwire.com/talk/public/style_emoticons/<#EMO_DIR#>/cool.png' class='bbc_emoticon' alt='B)' /> : ?>
   <p><?php echo $b</p>
<?php else: ?>
   <p>not same</p>
<?php endif; ?>


I moved the main php logic of the form to the top. This because I like it to have this way from top to bottom logic. I'm not saying you abolutely should do it that way, but for me it looks littler cleaner. There's also different approaches. (I'm sure diogo will post something else ;) )

This way I have set a $success var to be false and wrap the form into the if(!$success): and also print any errors before the form. And then in the else: of the if(!$success) is true have the success message printed and the form will not be shown.

@somartist | modules created | support me, flattr my work flattr.com


#16 onjegolders

onjegolders

    Hero Member

  • Members
  • PipPipPipPipPip
  • 799 posts
  • 207

  • LocationMidlands, UK

Posted 10 April 2012 - 03:41 PM

Thanks for your answers and for helping me to understand.
It's quite confusing at my level to know where to put the form and where the logic, I suppose the success or failure message would always have to come before or after the actual form.

I will take a closer look in the morning but I'm really grateful for the time you took to show me around :)

#17 onjegolders

onjegolders

    Hero Member

  • Members
  • PipPipPipPipPip
  • 799 posts
  • 207

  • LocationMidlands, UK

Posted 11 April 2012 - 04:48 AM

Would it be possible to take that code and only make some of the fileds "required"? As it is the user has to enter every field for the form to process.

#18 netcarver

netcarver

    Sr. Member

  • Members
  • PipPipPipPip
  • 428 posts
  • 341

  • LocationUK

Posted 11 April 2012 - 06:19 AM

Why not just delete the fields you don't require? Makes things simpler.
Steve ☧

#19 Soma

Soma

    Hero Member

  • Moderators
  • 3,191 posts
  • 1745

  • LocationSH, Switzerland

Posted 11 April 2012 - 06:44 AM

Would it be possible to take that code and only make some of the fileds "required"? As it is the user has to enter every field for the form to process.


Yes. http://bit.ly/IkP4qF

@somartist | modules created | support me, flattr my work flattr.com


#20 onjegolders

onjegolders

    Hero Member

  • Members
  • PipPipPipPipPip
  • 799 posts
  • 207

  • LocationMidlands, UK

Posted 11 April 2012 - 06:55 AM

Yes. http://bit.ly/IkP4qF


Thanks Soma for that link :)
I understand it's possible in PHP, I was just looking for help within the context of this code.
Thanks anyway

Why not just delete the fields you don't require? Makes things simpler.


Not sure I understand.
Some fields users may choose to fill in to provide further information but are optional.
If I delete them, the values won't get sent with the form.





Also tagged with one or more of these keywords: form, input

0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users