How to use classes and function in external pages ?
Archived from the Xataface Users forum.
pyroboynroses — Tue Feb 09, 2010 1:40 pm
Hello,
I’m setting a paypal button payment system on my xataface website and I need help to insert the id of the logged in user.
The button file is named pack15.php and it contains this code :
- Code: Select all
- `<HTML>
<HEAD>
Test
</HEAD>
<BODY>
<H1>Pack de 15 SMS</H1>
<form target="paypal" action="https://www.paypal.com/cgi-bin/webscr" method="post">
<input type=”hidden” name=”item_name” <?php
mysql_connect(localhost, ‘root’,’’) or die(“Erreur de connexion au serveur”);
mysql_select_db(‘lepensebete’) or die(“Erreur de connexion à la base de donnees”);
$query = “SELECT * FROM t_pack WHERE nom_pack = ‘Pack de 15 SMS’”;
$result = mysql_query($query);
$row = mysql_fetch_row($result);
echo ‘value=”’.$row[1].’”’;
mysql_close();
?»<input type=”hidden” name=”amount” <?php
mysql_connect(localhost, ‘root’,’’) or die(“Erreur de connexion au serveur”);
mysql_select_db(‘lepensebete’) or die(“Erreur de connexion à la base de donnees”);
$query = “SELECT * FROM t_pack WHERE nom_pack = ‘Pack de 15 SMS’”;
$result = mysql_query($query);
$row = mysql_fetch_row($result);
echo ‘value=”’.$row[4].’”’;
mysql_close();
?»<input type=”hidden” name=”custom” <?php
class test {
function test() {
$auth =& Dataface_AuthenticationTool::getInstance();
$user =& $auth->getLoggedInUser();
$id = $user->val(‘id’);
echo ‘value=”’.$id.’”’;
}
}
?»

</form>
</BODY>
</HTML>`
So I can in this file put database data in parameters of the form with a mysql query but I can’t get the id of the logged in user with my class :
- Code: Select all
<?php class test { function test() { $auth =& Dataface_AuthenticationTool::getInstance(); $user =& $auth->getLoggedInUser(); $id = $user->val('id'); echo 'value="'.$id.'"'; } } ?>>
I haven’t got errors but the field is empty…
How can i do that ?
Thank you for the help
pyroboynroses — Wed Feb 10, 2010 7:51 am
So I try several things.
I inspired from the index.php file needed in a xataface website.
So I tried that in my external page :
- Code: Select all
- `<?php
require_once ‘../xataface/dataface-public-api.php’;
df_init(FILE, ‘http://localhost/mysite/xataface’);$auth =& Dataface_AuthenticationTool::getInstance();
$user =& $auth->getLoggeInUser();
$id = $user->val(‘id’);
?>`
But this code doesn’t work. I have this error :
Fatal error: Class ‘Dataface_AuthenticationTool’ not found in …
So how can I fix that ???
Thanks for your help.
pyroboynroses — Wed Feb 10, 2010 8:09 am
Ok, I tried an echo command on the $user variable and it is empty !!!
Why ???
shannah — Wed Feb 10, 2010 9:34 am
Is there any reason why you want to do this as a separate page and not as a custom action?
pyroboynroses — Wed Feb 10, 2010 9:39 am
I would like to do that because it’s he code of a paypal button called by a link in a xataface record.
But if there is a way to do that in a custom action, I would be happy to know how I can do that.
Thanks for the answer.
shannah — Wed Feb 10, 2010 9:46 am
Simple custom action:
file: actions/hello.php
- Code: Select all
<?php class actions_hello { function handle($params){ echo "Hello world"; } }
Call your action with:
index.php?-action=hello
Custom Action using template:
file: actions/hello.php:
- Code: Select all
<?php class actions_hello { function handle($params){ df_display(array('name'=>'Steve'), 'hello.html'); } }
file: templates/hello.html:
- Code: Select all
- `<html><head>
Hello {$name} </head>Hello {$name}
`
Still access your action via index.php?-action=hello
Custom action using template that inherits from Xataface’s Main Template:
Everything same as previous example except templates/hello.html now looks like:
- Code: Select all
- `{use_macro file=”Dataface_Main_Template.html”}
{fill_slot name=”main_column”}Hello {$name}
{/fill_slot}
{/use_macro}`
pyroboynroses — Wed Feb 10, 2010 11:40 pm
Thanks !!
I’vz tried it and it works perfectly.
I will post the whole code for the paypal button soon and how it works with xataface.
pyroboynroses — Sat Feb 13, 2010 6:11 am
So this is the code in my record to have a button which point to the paypal.php action :
- Code: Select all
<form method="post" action="http://127.0.0.1/lepensebete/xatagestion/index.php?-action=paypal" > <input type="hidden" name="pack_name" value="Pack de 15 SMS"> <input type="submit" value="Acheter avec Paypal"> </form>
this button send with a post method the pack name to the paypal action (to find the correspondant record in the paypal action).
And this is the code for the paypal action (paypal.php in the actions directory) :
- Code: Select all
- `<HTML>
<HEAD>
Test
</HEAD>
<BODY>
<H1>Ajouter un pack de SMS au panier</H1>
<?php
class actions_paypal {
function handle($params){$auth =& Dataface_AuthenticationTool::getInstance();
$user =& $auth->getLoggedInUser();
$id = $user->val(‘id’);
$pack_name = $_POST[“pack_name”];mysql_connect(localhost, ‘root’,’’) or die(“Erreur de connexion au serveur”);
mysql_select_db(‘lepensebete’) or die(“Erreur de connexion à la base de donnees”);
$query = “SELECT * FROM t_pack WHERE nom_pack = ‘”.$pack_name.”’”;
$result = mysql_query($query);$row = mysql_fetch_row($result);
mysql_close();$item_name = $row[1];
$nb_sms = $row[2];
$prix_unit = $row[3];
$amount = $row[4];echo ‘
Récapitulatif du pack sélectionnéNom du pack Nombre de SMS Prix TTC unit SMS Prix TTC du Pack id utilisateur '.$item_name.' '.$nb_sms.' '.$prix_unit.' € '.$amount.' € '.$id.'
En cliquant sur "Ajouter au panier", vous aller être redirigé sur le site de paiement Paypal.
’;
}
}
?>
</BODY>
</HTML>`
The paypal button is in fact an html form with hidden fields.
If you have any question, I could answer
shannah — Tue Feb 16, 2010 1:13 pm
Thanks for posting the code. Glad to hear it works. One suggestion as a matter of style and convention is that delegate classes shouldn’t really be embedded in HTML like this.
Better to place the header and footer stuff either inside an echo statement inside your handle() method, or use a template (best solution).
-Steve