Go to main content

Textpattern CMS support forum

You are not logged in. Register | Login | Help

#1 2007-11-03 09:46:37

jstubbs
Member
From: Hong Kong
Registered: 2004-12-13
Posts: 2,395
Website

How to include a TXP tag in a PHP file?

I have an external PHP script that checks if an aMember user is logged in, and a current member. If the answer is yes, then the script displays a welcome message. If not, display a login message.

What I would like to do is output a TXP form within the PHP script – a form which outputs some secret details that only a current, logged in user should see.

But I don’t know how to include a TXP form like this: txp:output_form form=“secret_content” in the script.

Anyone know how? I get parse errors when including the tag now.

Offline

#2 2007-11-03 11:22:57

Mary
Sock Enthusiast
Registered: 2004-06-27
Posts: 6,236

Re: How to include a TXP tag in a PHP file?

If I were you, I would just fetch the form from the database.

SELECT Form FROM txp_form WHERE name = 'secret_content';

Offline

#3 2007-11-03 11:34:26

jstubbs
Member
From: Hong Kong
Registered: 2004-12-13
Posts: 2,395
Website

Re: How to include a TXP tag in a PHP file?

That’s very helpful Mary. May be a stupid question – but what’s the best way to output the contents of the form after selecting it? With built in TXP queries?

Offline

#4 2007-11-03 11:39:42

Mary
Sock Enthusiast
Registered: 2004-06-27
Posts: 6,236

Re: How to include a TXP tag in a PHP file?

That SQL would fetch the actual content, so just echo it.

Offline

#5 2007-11-03 12:15:16

jstubbs
Member
From: Hong Kong
Registered: 2004-12-13
Posts: 2,395
Website

Re: How to include a TXP tag in a PHP file?

Thanks. I have this now in the script:

$query = "SELECT Form FROM txp_form WHERE name = 'sitename'";
$result = mysql_query($query) or die("Couldn't execute query.");                 
echo $sitename; 

Which gives me an error in TXP with debugging set to on:

Tag error:  <txp:php> ->  Notice: Undefined variable: sitename  on line 16

Tried echo “$sitename”; and a few other ways, none worked. Would you mind telling me what I missed?

Offline

#6 2007-11-03 12:48:52

jstubbs
Member
From: Hong Kong
Registered: 2004-12-13
Posts: 2,395
Website

Re: How to include a TXP tag in a PHP file?

OK played around a bit more after realising that I was not connecting to the TXP database, which is why I was not getting any results ;-)

Could someone correct this SQL query for me – I used an old script and I am sure it could be improved. Not very important, but always nice to learn better practices….thanks for the help Mary!

$query = "SELECT Form FROM txp_form WHERE name = 'sitename'";
$result = mysql_query($query) or die ('Failed to execute ' . $query .'due to'. mysql_error());
list($sitename) = mysql_fetch_row($result);          
echo $sitename;

Offline

#7 2007-11-03 14:06:19

Mary
Sock Enthusiast
Registered: 2004-06-27
Posts: 6,236

Re: How to include a TXP tag in a PHP file?

(Assuming you have a form named “sitename” and aren’t trying to fetch the sitename as defined in the preferences – that’s in a different table…)

What needs correcting in the query? do you get an error or something?

Offline

#8 2007-11-03 20:33:03

jstubbs
Member
From: Hong Kong
Registered: 2004-12-13
Posts: 2,395
Website

Re: How to include a TXP tag in a PHP file?

Hi Mary,

I think I used the wrong word – by correcting I really meant “see if this could be better” – not because it does not work, only for the sake of purity. Its not really important, but its nice to learn better ways of doing things ;)

The form I am accessing is not called “sitename”, I just copied and pasted another form name. :-)

Offline

#9 2007-11-04 14:43:18

Mary
Sock Enthusiast
Registered: 2004-06-27
Posts: 6,236

Re: How to include a TXP tag in a PHP file?

Ah, ok.

In that case, I would switch this:

list($sitename) = mysql_fetch_row($result);

with this:

$sitename = mysql_result($result, 0);

One function call instead of two. :)

Offline

#10 2007-11-04 15:12:34

jstubbs
Member
From: Hong Kong
Registered: 2004-12-13
Posts: 2,395
Website

Re: How to include a TXP tag in a PHP file?

Thank you :-)

Offline

Board footer

Powered by FluxBB