Re: eval results to string?
| From: | Julian Wood | Date: | Wed, 11 Oct 2000 19:22:13 +0000 |
| Subject: | Re: eval results to string? | ||
| References: | 1 2 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-19682@lists.php.net to get a copy of this message | ||
Hi Dan,
Thanks for the input. I don't think I'm being clear in what I was attempting to
do. There's no problem generating a form with values in it _programmatically_ and
then mailing that. The point is, we want the system to be flexible, easy to use,
easy for the end user to read (ie we're mailing an html form with values filled
in) and able to accomodate any form of any length (they are typically 10 -20 kb
each!). We would also like to just be able to take someone's html form, mark it
up, and have the system work. We don't want to have to go and make another php
script to handle each form specifically. Creating the form programmatically (or
even just a list of keys and values like you do below), while it works, does not
give us these attributes.
I have now got this accomplished. All I have to do now is take any given form,
mark it up with standard php and they fit into the system. I don't have to worry
about escaping quotes or anything like that. There's only 3 possible lines of
code that are used over and over, for each of the possible html field types, so
even marking up is easy. For instance:
<input type="text" name="maxlength" size="5" value='<?php
echo $maxlength; ?>'>
<input type='radio' name='why' value='a'<?php echo
($why=='a') ? ' checked' : '';
?>>a. because
<option value='7'<?php echo ($top_left=='7') ? '
selected':''; ?>>7</option>
So now when I hit this file (get or post) with no accompanying get/post
variables, all the field values are blank or in their default state. When the
user submits, the script takes this very same form file and evaluates it in the
context of all those get/post variables (that were just submttted), and then
emails this result off. So now the marker gets a nice html email with the form
intact as it was originally designed, with all the values filled in. Does that
make more sense?
BTW, the key to getting this to work was being able to parse the form file into a
variable. Neither eval, require, nor include can do this. While the file
functions (fread, etc) can, they can't parse the php. The solution was to re-post
the submitted user info to that form, and take the result back into a variable,
which could then be emailed. All thanks to Allan van den Bosch's excellent
http_post class. All told, about 10 lines of code (plus the post class) to
process any form. The double post is maybe not as nice as an eval would have
been, but it keeps this system very clean.
Thanks,
Julian
Daniel Convissor wrote:
> Hi Julian:
>
> > Well, I looked into include or require, but neither can get me to a point
> > where I can take the resultant data and send it off in an email message
> > (that I can see). I'm still stuck with trying to get a parsed php file into
> > a variable that I can pass through to the mail command.
>
> Merge the various variables together into one variable, then pass that to the
> mail function. For example:
>
> $Body = "Name: $Name\n";
> $Body .= "Address: $Address\n";
> $Body .= "Phone: $Phone\n";
>
> mail("$Email", "New Submission", "$Body");
>
> Similarly, you can put the $Body = ... into a while list/each loop on
> HTTP_POST_VARS, rather than manually writing them out in your code like this:
>
> $Body = "";
> while ( list($Key,$Val) = each($HTTP_POST_VARS) ) {
> $Body .= "$Key: $Val\n";
> }
>
> > How do you mail a file that you include?
>
> That's not what you want to do in this case, though that is doable using
> fread() or fgets() to place the contents into a string variable then placing
> that string variable into the mail() function.
>
> Enjoy,
>
> --Dan
>
--
Julian Wood
Advanced Media for Learning
University of Calgary