PHP Variable Failure

Discussion in 'PHP' started by weaverofwords, May 12, 2013.

  1. #1
    I have tried several different variations of this code, and nothing seems to work. I am new to PHP and am trying to move away from generators and do everything myself.

    I have also tried putting $fullmessage parameters all under one variable heading and this did not work either. I don't know where I'm going wrong. I've spent about 5 hours so far trying to get this thing right.

    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "w3">
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <title>Untitled Document</title>
    </head>

    <body>
    <?php

    $subject="$subject";

    $message="$message";

    $fname="$fname";

    $lname="$lname";

    $address1="$address1";

    $address2="$address2";

    $city="$city";

    $state="$state";

    $zip="$zip";

    $dayphone="$dayphone";

    $eveningphone="$eveningphone";

    $mail_from="$email";

    $fullmessage="Name: " . $lname "," . $fname . "\n";
    $fullmessage="Address: " . $address1 "\n" . $address2 "\n";
    $fullmessage="City:" . $city . "\n";
    $fullmessage="State: " . $state . "\n";
    $fullmessage="Zip: " . $zip . "\n";
    $fullmessage="Day Phone: " . $dayphone . "\n";
    $fullmessage="Night Phone: " . $eveningphone . "\n";
    $fullmessage="Email: " . $email_from . "\n";

    $header="from: $lname, $fname <$mail_from>";

    ';

    $send_contact=mail($to,$subject,$fullmessage,$header);

    ?>
    </body>
    </html>

    here is the other way i have tried, with several variations

    $fullmessage="Name: " $lname "," $fname \n
    "Address: "$address1 \n $address2 \n
    "City: "$city \n
    "State: "$state \n
    "Zip: "$zip \n
    "Day Phone: "$dayphone \n
    "Night Phone: "$eveningphone \n
    "Email: "$email_from \n;
     
    Solved! View solution.
    weaverofwords, May 12, 2013 IP
  2. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #2
    Instead of doing this:
    
    $subject="$subject";
    
    PHP:
    ... try this:
    
    $subject=$_POST['subject'];
    
    PHP:
    ... assuming that these values come from a form.

    Also take a look at this: http://php.net/manual/en/tutorial.forms.php

    Oh, and to append data to a variable, you have to do it like this:
    
    $fullmessage .= "My string";
    
    PHP:
    (Note the dot)
     
    nico_swd, May 12, 2013 IP
  3. weaverofwords

    weaverofwords Peon

    Messages:
    8
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    1
    #3
    And I need to do this with all of my variables? This is related to a form. Sorry I didn't designate that at first. I've seen it with $_Request, but I am using the $_POST on the page side. The big issue I'm having is in the $fullmessage variable though, that's what is giving me all the errors?
     
    weaverofwords, May 12, 2013 IP
  4. HuggyStudios

    HuggyStudios Well-Known Member

    Messages:
    724
    Likes Received:
    20
    Best Answers:
    26
    Trophy Points:
    165
    #4
    Also to add to what nico_swd said you need to change this part of your code:
    
    $fullmessage="Name: " . $lname "," . $fname . "\n";
    $fullmessage="Address: " . $address1 "\n" . $address2 "\n";
    $fullmessage="City:" . $city . "\n";
    $fullmessage="State: " . $state . "\n";
    $fullmessage="Zip: " . $zip . "\n";
    $fullmessage="Day Phone: " . $dayphone . "\n";
    $fullmessage="Night Phone: " . $eveningphone . "\n";
    $fullmessage="Email: " . $email_from . "\n";
    
    PHP:
    to

    
    $fullmessage = "Name: " . $lname "," . $fname . "\n";
    $fullmessage .= "Address: " . $address1 "\n" . $address2 "\n";
    $fullmessage .= "City:" . $city . "\n";
    $fullmessage .= "State: " . $state . "\n";
    $fullmessage .= "Zip: " . $zip . "\n";
    $fullmessage .= "Day Phone: " . $dayphone . "\n";
    $fullmessage .= "Night Phone: " . $eveningphone . "\n";
    $fullmessage .= "Email: " . $email_from . "\n";
    
    PHP:
    When adding (concatenating) to a string you need to place a period before the equals sign. Read more here: http://php.net/manual/en/language.operators.string.php
     
    HuggyStudios, May 12, 2013 IP
  5. weaverofwords

    weaverofwords Peon

    Messages:
    8
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    1
    #5
    After changing both, i'm still getting an empty email with only "name" "address" "city" "State" "zip" "day phone" "night phone" and "email" with no corresponding values. I've added the $_POST to all the defining variables as well.
     
    weaverofwords, May 12, 2013 IP
  6. HuggyStudios

    HuggyStudios Well-Known Member

    Messages:
    724
    Likes Received:
    20
    Best Answers:
    26
    Trophy Points:
    165
    #6
    HuggyStudios, May 12, 2013 IP
  7. weaverofwords

    weaverofwords Peon

    Messages:
    8
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    1
    #7
    http://pastebin.com/T0SJHu6D
     
    weaverofwords, May 12, 2013 IP
  8. weaverofwords

    weaverofwords Peon

    Messages:
    8
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    1
    #8
    This is a screenshot of the title of the email I receive after submitting the form, if this helps diagnose as well.
     

    Attached Files:

    weaverofwords, May 12, 2013 IP
  9. HuggyStudios

    HuggyStudios Well-Known Member

    Messages:
    724
    Likes Received:
    20
    Best Answers:
    26
    Trophy Points:
    165
    #9
    Have you got the HTML form that is used?
     
    HuggyStudios, May 12, 2013 IP
  10. weaverofwords

    weaverofwords Peon

    Messages:
    8
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    1
    #10
    http://pastebin.com/nZUUXrir
     
    weaverofwords, May 12, 2013 IP
  11. weaverofwords

    weaverofwords Peon

    Messages:
    8
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    1
    #11
    http://pastebin.com/fsrTXaJ6

    Sorry, i set the last post to PHP rather than HTML
     
    Last edited: May 12, 2013
    weaverofwords, May 12, 2013 IP
  12. #12
    When you are trying to get the value from a post field you need to enter the name value like this.
    HTML
    <input type="text" name="first_name" />
    PHP
    $first_name = $_POST['first_name'];

    I've fixed your code for you to get it working, there's some changes you should make and I don't have the time to rewrite this for you. Look into mail header injections, cross site scripting and error handling. These subjects can all be found on Google.

    Also when debugging you need to use var_dump, again Google it.
    New script:
    
     
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <title>Untitled Document</title>
    </head>
     
    <body>
    <?php
     
    if(isset($_POST['Submit'])) {
     
      $subject = $_POST["subject"];
       
      $message = $_POST["message"];
       
      $fname = $_POST["fname"];
       
      $lname = $_POST["lname"];
       
      $address1 = $_POST["address1"];
       
      $address2 = $_POST["address2"];
       
      $city = $_POST["city"];
       
      $state = $_POST["state"];
       
      $zip = $_POST["zip"];
       
      $dayphone = $_POST["dayphone"];
       
      $eveningphone = $_POST["eveningphone"];
       
      $mail_from = $_POST["email"];
       
      $fullmessage = "Name: $lname $fname \n";
      $fullmessage .= "Address: $address1 \n $address2 \n";
      $fullmessage .= "City: $city \n";
      $fullmessage .= "State: $state \n";
      $fullmessage .= "Zip: $zip \n";
      $fullmessage .= "Day Phone: $dayphone \n";
      $fullmessage .= "Night Phone: $eveningphone \n";
      $fullmessage .= "Email: $email_from \n";
       
      $header="from: $lname, $fname <$mail_from>";
       
      $to='weaverofwords@yahoo.com';
     
      if(mail($to,$subject,$fullmessage,$header)) {
          echo '<p>mail sent</p>';   
      }else{
          echo '<p>mail error</p>';
      }
     
    }
     
    ?>
    </body>
    </html>
    
    Code (markup):
    I haven't tested this code but it should work.
     
    HuggyStudios, May 12, 2013 IP
  13. weaverofwords

    weaverofwords Peon

    Messages:
    8
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    1
    #13
    Awesome, now I see where I was going wrong. Thank you very much. I used "$" way too much. thank you for adding the "if - else", since I'm learning, I was going to add that and the re-direct after I got it right, but I was at it for hours. The only thing not filled in is the email field in the email, but I will toy around with that now that I'm moving forward again.

    I will definately look at those links, I'm not trying to have people write my stuff for me, just show me the right way so I can eventually show others! :D
     
    weaverofwords, May 12, 2013 IP
  14. HuggyStudios

    HuggyStudios Well-Known Member

    Messages:
    724
    Likes Received:
    20
    Best Answers:
    26
    Trophy Points:
    165
    #14
    Your'e welcome, I strongly recommend looking into those areas I highlighted.
     
    HuggyStudios, May 12, 2013 IP