Writing PHP code in templates

For questions and problems with the CMS core. This board is NOT for any 3rd party modules, addons, PHP scripts or anything NOT distributed with the CMS made simple package itself.
Post Reply
rfuste
Forum Members
Forum Members
Posts: 50
Joined: Wed Jun 07, 2006 7:56 pm

Writing PHP code in templates

Post by rfuste »

hello everyone!

i'm certainly not a php guru, but i have a couple of sample php code snippets i've used successfully in an old site contact form, where it would display the form, and once you submit it, it would run through the same file again and decide whether it should display a thank you message or an "error: missing information.. please try again" message.

anyway, i'm trying to figure out how to write php within a template.  if i simply write the code in, the page won't display at all.  i can only guess that the php code contains some { and } characters, which seem to be used by the cms templating language.  if i "neutralize" those { } characters by wrapping my php code with {literal} and {/literal} tags, everything actually displays, instead of working in the background like it's supposed.

is there a specific way that i need to write the php code so as to integrate properly?

thanks for any help!!
calguy1000
Support Guru
Support Guru
Posts: 8169
Joined: Tue Oct 19, 2004 6:44 pm

Re: Writing PHP code in templates

Post by calguy1000 »

It's in the documentation under "Developers FAQ"
Follow me on twitter
Please post system information from "Extensions >> System Information" (there is a bbcode option) on all posts asking for assistance.
--------------------
If you can't bother explaining your problem well, you shouldn't expect much in the way of assistance.
rfuste
Forum Members
Forum Members
Posts: 50
Joined: Wed Jun 07, 2006 7:56 pm

Re: Writing PHP code in templates

Post by rfuste »

thank you, calguy1000 - i apologize for not having found it sooner in the obvious place.

i tried creating my own user-defined tag, but it's not working - it gives me an error message of "Invalid Code entered."  I took off the tags, but it's still giving me the error message.

i'm pasting the code below to see if your quick eyes might see an obvious problem that would be causing the error.  this is only part of the overall php code to be used through the page:
________________________
$dothanks = false;
function ValidEmail($email) {
    if (eregi("^[a-z0-9]+([-_\.]?[a-z0-9])+@[a-z0-9]+([-_\.]?[a-z0-9])+\.[a-z]{2,4}", $email)){
        return TRUE;
    } else {
        return FALSE;
    }
}
// if submitted form process and send mail
if ($REQUEST_METHOD == "POST") {
$validForm = false;
if (ValidEmail($email) && $name) { $validForm=true; }
if ($validForm) {
    // just to be on the safe side
    // strip out HTML tags
    // (scripting code may mess with some email clients)
$name = strip_tags($name);
$address1 = strip_tags($address1);
$address2 = strip_tags($address2);
$city = strip_tags($city);
$state = strip_tags($state);
$zip = strip_tags($zip);
$phone = strip_tags($phone);
$email = strip_tags($email);
$found = strip_tags($found);
    $interest = strip_tags($interest);
$director = strip_tags($director);
$comments = strip_tags($comments);

    $to_realname = "Still Waters";
// use info@comebestill.org & webmaster@comebestill.org
    $to_email = "info@comebestill.org";
$from_realname = $name;
    $from_email = $email;

    $subject = "Comments/Questions Submitted to ComeBeStill.org";
    $message = '
Here is a question or feedback from someone visiting the Still Waters website.





Name
Address 1
Address 2
City
State
Zip

Phone
E-mail

How did you find our website?
What services are you interested in?
If interested in spiritual direction, which director is preferred?
Comments/Questions

' .
$name . '' .
$address1 . '' .
$address2 . '' .
$city . '' .
$state . '' .
$zip . '' .
$phone . '' .
$email . '' .
$found . '' .
$interest . '' .
$director . '' .
$comments . '' .
'


';

mail($to_realname . "", $subject,    $message,    "From: " . $from_realname . "\n" .    "MIME-Version: 1.0\n" .    "Content-type: text/html; charset=iso-8859-1");


    $replyMessage = 'Dear ' . $name . ',

Thank you for your recent inquiry and feeback on Still Waters.
We appreciate your input, and we hope to respond to you soon if requested in your form message.

Blessings,

          Still Waters Staff
          (269) 695-REST (7378)
          www.ComeBeStill.org


';

mail($from_realname . "", $subject,    $replyMessage,    "From: " . $to_realname . "\n");

$dothanks = true;
}
else {
$error = '

Incomplete information.Please make sure all required fields are completed and try again.';
}
}
if ($dothanks) }

______________________

thanks!
rob
ludo2k
Forum Members
Forum Members
Posts: 10
Joined: Sat Aug 12, 2006 10:43 pm

Re: Writing PHP code in templates

Post by ludo2k »

Hi,

i had the same prob yesterday on a smaller code, and when i had deleted // comments it worked!

maybe you can try this?
rfuste
Forum Members
Forum Members
Posts: 50
Joined: Wed Jun 07, 2006 7:56 pm

Re: Writing PHP code in templates

Post by rfuste »

Thanks, ludo2k, for replying.  I took out the comment tags ( // ), while still keeping out the tags, and it still gives me same error message:  Invalid code entered.

Any ideas?

Thanks!
calguy1000
Support Guru
Support Guru
Posts: 8169
Joined: Tue Oct 19, 2004 6:44 pm

Re: Writing PHP code in templates

Post by calguy1000 »

just a thought
but try commeneting out everything but the function, so that the UDT doesn't do anything.... and see if you still get the error.

if you don't..... then a simple binary search for the error would have to be done.  i.e:  comment out only 1/2 the code, see if you get the error.  if not, comment out the other 1/2...  and then subdevide till you get the error.

it's a bit more complex than that with code most times, but the principle is the same.
Follow me on twitter
Please post system information from "Extensions >> System Information" (there is a bbcode option) on all posts asking for assistance.
--------------------
If you can't bother explaining your problem well, you shouldn't expect much in the way of assistance.
Post Reply

Return to “CMSMS Core”