20i Reseller Hosting

php form response

Discussion in 'Scripts and Coding' started by theonemanyuri, Nov 16, 2012.

Thread Status:
Not open for further replies.
  1. theonemanyuri

    theonemanyuri Active Member

    Joined:
    Jun 2010
    Posts:
    556
    Likes Received:
    2
    I don't have any clue about php really so if anyone could give a layman's answer that would be awesome. I've got a contact form, got it all styled as I want, but on submit it currently prints a response, unformatted, on a white page (code below).

    if($sent)
    {print "Your mail was sent successfully"; }
    else
    {print "We encountered an error sending your mail"; }

    How could I change this to to make it a bit more sophisticated? I don't really want to use js, is there a php solution? Either a pop up to confirm it was/wasn't sent or ideally printing those statements somewhere on the page with the form, as opposed to unformatted on a separate page?
     
  2. Domain Forum

    Acorn Domains Elite Member

    Joined:
    1999
    Messages:
    Many
    Likes Received:
    Lots
    articles.co.uk
     
  3. fish United Kingdom

    fish Well-Known Member

    Joined:
    Nov 2006
    Posts:
    2,725
    Likes Received:
    27
    if successful redirect to home page maybe?
     
  4. theonemanyuri

    theonemanyuri Active Member

    Joined:
    Jun 2010
    Posts:
    556
    Likes Received:
    2
    Could do, what would the code be dude? I tried sending it back to the homepage before and it didn't work for some reason
     
  5. dlone United Kingdom

    dlone Active Member

    Joined:
    Aug 2006
    Posts:
    156
    Likes Received:
    3
  6. theonemanyuri

    theonemanyuri Active Member

    Joined:
    Jun 2010
    Posts:
    556
    Likes Received:
    2
    That's what I tried and it didn't work, I must have put it in the wrong place. Could you give it in context to the rest of the code? Is this right?

    if($sent)
    {header('Location: http://www.example.com/'); }
    else
    {print "We encountered an error sending your mail"; }

    I literally know sod all php, apologies.
     
  7. AssetDomains

    AssetDomains Well-Known Member

    Joined:
    Feb 2010
    Posts:
    3,046
    Likes Received:
    69
    Try it like this

    Code:
    <?php
    if($sent)
    header('Location: [URL]http://stackoverflow.com'[/URL]);
    else 
    {print "We encountered an error sending your mail"; } 
    ?>
    
    Is there other code on your page before this as it wont work if there is
     
  8. theonemanyuri

    theonemanyuri Active Member

    Joined:
    Jun 2010
    Posts:
    556
    Likes Received:
    2
    This is as it is on site, so yeh there is code before it. Would it work if I brought the if and else statements to the top as you've done, and put the the rest below it? Appreciate the help guys, cheers.

    Code:
    <?php 
     $to = "contact@mysite.co.uk"; 
     $subject = "Contact Us"; 
     $name = $_REQUEST['name'] ;
     $email = $_REQUEST['email'] ; 
     $message = $_REQUEST['message'] ; 
     $headers = "From: $email"; 
     $sent = mail($to, $subject, $message, $headers) ; 
     if($sent)
     header('Location: http://stackoverflow.com'); 
     else 
     {print "We encountered an error sending your mail"; }
     ?> 
     
  9. AssetDomains

    AssetDomains Well-Known Member

    Joined:
    Feb 2010
    Posts:
    3,046
    Likes Received:
    69
    If thats all is on the page it should work I think. There is no blank lines or any other text before the code is there
     
  10. theonemanyuri

    theonemanyuri Active Member

    Joined:
    Jun 2010
    Posts:
    556
    Likes Received:
    2
    Nope the whole page is exactly as you see there. It works now, thanks for the help!
     
Thread Status:
Not open for further replies.