SitePoint Sponsor

User Tag List

Results 1 to 23 of 23
  1. #1
    SitePoint Enthusiast
    Join Date
    Jun 2003
    Location
    Lusatia
    Posts
    59
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    PHP "quiz"/questionnaire

    Hi there!

    I am wondering if anybody could assist me in prepering a PHP mail form?

    Personally I have no knowledge of PHP but I heared is one of the best tools for these sort of things.

    I would like to post on the web a "quiz"/questionnaire to promote the knowledge of our ethnic minority. Basically what I would like the viewers to do is to answear questions to test their knowledge of our minority.

    The form should send back questions and answears via an email and perhps also say thank you in an "echo" and go back to a set page.

    Any help would be greatly appreciated!!!

    you can view the fourmular at:

    http://www21.brinkster.com/lusatia/

  2. #2
    SitePoint Evangelist worksdev's Avatar
    Join Date
    Mar 2002
    Location
    Central, PA - originally from Monterey, CA
    Posts
    497
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hello wat:

    This scripts takes any form posted to it and mails the contents to you:

    PHP Code:
    <?php 
    // ************Begin Configure***************
    //Put where you want the email to go
    $mailto "your@email.com";
    //Put your subject in here
    $subject "Your subject here.";
    //Put where to redirect to after sending the email
    $redirect "thankyoupage.html";
    // ************End Configure****************


    foreach($HTTP_POST_VARS as $key => $value) {

          
    $message .= $key ':  ' $value
          
    $message .= "\n"

    }
    if (@
    mail($mailto$subject$message)) {
      
      
    header("Location: $redirect");
    } else {
      
    // This echo's the error message if the email did not send.  
      // You could change  the text in between the <p> tags.
      
    echo('<p>Mail could not be sent.  Please use your back button to try again.</p>');
    }
    ?>
    You can put your thank you message in the redirect page.

    If you need help modifying it, let me know.

    Best Regards,
    worksdev
    Church Website Design by Enlighten Web Services -
    Web design-hosting-support for Christian ministries
    Friendly Community of Webmasters
    Christian Websites :: Christian Web Design

  3. #3
    SitePoint Wizard
    Join Date
    Mar 2002
    Location
    Bristol, UK
    Posts
    2,240
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hey, I never knew you could do it like that, mind if I use it?

    -Sam
    Sam Hastings

  4. #4
    SitePoint Evangelist worksdev's Avatar
    Join Date
    Mar 2002
    Location
    Central, PA - originally from Monterey, CA
    Posts
    497
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Devilware.net
    Hey, I never knew you could do it like that, mind if I use it?
    -Sam
    I don't mind. It really came from the help of other posters here on SitePoint.

    Best Regards,
    worksdev
    Church Website Design by Enlighten Web Services -
    Web design-hosting-support for Christian ministries
    Friendly Community of Webmasters
    Christian Websites :: Christian Web Design

  5. #5
    SitePoint Wizard
    Join Date
    Mar 2002
    Location
    Bristol, UK
    Posts
    2,240
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks [img]images/smilies/smile.gif[/img] It'll definitely come in handy as a generic form handler - as hardly any customisation is needed.

    -Sam
    Sam Hastings

  6. #6
    SitePoint Evangelist netkid's Avatar
    Join Date
    Jun 2003
    Location
    USA
    Posts
    563
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    yea...i think ill use it too

    Santosh Sankar
    Bullish Bankers

  7. #7
    SitePoint Evangelist netkid's Avatar
    Join Date
    Jun 2003
    Location
    USA
    Posts
    563
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Can you explain this part:

    PHP Code:

    foreach($HTTP_POST_VARS as $key => $value) { 

          
    $message .= $key ':  ' $value
          
    $message .= "\n"


    where do the $key and $value variables come from?

    What element off the form?? does it come from the form??

    Please explain the code...I'm sorta new to this...

    Santosh Sankar
    Bullish Bankers

  8. #8
    SitePoint Wizard
    Join Date
    Mar 2002
    Location
    Bristol, UK
    Posts
    2,240
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    $HTTP_POST_VARS is the array sent by the form, and it's being looped through using key (the name of the form field) and value (what the user entered). Basically it grabs the form data that was sent

    -Sam
    Sam Hastings

  9. #9
    SitePoint Evangelist netkid's Avatar
    Join Date
    Jun 2003
    Location
    USA
    Posts
    563
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    So $HTTP_POST_VARS will take the form values from the page that the PHP is being used from??

    And $key is just a variable you set so the array from the form can loop through it?

    Santosh Sankar
    Bullish Bankers

  10. #10
    SitePoint Wizard siteguru's Avatar
    Join Date
    Oct 2002
    Location
    Scotland
    Posts
    3,631
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    A PHP solution is no good to wat - (s)he is a Brinkster member, and a General one at that, so ...

    No PHP - only ASP
    No email components available.

    Ian Anderson
    www.siteguru.co.uk

  11. #11
    SitePoint Evangelist netkid's Avatar
    Join Date
    Jun 2003
    Location
    USA
    Posts
    563
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    then Javascript for her then??

    Santosh Sankar
    Bullish Bankers

  12. #12
    SitePoint Wizard siteguru's Avatar
    Join Date
    Oct 2002
    Location
    Scotland
    Posts
    3,631
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    That said, the ASP equivalent to
    PHP Code:
    foreach($HTTP_POST_VARS as $key => $value) { 

          
    $message .= $key ':  ' $value
          
    $message .= "\n"


    is
    Code:
    For each key in Request.Form 
    
          message = message & key & ":  " & Request.Form(key) & vbNewLine 
    
    }
    Ian Anderson
    www.siteguru.co.uk

  13. #13
    SitePoint Evangelist worksdev's Avatar
    Join Date
    Mar 2002
    Location
    Central, PA - originally from Monterey, CA
    Posts
    497
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by siteguru
    A PHP solution is no good to wat - (s)he is a Brinkster member, and a General one at that, so ...

    No PHP - only ASP
    No email components available.

    Here's a simular script written in ASP:


    Code:
    <%@ Language=VBScript %>
    
    <%
    ' The original code came from http://www.4guysfromrolla.com/webtech/071599-1.2.shtml
    ' I modified the code somewhat.
    ' Many of the comments are thiers.
    'I have added a few comments.
    
    	'Who does this go to?
    	Const strTo = "youremail@website.com"
    	
    	'This information is optional -- Just name the fields on the form you submit to this.
    	Dim strFrom, strSubject, strRedirectURL, strFromPath
    	
    	strFrom = Request.Form("email")
    	if Len(strFrom) = 0 then strFrom = strTo
    	
    	strSubject = Request.Form("txtEmailSubject")
    	if Len(strSubject) = 0 then strSubject = "Contact from My Web Site!"
    	
    strRedirectURL = "thankyou.asp"
    	
    
    	
    	
    
    	Dim strBody
    	strBody = strHeader & vbCrLf & vbCrLf
    	strBody = strBody & "FORM: " & strFromPath & vbCrLf & _
    			  "FORM submitted at " & Now() & vbCrLf & vbCrLf
    
    'This section iterates through the form to get all it's values and then send them.
    	For I = 1 To Request.Form.Count
        fieldName = Request.Form.Key(I)
        fieldValue = Request.Form.Item(I) 
    
    					strBody = strBody & fieldName & ": " & fieldValue & vbCrLf & vbCrLf
    
    	
    	Next
    	
    
    	
    	strBody = strBody & vbCrLf & strFooter
    	
    	'Time to send the email
    	Dim objCDO
    	Set objCDO = Server.CreateObject("CDONTS.NewMail")
    	objCDO.To = strTo
    	objCDO.From = strFrom
    	
    	objCDO.Subject = strSubject
    	objCDO.Body = strBody
    	
    	objCDO.Send
    	
    	Set objCDO = Nothing
    	
    	Response.Redirect strRedirectURL
    %>
    Church Website Design by Enlighten Web Services -
    Web design-hosting-support for Christian ministries
    Friendly Community of Webmasters
    Christian Websites :: Christian Web Design

  14. #14
    SitePoint Enthusiast
    Join Date
    Jun 2003
    Location
    Lusatia
    Posts
    59
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    PHP questionnaire

    Hi worksdev, hi guys!

    Thanks a lot for your qiuck and positive reply. Yes, I would like to take you up on your offer of help, especially as I have no knowledge of PHP. If you wouldn't kind of mind walking me through this, perhaps I can learn it at the same time.

    Questions:

    1) do I need to create any other files or just save the code as "form.php" and upload it?

    2) Where do I put values for the "questionnaire" so that when I get the email with answears things look like this:

    II. What minority lives in your region?

    ANSWEAR

    etc...

    Thanks again for your help!

    P.S. I can use PHP on our local server but no ASP or VBS etc. The Brinkster account is just for testing things...

  15. #15
    SitePoint Enthusiast press711's Avatar
    Join Date
    Apr 2003
    Location
    gold coast
    Posts
    70
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Also try http://www.phpguru.org/mime.mail.html . A powerfull yet easy to use class comes with examples.
    ---------------


  16. #16
    SitePoint Evangelist worksdev's Avatar
    Join Date
    Mar 2002
    Location
    Central, PA - originally from Monterey, CA
    Posts
    497
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I also like this class.

    http://phpmailer.sourceforge.net/

    Best Regards,
    worksdev
    Church Website Design by Enlighten Web Services -
    Web design-hosting-support for Christian ministries
    Friendly Community of Webmasters
    Christian Websites :: Christian Web Design

  17. #17
    SitePoint Evangelist worksdev's Avatar
    Join Date
    Mar 2002
    Location
    Central, PA - originally from Monterey, CA
    Posts
    497
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hello wat:

    Please see the answers below:

    Quote Originally Posted by wat
    1) do I need to create any other files or just save the code as "form.php" and upload it?
    Yes. The process would be to edit the config section with your information. I would save the file as something like formprocessor.php or mailform.php.

    Then upload it to your php equiped server.

    The script redirects to a thank you page. You set the page in the config section. So make sure you have a page to send it to.

    When you submit your form to it, make sure the form action is "post". Make sure your form action is to submit to the name of the form processor script.

    Quote Originally Posted by wat
    2) Where do I put values for the "questionnaire" so that when I get the email with answears things look like this:

    II. What minority lives in your region?

    ANSWEAR
    The processor works in that it puts the name of the text field next to the value that was submitted. Something like:

    Name: Sean
    email: myemail@my_site.com

    So you will want to name your text fields acording to your questions. Having the full question might be a little long. So for the question:

    1. Welches slawische Volk ist in der Lausitz in Sachsen und Brandenburg beheimatet ?

    You may want to name the text field:

    1_Welches_slawische

    Or something shorter:

    Then the form field will mail it to you as:

    1_Welches_slawische: People group

    Hope that helps.

    Of course your server will also have to be set up correctly to use the mail function.

    If you have any issues let me know.

    Best Regards,
    worksdev
    Church Website Design by Enlighten Web Services -
    Web design-hosting-support for Christian ministries
    Friendly Community of Webmasters
    Christian Websites :: Christian Web Design

  18. #18
    SitePoint Enthusiast
    Join Date
    Jun 2003
    Location
    Lusatia
    Posts
    59
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hey worksdev,

    Thank you so much for your help. Should I have more questions with this form can I trouble you again?

    Many thanks again :-)

  19. #19
    SitePoint Evangelist worksdev's Avatar
    Join Date
    Mar 2002
    Location
    Central, PA - originally from Monterey, CA
    Posts
    497
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Yes. It's no trouble.

    If you need any help just let me know.

    If you run this quiz for a long time, or for a lot of people, you may eventually want to consider having your quiz results submit directly into a database.

    This makes it easy to keep track of data and run reports.

    Best Regards,
    worksdev
    Church Website Design by Enlighten Web Services -
    Web design-hosting-support for Christian ministries
    Friendly Community of Webmasters
    Christian Websites :: Christian Web Design

  20. #20
    SitePoint Enthusiast
    Join Date
    Jun 2003
    Location
    Lusatia
    Posts
    59
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi worksdev,

    I finally did make the "questionnaire" and I tested it today. Shoot! I am getting the error echo:

    "Mail could not be sent. Please use your back button to try again" ????

    This is what I have in my form:

    <form name="formprocessor" method="post" action="mailform.php">

    I named my fields like this:

    <input type="text" name="1_question" size="20">

    and this is the php code

    <?php
    // ************Begin Configure***************
    //Put where you want the email to go
    $mailto = "stiftung-ski@sorben.com";
    //Put your subject in here
    $subject = "Quiz";
    //Put where to redirect to after sending the email
    $redirect = "dzakuju.html";
    // ************End Configure****************


    foreach($HTTP_POST_VARS as $key => $value) {

    $message .= $key . ': ' . $value;
    $message .= "\n";

    }
    if (@mail($mailto, $subject, $message)) {

    header("Location: $redirect" );
    } else {
    // This echo's the error message if the email did not send.
    // You could change the text in between the <p> tags.
    echo('<p>Mail could not be sent. Please use your back button to try again.</p>');
    }
    ?>


    The form page and the php page are in the same folder so no root directory mistakes...???

    Could you tell me what am I doing wrong?! Thanks for your help :-)

  21. #21
    public static void brain Gybbyl's Avatar
    Join Date
    Jun 2002
    Location
    Montana, USA
    Posts
    647
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You might not have the right mail libraries installed on your server. Change that code so it looks like this:
    PHP Code:
    // ************Begin Configure***************
    //Put where you want the email to go
    $mailto "stiftung-ski@sorben.com";
    //Put your subject in here
    $subject "Quiz";
    //Put where to redirect to after sending the email
    $redirect "dzakuju.html";
    // ************End Configure****************


    foreach($HTTP_POST_VARS as $key => $value) {
        
    $message .= $key ': ' $value;
        
    $message .= "\n";
    }
    $check mail($mailto$subject$message) or die($php_errormsg);
    if (
    $check) {
        
    header("Location: $redirect);
    } else {
        
    // This echo's the error message if the email did not send.
        // You could change the text in between the <p> tags.
        
    echo('<p>Mail could not be sent. Please use your back button to try again.</p>');

    That should give you a nice little error printout if (when) something goes wrong.
    Ryan

  22. #22
    public static void brain Gybbyl's Avatar
    Join Date
    Jun 2002
    Location
    Montana, USA
    Posts
    647
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Actually, this would do the trick, too:

    PHP Code:
    // ************Begin Configure***************
    //Put where you want the email to go
    $mailto "stiftung-ski@sorben.com";
    //Put your subject in here
    $subject "Quiz";
    //Put where to redirect to after sending the email
    $redirect "dzakuju.html";
    // ************End Configure****************

    foreach($HTTP_POST_VARS as $key => $value) {
        
    $message .= $key ': ' $value;
        
    $message .= "\n";
    }
    if (
    mail($mailto$subject$message)) {
        
    header("Location: $redirect);
    } else {
        
    // This echo's the error message if the email did not send.
        // You could change the text in between the <p> tags.
        
    echo('<p>Mail could not be sent. Please use your back button to try again.</p>');

    As long as you aren't supressing the error with '@', it will die less gracefull, but at least you will know what the problem is.
    Ryan

  23. #23
    SitePoint Evangelist worksdev's Avatar
    Join Date
    Mar 2002
    Location
    Central, PA - originally from Monterey, CA
    Posts
    497
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi Wat:

    Sorry I was away for awhile.

    Did you get it working?

    Best Regards,
    worksdev
    Church Website Design by Enlighten Web Services -
    Web design-hosting-support for Christian ministries
    Friendly Community of Webmasters
    Christian Websites :: Christian Web Design


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •