• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 289
  • Last Modified:

Accessing form elements from PHP

I have a form that asks for comments. The form is submitted to a PHP action program that emails off the contents and redisplays the form.  But the form is displayed by just including a form.html file, so it's just the generic form that gets generated again.  But I'd like to display "Thank you for your comments." in  the comment textarea element when I display the form the second time, so I need some way to get a hold of input#comments, which is a textarea in the  form, from php,   so I can set value to "Thank you for your comments."

Thanks for any suggestions
0
steva
Asked:
steva
3 Solutions
 
Dave BaldwinFixer of ProblemsCommented:
with what you described, I would just copy the original form, alter it to say what you want, and include the altered form instead of the original one.  Call it 'form2.html' or 'thanks.html' and include that instead.
0
 
Marco GasiFreelancerCommented:
As far as I know you can't do what you want this way. What do you mean with 'PHP action program'? Another php script?
Anyway, this is what I would do: post form to the same page (action="<?php echo $_SERVER['PHP_SELF']); ?>"). At the top of this page I would check if submit is post

if (isset($_POST['submit']){
  // retrieve/set values for form's fields and:
  //   1: send email using mail function PHP Mailer and
  //   2: printout the form with Thanks:
}else{
  //printopur the blank form
}

Simple as drink a glass of water :-)

Cheers
0
 
Ray PaseurCommented:
Suggest you get this book and work through the exercises.  From this and other questions here I can see that you're still learning your way around PHP.  Whether you are new to just PHP or new to programming, the SitePoint book is a big help.
http://www.sitepoint.com/books/phpmysql4/

The code snippet shows how to load something after a form is submitted.  See it in action here:
http://www.laprbass.com/RAY_form_thankyou_page.php

Best regards, ~Ray
<?php // RAY_form_thankyou_page.php
error_reporting(E_ALL);
echo "<pre>\n";

// SHOW HOW TO DO FORM INPUT, VALIDATION, AND THANK YOU PAGE

// IF THERE IS SOMETHING THAT HAS BEEN POSTED
if (!empty($_POST))
{

    // THIS IS THE VALIDATION PROCESS - ONLY TWO RESULTS
    // EITHER A FAILURE PAGE OR A THANK YOU PAGE
    // CHECK TO SEE IF IT IS WHAT WE EXPECT
    if ($_POST["xyz"] != 'XYZ')
    {
        // CREATE ERROR PAGE IF DATA FAILED VALIDATION
        echo "\nSORRY YOU DID NOT ENTER XYZ";
    }
    else
    {
        // CREATE THANK YOU PAGE IF DATA PASSED VALIDATION
        echo "\nTHANK YOU, ";
        die("ALL DONE NOW");
    }

}// END OF PHP - PUT UP THE FORM
?>
<form method="post">
ENTER 'XYZ' IN UPPER CASE LETTERS HERE:
<input name="xyz" />
<input type="submit" />
</form>

Open in new window

0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
stevaAuthor Commented:
Dave,

Yes, it's looking like that's the only way to do it.

marcusG,

All I meant by "PHP action program" is the PHP program called by the form action attribute. Sorry for not saying that more clearly.  And again, as per your example, it looks like the only way to get a slightly different form the second time is to create it from whole cloth.

Ray,
New to PHP, not new to programming.  I have three PHP books I'm working from::

 "PHP & MySQL for Dummies"  by Valade,
 "PHP, MySQL & JavaScript" by Nixon, and
 "PHP and MySQL Web Development"  by Welling and Thomson.

 If I feel I need another book I'll look at the SitePoint book you suggested.  I don't think any of them, though,  would have told me how to access form elements from PHP because apparently it can't be done.  I thought there might be a way because I know you can do it from ASPX.

Thanks for the sample code.  I'll put that in my toolbox.  Your solution is to not repaint the form at all if the input is ok, and just say Thank You, but I think  I would prefer, in my case,  to show the form with the Thank You in the textarea when I'm done, so I'll go ahead and create a form2.html with the Thank You  and include it after sending the email.

0
 
Ray PaseurCommented:
If you're not new to programming and you have Welling, then I have a different suggestion.  Look up "PHP 5 in Practice" by Elliott White.  It's a great cookbook!
0
 
Bruce SmithSoftware Engineer IICommented:
Or you could use a session variable to trigger a thank you message. Once the form is submitted toggle a session variable from false to true. Add a conditional statement right below the form that 'echoes' "Thank You" if the session variable is true. If it's false, echo nothing. Make sure that the action of your form is the same page.

With session variables, you can also customize the "Thank You" message to the respected author of the comment. So if the comment author's name is 'Dave' you could tailor your "Thank You" message to "Thanks for your comments, Dave!"

I hope this helps some.

Cheers
0
 
stevaAuthor Commented:
Ray,
I just ordered PHP 5 in Practice.  Thanks!

patsmitty:

That sounds like a great idea.  Thanks!  Sorry all the question points are gone.
0
 
Ray PaseurCommented:
You'll love it.  I know Eli personally.  He was a core developer of Digg.  All the best, ~Ray
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now