By: andrew
Date: 2010-09-21
Time: 14:50
|
Joomla plugin handling form posts
Can anyone please help.
I am trying to understand how to get a html form template placed in a joomla article to select data from a database.
I have copied the 'Enter data in a form' example into my joomla site in order to work out how it all works, but I can not get it working correctly.
I get errors about the required global variables not being set. Following much reading of the php manual, I can not work out how to get the [onshow.x_type;ope=html;select] working.
I have also noticed that the html template form action is replacing <form action="[onshow..script_name]" with <form action="index.php" and not the script merging the template.
Can anyone explain all this in basic terms and help me get it working?
The output in the joomla article:
<table class="nopad">
<tr valign="top">
<td>
<br /><b>TinyButStrong Error</b> in field [onshow.x_type...] : the PHP global variable named 'x_type' does not exist or is not set yet. <em>This message can be cancelled using parameter 'noerr'.</em><br />
<br /><b>TinyButStrong Error</b> in field [onshow.x_name...] : the PHP global variable named 'x_name' does not exist or is not set yet. <em>This message can be cancelled using parameter 'noerr'.</em><br />
<br /><b>TinyButStrong Error</b> in field [onshow.x_subname...] : the PHP global variable named 'x_subname' does not exist or is not set yet. <em>This message can be cancelled using parameter 'noerr'.</em><br />
<br /><b>TinyButStrong Error</b> in field [onshow.msg_color...] : the PHP global variable named 'msg_color' does not exist or is not set yet. <em>This message can be cancelled using parameter 'noerr'.</em><br />
<br /><b>TinyButStrong Error</b> in field [onshow.msg_text...] : the PHP global variable named 'msg_text' does not exist or is not set yet. <em>This message can be cancelled using parameter 'noerr'.</em><br />
<br /><b>TinyButStrong Error</b> in field [onshow.x_type...] : the PHP global variable named 'x_type' does not exist or is not set yet. <em>This message can be cancelled using parameter 'noerr'.</em><br />
<br /><b>TinyButStrong Error</b> in field [onshow.x_name...] : the PHP global variable named 'x_name' does not exist or is not set yet. <em>This message can be cancelled using parameter 'noerr'.</em><br />
<br /><b>TinyButStrong Error</b> in field [onshow.x_subname...] : the PHP global variable named 'x_subname' does not exist or is not set yet. <em>This message can be cancelled using parameter 'noerr'.</em><br />
<br /><b>TinyButStrong Error</b> in field [onshow.msg_color...] : the PHP global variable named 'msg_color' does not exist or is not set yet. <em>This message can be cancelled using parameter 'noerr'.</em><br />
<br /><b>TinyButStrong Error</b> in field [onshow.msg_text...] : the PHP global variable named 'msg_text' does not exist or is not set yet. <em>This message can be cancelled using parameter 'noerr'.</em><br />
<table class="contentpaneopen">
<tr>
<td class="contentheading" width="100%">
Data Entry </td>
<td align="right" width="100%" class="buttonheading">
<a href="/pt/index.php?view=article&id=48%3Adata-entry&format=pdf&option=com_content&Itemid=57" title="PDF" onclick="window.open(this.href,'win2','status=no,toolbar=no,scrollbars=yes,titlebar=no,menubar=no,resizable=yes,width=640,height=480,directories=no,location=no'); return false;" rel="nofollow"><img src="/pt/images/M_images/pdf_button.png" alt="PDF" /></a> </td>
<td align="right" width="100%" class="buttonheading">
<a href="/pt/index.php?view=article&id=48%3Adata-entry&tmpl=component&print=1&layout=default&page=&option=com_content&Itemid=57" title="Print" onclick="window.open(this.href,'win2','status=no,toolbar=no,scrollbars=yes,titlebar=no,menubar=no,resizable=yes,width=640,height=480,directories=no,location=no'); return false;" rel="nofollow"><img src="/pt/images/M_images/printButton.png" alt="Print" /></a> </td>
<td align="right" width="100%" class="buttonheading">
<a href="/pt/index.php?option=com_mailto&tmpl=component&link=aHR0cDovL3d3dy5hbmRyZXdzY3VsbGlvbi5jb20vcHQvaW5kZXgucGhwP29wdGlvbj1jb21fY29udGVudCZ2aWV3PWFydGljbGUmaWQ9NDg6ZGF0YS1lbnRyeSZJdGVtaWQ9NTc=" title="E-mail" onclick="window.open(this.href,'win2','width=400,height=350,menubar=yes,resizable=yes'); return false;"><img src="/pt/images/M_images/emailButton.png" alt="E-mail" /></a> </td>
</tr>
</table>
<table class="contentpaneopen">
<tr>
<td valign="top">
<span class="small">
Written by Administrator </span>
</td>
</tr>
<tr>
<td valign="top" class="createdate">
Monday, 13 September 2010 08:16 </td>
</tr>
<tr>
<td valign="top">
<form action="index.php" method="post" name="frm_info" id="frm_info">
<table width="300" border="0" align="center" cellpadding="0" cellspacing="0">
<tr>
<td width="296"><table width="100%" border="1" cellpadding="0" cellspacing="0">
<tr>
<td height="20" bgcolor="#BBCCED">Enter information:</td>
</tr>
<tr>
<td><table width="100%" border="0" cellspacing="0" cellpadding="2">
<tr align="left" valign="bottom" class="back-special2">
<td width="76">Gender : </td>
<td width="212"><select name="x_type" class="normal" id="select2">
<option value="-"><other></option><option value="Mr">Mister</option><option value="Mme">Madame</option><option value="Ms">Missis</option>
<option>[onshow.x_type;ope=html;select]</option>
</select>
</td>
</tr>
<tr align="left" valign="bottom" class="back-special2">
<td>Last name: </td>
<td><input name="x_name" type="text" class="normal" id="x_name2" value="[onshow.x_name]">
</td>
</tr>
<tr align="left" valign="bottom" class="back-special2">
<td>First name:</td>
<td><input name="x_subname" type="text" class="normal" id="x_subname2" value="[onshow.x_subname]">
</td>
</tr>
</table></td>
</tr>
<tr>
<td height="30" bgcolor="#BBCCED"><div align="center">
<input name="btn_save" type="submit" class="normal" id="btn_save2" value=" Validate ">
</div></td>
</tr>
</table></td>
</tr>
</table>
<div align="center"><br>
<strong><font color="[onshow.msg_color]">[onshow.msg_text]</font></strong> </div>
</form></td>
</tr>
<tr>
<td class="modifydate">
Last Updated on Monday, 13 September 2010 13:02 </td>
</tr>
</table>
|
Below is the php script file (nothing changed except the path to the include files and template file and also the removal of line 6 '$TBS = new clsTinyButStrong;' as per instructions in a previous post for joomla plugin):
<?php
include_once('/home/path/plugins/content/tinybutstrong_class_php5.php');
include_once('/home/path/plugins/content/tbs_plugin_html.php'); // Plug-in for selecting HTML items.
$TBS->LoadTemplate('/home/path/plugins/tbs_htm_templates/test_form.html');
$typelist = array('<other>'=>'-','Mister'=>'Mr','Madame'=>'Mme','Missis'=>'Ms') ;
$TBS->MergeBlock('typeblk',$typelist) ;
if (!isset($_POST)) $_POST=&$HTTP_POST_VARS;
if (!isset($_POST['x_type'])) {
$x_type = '-' ;
$x_name = '' ;
$x_subname = '' ;
$msg_text = 'Enter your information and click on [Validate].' ;
$msg_color = '#0099CC' ; //blue
} else {
$msg_text = '';
$msg_body = array();
$x_type = $_POST['x_type'];
$x_name = $_POST['x_name'];
$x_subname = $_POST['x_subname'] ;
if (trim($x_type)=='-') $msg_body[] = ' your gender' ;
if (trim($x_name)=='') $msg_body[] = ' your last name' ;
if (trim($x_subname)=='') $msg_body[] = ' your first name' ;
if (count($msg_body)==0) {
$msg_text = 'Thank you.' ;
$msg_color = '#336600' ; //green
} else {
$msg_text = 'Please enter' . join($msg_body, ' and');
$msg_color = '#990000' ; //red
}
}
$TBS->Show();
?> |
Below is the template file:
<form action="[onshow..script_name]" method="post" name="frm_info" id="frm_info">
<table width="300" border="0" align="center" cellpadding="0" cellspacing="0">
<tr>
<td width="296"><table width="100%" border="1" cellpadding="0" cellspacing="0">
<tr>
<td height="20" bgcolor="#BBCCED">Enter information:</td>
</tr>
<tr>
<td><table width="100%" border="0" cellspacing="0" cellpadding="2">
<tr align="left" valign="bottom" class="back-special2">
<td width="76">Gender : </td>
<td width="212"><select name="x_type" class="normal" id="select2">
<option value="[typeblk.val]">[typeblk.key;block=option]</option>
<option>[onshow.x_type;ope=html;select]</option>
</select>
</td>
</tr>
<tr align="left" valign="bottom" class="back-special2">
<td>Last name: </td>
<td><input name="x_name" type="text" class="normal" id="x_name2" value="[onshow.x_name]">
</td>
</tr>
<tr align="left" valign="bottom" class="back-special2">
<td>First name:</td>
<td><input name="x_subname" type="text" class="normal" id="x_subname2" value="[onshow.x_subname]">
</td>
</tr>
</table></td>
</tr>
<tr>
<td height="30" bgcolor="#BBCCED"><div align="center">
<input name="btn_save" type="submit" class="normal" id="btn_save2" value=" Validate ">
</div></td>
</tr>
</table></td>
</tr>
</table>
<div align="center"><br>
<strong><font color="[onshow.msg_color]">[onshow.msg_text]</font></strong> </div>
</form> |
|
By: Skrol29
Date: 2010-09-21
Time: 22:08
|
Re: Joomla plugin handling form posts
Hi Andrew,
In order to avoid the TBS error messages about global variables, just add the following line just before you call on of the variables, (i.e. just before "if (!isset($_POST)) $_POST=&$HTTP_POST_VARS;))"
global $x_name, $x_subname, $msg_color, $msg_text;
|
Note that the line:
include_once('/home/path/plugins/content/tinybutstrong_class_php5.php');
|
is superfluous since the TBS library is already include with the plugin. You'd better take it off.
The field [onshow..script_name] will display the current script, which is actually "script.php" because this is the main Joomla script, you can see it in the URL.
This script is calling the plug-in, and the plug-in is calling your subscript.
But I think you need the form to send information to the same Joomla page, not directly your subscript. Because your subscript cannot run standalone.
The PHP global variable $_SERVER['REQUEST_URI'] contains the URL including the Joomla parameters for displaying the current article, so you can display
this URL in the action attribute using the field: [onshow._SERVER.REQUEST_URI]
This is not tested but it should work.
Enjoy,
|