How to display $_POST variables in html within a php script?

Discussion in 'PHP' started by mrukjames, Oct 5, 2008.

  1. #1
    Hi, this is my first ever php script that I have been working on the last few days. It's now starting to drive me crazy. It's a feedback form for my website that displays a response when you submit something and then gets the server to email me a copy of the feedback. The original code I used was something like...

    <html>
    <body>
    <h1>Dear <?php echo $_POST["FirstName"]; ?>, thank you for your feedback!</h1>
    <h2>Your feedback will be evaluated</h2><br>
    
    
    You entered the following details: <br><br>
    
    <ul>
    
    <li>Your name is <?php echo $_POST["FirstName"]; ?> <?php echo $_POST["LastName"]; ?></li>
    <li>You are <?php echo $_POST["Age"]; ?> years old and <?php echo $_POST["sex"]; ?></li>
    <li>You are from <?php echo $_POST["Country"]; ?></li>
    <li>Your email address is <?php echo $_POST["EmailAddress"]; ?></li>
    </ul>
    
    
    <p>You rate this site:</p> <p class="gold"> <?php echo $_POST["Value"]; ?></p><br><br>
    <p>This is because:</p> <p class="green"><?php echo $_POST["comments"]; ?></p> <br><br></p>
    
    <p>And you think this site can be improved the following way:</p> <p class="green"> <?php echo $_POST["improvements"]; ?></p>
    
    <?php
    $to = "james@anxietymadewell.com";
    $subject = "Feedback from Website";
    $message = "Name: ".$_POST["FirstName"]." ".$_POST["LastName"]. "\nAge: ".$_POST["Age"]."\nSex: ".$_POST["sex"]."\nCountry:".$_POST["Country"]."\n\nRates this site: ".$_POST["Value"]."\n\nThis is because: ".$_POST["comments"]."\n\n".$_POST["improvements"];
    $from = $_POST["EmailAddress"];
    $headers = "From: $from";
    mail($to,$subject,$message,$headers);
    ?>
    
    </body>
    </html> 
    Code (markup):
    This worked. But I want to put a captcha on my form. Now my php code is...

    
    <html>
    <body>
    <?php 
       session_start();
       if(($_SESSION['security_code'] == $_POST['security_code']) && (!empty($_SESSION['security_code'])) ) {
          // Insert you code for processing the form here, e.g emailing the submission, entering it into a database. 
    $to = "james@anxietymadewell.com";
    $subject = "Feedback from Website";
    $message = "Name: ".$_POST["FirstName"]." ".$_POST["LastName"]. "\nAge: ".$_POST["Age"]."\nSex: ".$_POST["sex"]."\nCountry:".$_POST["Country"]."\n\nRates this site: ".$_POST["Value"]."\n\nThis is because: ".$_POST["comments"]."\n\n".$_POST["improvements"];
    $from = $_POST["EmailAddress"];
    $headers = "From: $from";
    mail($to,$subject,$message,$headers);
          unset($_SESSION['security_code']);
    
    echo '<h1>Dear <?php echo $_POST["FirstName"]; ?>, thank you for your feedback!</h1>
    <h2>Your feedback will be evaluated</h2><br>
    
    
    You entered the following details: <br><br>
    
    <ul>
    
    <li>Your name is <?php echo $_POST["FirstName"]; ?> <?php echo $_POST["LastName"]; ?></li>
    <li>You are <?php echo $_POST["Age"]; ?> years old and <?php echo $_POST["sex"]; ?></li>
    <li>You are from <?php echo $_POST["Country"]; ?></li>
    <li>Your email address is <?php echo $_POST["EmailAddress"]; ?></li>
    </ul>
    
    
    <p>You rate this site:</p> <p class="gold"> <?php echo $_POST["Value"]; ?></p><br><br>
    <p>This is because:</p> <p class="green"><?php echo $_POST["comments"]; ?></p> <br><br></p>
    
    <p>And you think this site can be improved the following way:</p> <p class="green"> <?php echo $_POST["improvements"]; ?></p>
    
    
    
    <p>Click <a href="../index.html">Here</a> to return back to the main page</p>.
    </body>
    </html>';
    
    
       } else {
          // Insert your code for showing an error message here
    echo "Sorry the security code you typed in seems to be invalid. Please go back, reload the page and try again. The information you typed in should still be there. Thanks.";
       }
    ?>
    Code (markup):
    The problem here is that the echo $_POST variables are not working within the html. How can I get around this problem? How can I get the same html output as before?

    Thanks
     
    mrukjames, Oct 5, 2008 IP
  2. Scorpiono

    Scorpiono Well-Known Member

    Messages:
    1,330
    Likes Received:
    35
    Best Answers:
    0
    Trophy Points:
    120
    #2
    I use normal quotes $_POST['normalquote'] but not sure if that's the problem..
     
    Scorpiono, Oct 5, 2008 IP
  3. mrukjames

    mrukjames Peon

    Messages:
    66
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Hi Scorpiono,

    Thanks for your suggestion. I tried doing that but it produces a syntax error when the page loads. Any other suggestions? What is the correct way of doing what I want in php? How would you setup this script?
     
    mrukjames, Oct 5, 2008 IP
  4. TwistMyArm

    TwistMyArm Peon

    Messages:
    931
    Likes Received:
    44
    Best Answers:
    0
    Trophy Points:
    0
    #4
    We probably need to see the code for the form that you are using, too... are you sure the form is using POST, for example?
     
    TwistMyArm, Oct 5, 2008 IP
  5. mrukjames

    mrukjames Peon

    Messages:
    66
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Hi the form I am using is...

    <html>
    <body>
    
    <h1>Feedback form</h1>
    <h2>Please fill out this feedback form to better improve your experience of this site</h2>
    
    
    <br><br>
    
    <fieldset>
    <legend>
    
    AnxietyMadeWell.Com Feedback Form:
    </legend>
    <form name="input" action="feedback.php" method="post">
    First name:
    <input type="text" name="FirstName" size="20">
    Last name: 
    <input type="text" name="LastName" size="20">
    <br><br>
    Your age: 
    <input type="text" name="Age" size="4">
    <br><br>
    Your Country/Location:
    <input type="text" name="Country" size="20">
    <br><br>
    Email address:
    <input type="text" name="EmailAddress" size="20"><br><br>
    
    
    <input type="radio" name="sex" value="Male"> Male
    <br>
    <input type="radio" name="sex" value="Female"> Female
    
    <br><br>
    
    How do you rate this site?<br><br>
    
    Excellent:
    <input type="radio" name="Value" value="Excellent">
    
    Good:
    <input type="radio" name="value" value="Good">
    
    Okay: 
    <input type="radio" name="value" value="Okay">
    
    Bad: 
    <input type="radio" name="value" value="Bad">
    
    Terrible: 
    <input type="radio" name="value" value="Terrible"><br><br>
    
    <textarea rows="5" cols="30" name="comments">Why?</textarea>
    
    <br><br>
    
    <textarea rows="5" cols="30" name="improvements">How could this site be improved?</textarea>
    
    <br><br>
    
    <img src="CaptchaSecurityImages.php">
    Security Code: 
    <input id="security_code" name="security_code" type="text" size="5">
    <br><br>
    
    <input type="submit" value="Submit">
    
    
    
    </form>
    </fieldset>
    </body>
    </html>
    Code (markup):
     
    mrukjames, Oct 5, 2008 IP
  6. Barti1987

    Barti1987 Well-Known Member

    Messages:
    2,703
    Likes Received:
    115
    Best Answers:
    0
    Trophy Points:
    185
    #6
    Use string concatenation:

    
    echo '<h1>Dear '. $_POST[FirstName].' thank you for your feedback!</h1> ....
    
    PHP:
    Peace,
     
    Barti1987, Oct 5, 2008 IP
  7. mrukjames

    mrukjames Peon

    Messages:
    66
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #7
    Hi Azizny,

    It worked! You completely solved my problem. Thank you.
     
    mrukjames, Oct 6, 2008 IP
  8. emel

    emel Peon

    Messages:
    15
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #8
    another easy was is to you $_REQUEST[FirstName]
    then it doesn't matter if the data was sent by post or get
     
    emel, Oct 7, 2008 IP
  9. mrukjames

    mrukjames Peon

    Messages:
    66
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #9
    Hi Thanks for you advice so far.
    This is the current version of my script. It completely works. However I don't think the error message is very customer friendly, such as asking people to go back and reload the page for the security image.

    So I am wondering how to the display the error message on the same page as the form? Any ideas or suggestions?



    
    <?php 
       session_start();
       if(($_SESSION['security_code'] == $_POST['security_code']) && (!empty($_SESSION['security_code'])) ) {
          // Insert you code for processing the form here, e.g emailing the submission, entering it into a database. 
    
    $output = '<html>
    <head>
    <title>Feedback Form- Confirmed</title>
      <link rel="stylesheet" href="../css/feedback.css" type="text/css">
    </head>
    <h1>Dear '.$_POST["FirstName"].', thank you for your feedback!</h1>
    <h2>Your feedback will be evaluated</h2><br>
    You entered the following details:
    <ul>
    <li>Your name is '.$_POST["FirstName"].' '.$_POST["LastName"].'</li>
    <li>You are '.$_POST["Age"].' years old and '.$_POST["sex"].'</li>
    <li>You are from '.$_POST["Country"].'</li>
    </ul>
    <p>You rate this site: </p> <p class="gold">'.$_POST["Value"].'</p>
    <p>Your reason for this is: </p> <p class="green">'.$_POST["comments"].'</p></p>
    <p>And this is your suggestion on how to improve the site: </p> <p class="green">'.$_POST["improvements"].'</p>
    <br><br>
    <p>Click <a href="../index.html">Here</a> to return back to the main page</p>.
    </body>
    </html>';
    
    $to = "james@anxietymadewell.com";
    $subject = "Feedback from Website";
    $message = $output; 
    $from = $_POST["EmailAddress"];
    $headers = "From: $from";
    $headers  = 'MIME-Version: 1.0' . "\r\n";
    $headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
    
    mail($to,$subject,$message,$headers);
          unset($_SESSION['security_code']);
    
    $name = $_POST["FirstName"];
    
    echo $output;
    
      } else {
          // Insert your code for showing an error message here
    
    echo "The security code you typed seems to be invalid. Please go back, reload the page and try again. The information you typed in should still be there. Thanks.";
       }
    
    ?>
    PHP:

    Thanks
     
    mrukjames, Oct 9, 2008 IP
  10. gzav

    gzav Peon

    Messages:
    46
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #10
    Would that work? I always thought you had to put quotations around the variables within arrays...
     
    gzav, Oct 9, 2008 IP
  11. TwistMyArm

    TwistMyArm Peon

    Messages:
    931
    Likes Received:
    44
    Best Answers:
    0
    Trophy Points:
    0
    #11
    In reality, PHP sees that as a constant named FirstName. However, if no constant by that name exists, it assumes the constant is a string whose value is the same as the name of the constant itself. ie. if there is no constant by the name of FirstName, it will assume that it means "FirstName". If you make your error reporting picky enough, it will tell you that.

    So yes, you should use quotes around that FirstName (either single or double).
     
    TwistMyArm, Oct 9, 2008 IP
  12. Barti1987

    Barti1987 Well-Known Member

    Messages:
    2,703
    Likes Received:
    115
    Best Answers:
    0
    Trophy Points:
    185
    #12
    When I use them within statements that have apostrophes I don't use apostrophes, it makes it easier to read.

    What TwistMyArm said was correct, you should enclose them with apostrophes. Only use quotes when you need to use variables:

    
    $number = 3;
    $arrayName['field_3'] = 'test';
    echo $arrayName["field_$number"];
    
    PHP:
    Peace,
     
    Barti1987, Oct 9, 2008 IP
  13. mrukjames

    mrukjames Peon

    Messages:
    66
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #13
    Hi, is it possible to use something like this in PHP to reload <img src="CaptchaSecurityImages.php"> in the cache when you return back to the form from the captcha error message?


    <img src="picture.gif" name="myImageName">
    
    <script language="JavaScript"><!--
    function reloadImage() {
        var now = new Date();
        if (document.images) {
            document.images.myImageName.src = 'picture.gif?' + now.getTime();
        }
        setTimeout('reloadImage()',10000);
    }
    
    setTimeout('reloadImage()',10000);
    //--></script>
    Code (markup):
     
    mrukjames, Oct 10, 2008 IP