SitePoint Sponsor

User Tag List

Results 1 to 25 of 25
  1. #1
    SitePoint Enthusiast
    Join Date
    Jan 2009
    Posts
    58
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    help needed for form processing code

    Hi all,

    I am completely new with php and have been trying for over a month to add some simple form processing code to the website of a friend. I have tried different scripts and tried changing it several times. I have also previously had some help from people on this forum (and thank you very much I appreciate it) and nothing seems to work.

    I am beginning to think that it must be the settings of the web hosting company but they say that it is the code and that they cant help with that as it is not their problem.

    I am at my wits end and if anyone can help I would be eternally grateful.

    Is anyone able to test to see if the code works as if they can I can contact the web hosting company and tell them it is a problem on their side.

    Also is it best on the php script to start and finish it with the <head> and
    <body> tags or does this make no difference?

    When I load it to the web page and submit the info it always comes up with the message "No se ha realizado el envio de informaciones a $email" (which was originally "invalid email address but have had to change it as my friend is Spanish) and if I delete this bit of code, a blank screen pops up and the the info is not sent.

    The php is:

    Code:
    <?php
    
    /* All form fields are automatically passed to the PHP script through the array $HTTP_POST_VARS. */
    
    $email = 'jan_colombini@hotmail.com';
    
    $contactname = $HTTP_POST_VARS['contactname'];
    
    $telephone = $HTTP_POST_VARS['telephone'];
    
    $email = $HTTP_POST_VARS['email'];
    $detalles = $HTTP_POST_VARS['detalles'];
    
    $tipo = $HTTP_POST_VARS['tipo'];
    
    
    
    if (!preg_match("/\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*/", $email)) {
    
      echo "<h4>Correo Invalido</h4>";
    
      echo "<a href='javascript:history.back(1);'>Back</a>";
    
    } elseif ($subject == "") {
    
      echo "<h4>Sin Mensaje</h4>";
    
      echo "<a href='javascript:history.back(1);'>Back</a>";
    
    }
    
    
    
    /* Sends the mail and outputs the "Thank you" string if the mail is successfully sent, or the error string otherwise. */
    
    elseif (mail($email, $contactname, $telephone, $email, $detalles, $tipo)) {
    
      echo "<h4>Su mensaje ha sido enviado</h4>";
    
    } else {
    
      echo "<h4>No se ha realizado el envio de informaciones a $email</h4>";
    
    }
    
    ?>
    and my HTML is:

    Code:
     <form action="form.php" method="post" class="contact">
    
            <fieldset>
    
             
    
              <div>
    
                <label for="contactname" class="fixedwidth">Nombre y Apellido</label>
    
                <input type="text" name="contactname" id="contactname"/>
    
              </div>
    
              <div>
    
                <label for="telephone" class="fixedwidth">Numero de Telefono</label>
    
                <input type="text" name="telephone" id="telephone"/>
    
              </div>
    
              <div>
    
                <label for="email" class="fixedwidth">Correo Electronico</label>
    
                <input type="text" name="email" id="email"/>
    
              </div>
    
              <div>  
    
                  <label for="tipo" class="fixedwidth">Tipo de Seguro</label>
    
                  <select name="tipo" id="tipo">     
    
                    <option>Rodaje de spots</option>
    
                    <option>Sesiones de Fotografía</option>
    
                    <option>Cortometrajes</option>     
    
                    <option>Otros suguros</option>  
    
                  </select> 
    
                </div>
    
                <div>
    
                  <p>Texto</p>
    
                  <div class="textarea">
    
                  <textarea name="detalles" id="details" cols="30" rows="7"></textarea>
    
                  </div>
    
    <div class="buttonarea">
    
    
    
      <input type="submit" value="Envianos la informacion"/>
    
    
    
    </div>
    
                </div>
    
               <p>Una vez recibida su consulta nos pondremos en contacto con usted en la mayor brevedad</p>
    
            </fieldset>
    
          </form>
    If anyone can help I really really would appreciate it.
    Jan

  2. #2
    dooby dooby doo silver trophybronze trophy
    spikeZ's Avatar
    Join Date
    Aug 2004
    Location
    Manchester UK
    Posts
    13,788
    Mentioned
    151 Post(s)
    Tagged
    3 Thread(s)
    Hi Jan
    There are a couple of problems in the form;

    $HTTP_POST_VARS was removed along time ago! Use $_POST now

    You check for the $subject field value but there is no subject field so the value will always be false

    Try the following code:
    PHP Code:
        $send true;
        
    $email 'jan_colombini@hotmail.com';
        
    $contactname $_POST['contactname'];
        
    $telephone $_POST['telephone'];
        
    $email $_POST['email'];
        
    $detalles $_POST['detalles'];
        
    $tipo $_POST['tipo'];
        
        if (!
    preg_match("/\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*/"$email)) {
          echo 
    "<h4>Correo Invalido</h4>";
          echo 
    "<a href='javascript:history.back(1);'>Back</a>";
          
    $send false;
        } 
        
    /* THERE IS NO SUBJECT FIELD!!!
        elseif ($subject == "") {
          echo "<h4>Sin Mensaje</h4>";
          echo "<a href='javascript:history.back(1);'>Back</a>";
          $send = false;
        }*/
        /* Sends the mail and outputs the "Thank you" string if the mail is successfully sent, or the error string otherwise. */
        
    if($send != false) {
        
    mail($email$contactname$telephone$email$detalles$tipo);
          echo 
    "<h4>Su mensaje ha sido enviado</h4>";
        } else {
          echo 
    "<h4>No se ha realizado el envio de informaciones a $email</h4>";
        } 
    It also looks like your error reporting is turned off which is why you gt the blank screen. Add the following code somewhere in your project:

    PHP Code:
    /** error handler
        ** set error reporting level to 0 for live site, 1 for dev */
    ini_set("display_errors"1); 
    ERROR_REPORTING(E_ALL); 
    And see what happens.
    Mike Swiffin - Community Team Advisor
    Only a woman can read between the lines of a one word answer.....

  3. #3
    SitePoint Enthusiast
    Join Date
    Jan 2009
    Posts
    58
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Wow,

    Spikez, I have had a lot of fantastic replies from this forum but this one really is on the top of the pile!!! Thank you very much. I cant test it at the moment as I am at work but I had a quick look at your reply and felt I had to send a thank you message to you for you hard work.

    I will let you know over the weekend if it has worked or if I still have problems.

    Thank you again
    Jan

  4. #4
    dooby dooby doo silver trophybronze trophy
    spikeZ's Avatar
    Join Date
    Aug 2004
    Location
    Manchester UK
    Posts
    13,788
    Mentioned
    151 Post(s)
    Tagged
    3 Thread(s)
    You are most welcome Jan, let me know how it goes
    Mike Swiffin - Community Team Advisor
    Only a woman can read between the lines of a one word answer.....

  5. #5
    SitePoint Addict
    Join Date
    Apr 2009
    Posts
    248
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    One other thing to note: it looks like you're running a Regex over the $email variable to check to make sure that it's a valid email address.

    There's actually a built in PHP function which will do this for you. Instead of running your Regex, try

    PHP Code:
    $email filter_var($_POST['email'], FILTER_VALIDATE_EMAIL); 
    This is cleaner, a heck of a lot easier to read, and usually ends up being quite a bit better than most peoples' home-grown expressions.

  6. #6
    SitePoint Enthusiast
    Join Date
    Jan 2009
    Posts
    58
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi Situationsoap,

    Thanks for the advice, one question though, (I am still learning the basics I am afraid) do I replace the following code you suggested:

    $email = filter_var($_POST['email'], FILTER_VALIDATE_EMAIL);

    for the whole of the section that I had?

    if (!preg_match("/\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*/", $email)) { echo "<h4>Correo Invalido</h4>"; echo "<a href='javascript:history.back(1);'>Back</a>"; $send = false;

    or just the first line?

    if (!preg_match("/\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*/", $email))

    and if I have to change it all, what message will it display if an incorrect email is sent? (As I need it in Spanish)

    Thanks again for your help and I will let you know how I get on.
    Jan

  7. #7
    SitePoint Addict
    Join Date
    Apr 2009
    Posts
    248
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Jan, the filter would only replace your preg. Sorry for not being clear on that one. I'd instead recommend
    PHP Code:
    $email filter_var($_POST['email'], FILTER_VALIDATE_EMAIL) or die("<h4>Correo Invalido</h4> <a href='javascript:history.back(1);'>Back</a>"); 
    This will print your error message if the email string isn't a valid email, or assign the variable if it is.

  8. #8
    SitePoint Enthusiast
    Join Date
    Jan 2009
    Posts
    58
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Thumbs up

    Thanks Situationsoap,

    I appreciate your help and will let you know how I get on.
    Jan

  9. #9
    SitePoint Enthusiast
    Join Date
    Jan 2009
    Posts
    58
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi,

    I am still having troubles, I amended the code to:

    Code:
    <?php
    
        $send = true;
        $email = 'jan_colombini@hotmail.com';
        $contactname = $_POST['contactname'];
        $telephone = $_POST['telephone'];
        $email = $_POST['email'];
        $detalles = $_POST['detalles'];
        $tipo = $_POST['tipo'];
    
    $email = filter_var($_POST['email'], FILTER_VALIDATE_EMAIL) or die("<h4>Correo Invalido</h4> <a href='javascript:history.back(1);'>Atras</a>"); 
    
      echo "<a href='javascript:history.back(1);'>Atras</a>";
    
    } elseif ($detalles == "") {
    
      echo "<h4>Sin Mensaje</h4>";
    
      echo "<a href='javascript:history.back(1);'>Atras</a>";
    
    }
    
    
    /* Sends the mail and outputs the "Thank you" string if the mail is successfully sent, or the error string otherwise. */
    
        if($send != false) {
        mail($email, $contactname, $telephone, $email, $detalles, $tipo);
          echo "<h4>Su mensaje ha sido enviado</h4>";
        } else {
          echo "<h4>No se ha realizado el envio de informaciones a $email</h4>";
        } 
    
    /** error handler
        ** set error reporting level to 0 for live site, 1 for dev */
    ini_set("display_errors", 1); 
    ERROR_REPORTING(E_ALL); 
    
    ?>
    It is still showing a blank screen as soon as you press the send info button but now it is not even showing the "back" link to go back to the previous page.

    I am not sure how to use the error handler bit so I put it at the bottom of the code but didn't seem to do anything.

    Do you have any other suggestions? I hope I am not starting to be a pain in the a****.
    Jan

  10. #10
    SitePoint Addict
    Join Date
    Apr 2009
    Posts
    248
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You should post the error handling code at the top of your script. Didn't have a chance to look at anything else yet, though.

  11. #11
    SitePoint Enthusiast
    Join Date
    Jan 2009
    Posts
    58
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    No worries,

    I will do that as soon as I can and let you know what comes up
    Jan

  12. #12
    SitePoint Addict
    Join Date
    Apr 2009
    Posts
    248
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    After a second look, it seems that you're sending way too many parameters to the mail function. Google PHP mail for the manual page (I can't post links yet) for more details, but I think you'll need to combine your variables into one message before calling the function.

  13. #13
    SitePoint Enthusiast
    Join Date
    Jan 2009
    Posts
    58
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I have tried to put the error handling code at the top but it still shows a blank screen!

    I have also changed the details to

    }if ($detalles == "") {

    Does anyone have any other suggestions?
    Jan

  14. #14
    SitePoint Addict
    Join Date
    Apr 2009
    Posts
    248
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    A favorite method of mine it to use

    PHP Code:
    print_r($_POST); 
    At the top of the page. If you're not throwing an error, and if you're not seeing content, then one of your variables probably isn't coming through correctly. Check the output of that function to make sure that your variables look like what they should.

  15. #15
    SitePoint Enthusiast
    Join Date
    Jan 2009
    Posts
    58
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi Situationsoap,

    You have worked so hard for me, I really appreciate it. Thank you.

    I put the code at the top of the page and transferred the page to the website and... nothing it is still it is only showing a blank page. I must be doing something really wrong. Either that or it is a basic mistake!

    I am not a quitter and I will get this sorted even if it kills me!! But in the mean time if you can think of anything else to help me I would really appreciate it!
    Jan

  16. #16
    Non-Member
    Join Date
    Apr 2009
    Posts
    115
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You are using $post values but is there even a form for the user to input these values? Can you post your entire file and give a link so we know if anything is outputted?

  17. #17
    SitePoint Enthusiast
    Join Date
    Jan 2009
    Posts
    58
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi A2k,

    My code now looks like this as I am no deleting any of the suggestions that I have found here:

    Code:
    <?php
    
    print_r($_POST); 
    
    
    
    /** error handler
        ** set error reporting level to 0 for live site, 1 for dev */
    ini_set("display_errors", 1); 
    ERROR_REPORTING(E_ALL); 
    
    
    
        $send = true;
        $email = $_POST['email'];
        $contactname = $_POST['contactname'];
        $telephone = $_POST['telephone'];
        $email = $_POST['email'];
        $detalles = $_POST['detalles'];
        $tipo = $_POST['tipo'];
    
        $recipient = "jan_colombini@hotmail.com"; 
    
    $email = filter_var($_POST['email'], FILTER_VALIDATE_EMAIL) or die("<h4>Correo Invalido</h4> <a href='javascript:history.back(1);'>Atras</a>"); 
    
      echo "<a href='javascript:history.back(1);'>Atras</a>";
    
    } elseif ($detalles == "") {
    
      echo "<h4>Sin Mensaje</h4>";
    
      echo "<a href='javascript:history.back(1);'>Atras</a>";
    
    }
    
    
    
    /* Sends the mail and outputs the "Thank you" string if the mail is successfully sent, or the error string otherwise. */
    
        if($send != false) {
        mail($recipient, $email, $contactname, $telephone, $email, $detalles, $tipo);
          echo "<h4>Su mensaje ha sido enviado</h4>";
        } else {
          echo "<h4>No se ha realizado el envio de informaciones a $email</h4>";
        } 
    
    
    ?>
    The link to the website is: http://www.elisegur.es/contact.html feel free to try the form and other options on it, I am sorry that it is in Spanish but it is a friends website, for the moment (until I get it right), I have put my email address in the field so all emails that are submitted will come to me. Feel free to try it out.

    If you can help i will be indebted to you!!

    Thanks again.
    Jan

  18. #18
    SitePoint Addict
    Join Date
    Apr 2009
    Posts
    248
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You set a class value in your form declaration on the contact data entry page (the one you linked to above). Why did you do that? I'm not sure that class is a valid attribute for HTML Forms, and I think (?) that might be what's causing your problem.

  19. #19
    SitePoint Enthusiast
    Join Date
    Jan 2009
    Posts
    58
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Really? I did not know that. I set it to be able to style it in CSS. You mean this bit right?

    Code:
    <form action="form.php" method="post" class="contact">
    I have set class values to all of the input sections, if you are right I will have to restyle most of the page (but if the PHP works I will feel like I have lost a small battle and won several major wars).

    Could you tell me if I understood right and the above line of code in my HTML is what you were referring to and could be causing the problem.
    Jan

  20. #20
    SitePoint Addict
    Join Date
    Apr 2009
    Posts
    248
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    That is the line of code I was referring to. I can't promise I'm right: I'm not a CSS expert by any stretch.

    Input types can most definitely have valid CSS definitions (and thus, classes), but Forms don't be default display anything: they simply collect and hold data, so I don't know why one would need a class for them.

    Like I said, that just seemed odd to me, and I don't see anything else that's jumping out as incorrect right now. Let me know how that turns out.

  21. #21
    Theoretical Physics Student bronze trophy Jake Arkinstall's Avatar
    Join Date
    May 2006
    Location
    Lancaster University, UK
    Posts
    7,062
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Forms are containers too. So if you want to style the area around a form (like box it), you'd be wasting code putting it inside a <div>, you can style the form as a block itself.
    Jake Arkinstall
    "Sometimes you don't need to reinvent the wheel;
    Sometimes its enough to make that wheel more rounded"-Molona

  22. #22
    SitePoint Addict
    Join Date
    Apr 2009
    Posts
    248
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by arkinstall View Post
    Forms are containers too. So if you want to style the area around a form (like box it), you'd be wasting code putting it inside a <div>, you can style the form as a block itself.
    You learn something new every day!

    Forgive my ignorance.

  23. #23
    Theoretical Physics Student bronze trophy Jake Arkinstall's Avatar
    Join Date
    May 2006
    Location
    Lancaster University, UK
    Posts
    7,062
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    You are willing to learn; therefore the word 'ignorance' is unjust.
    Jake Arkinstall
    "Sometimes you don't need to reinvent the wheel;
    Sometimes its enough to make that wheel more rounded"-Molona

  24. #24
    SitePoint Guru risoknop's Avatar
    Join Date
    Feb 2008
    Location
    end($world)
    Posts
    834
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Personally, I don't style forms with CSS anymore (well only the basic stuff like font, margins etc) because it is impossible to make them look the same in every popular browser (or at least in 4/5 most popular...).

    I just leave the form elements appearance to the browser rendering engine. Saves tons of time and the forms, even though they look ugly by default in some browsers, at least match the browser interface

  25. #25
    SitePoint Enthusiast
    Join Date
    Jan 2009
    Posts
    58
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Thumbs up

    Hi risoknop,

    I am only doing basic styling to the boxes themselves, Just to keep them in line and make them blend in a little, otherwise they seem to go all over the place.

    I am not just new at PHP I am fairly new at web design in general so it is always great to have the opinions of more experienced designers.

    I will have another look and play with the PHP for the form and maybe start a fresh and try one line at a time to see where the problem lies. I feel I have taken so much time from all the people here that I am starting to feel a little guilty.

    Just wanted to say a HUGE thank you to everyone here as without Sitepoint and you guys I don't think I would have even able to build my first website!! Knowing I have such a great resource for help gives me a lot more confidence to try new things and lets me enjoy everything I do so much more.

    Jan


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
  •