How to display and process forms in a Joomla article using DirectPHP?

With DirectPHP, you can easily display forms in a Joomla article by using PHP's echo() function to display the HTML form.

However, many people have asked in the forum how to process this HTML form within the Joomla article?


Displaying the Form

Below is a sample form displayed using DirectPHP:

var1:
var2:

The code above is displayed using the following PHP statements:

<?php
echo '<form action="index.php" method="POST">
var1: <input name="var1"><br />
var2: <input name="var2"><br />

<input type="hidden" name="option" value="com_content">
<input type="hidden" name="Itemid" value="1">
<input type="hidden" name="id" value="2014">
<input type="hidden" name="view" value="article">

<input type="submit" value="Submit">
</form>';
?>

Submitted Form Value

Try enter some string into var1 and var2 above and then click the submit button:

Submitted value of var1:
Submitted value of var2:

The code to displayed the submitted values are as follows:

echo "Submitted value of var1: ".$_POST['var1']."<br />";
echo "Submitted value of var2: ".$_POST['var2']."<br />";

Explanation

Please refer to the article: How to pass variables or parameters from one Joomla article to another using DirectPHP?

As explained in the article, you need to pass in the following parameters in order to come back to the same Joomla article:

  • option
  • Itemid
  • id
  • view
  • lang (if you are using languages other than English)

When using a form, we pass along these parameters using the <INPUT TYPE="HIDDEN"> statements.

<input type="hidden" name="option" value="com_content">
<input type="hidden" name="Itemid" value="1">
<input type="hidden" name="id" value="2014">
<input type="hidden" name="view" value="article">

Note that we are using the POST method here. So we use $_POST to retrieve the parameters.

echo "Submitted value of var1: ".$_POST['var1']."<br />";
echo "Submitted value of var2: ".$_POST['var2']."<br />";

If you are using the GET method, you will use $_GET to retrieve the paratemeters.

echo "Submitted value of var1: ".$_GET['var1']."<br />";
echo "Submitted value of var2: ".$_GET['var2']."<br />";

Conclusion

Joomla is written in PHP. So it's easy to incorporate HTML forms into a Joomla article through the use of DirectPHP.

Just make sure you pass along the additional Joomla parameters (i.e. option, Itemid, id, view and lang), and it should work just like a standard PHP/HTML page.

Comments   

0 # Stathes 2015-12-10 12:09
Hello, I am trying to pass post variables, but when i save the article i got this error
Not Implemented

GET to /~trailpro/administrator/index.php not supported.
Additionally, a 501 Not Implemented error was encountered while trying to use an ErrorDocument to handle the request.

and cannot procced
Thanx
Reply | Reply with quote | Quote
+2 # kksou 2015-12-18 10:44
Hi Stathes,

Sorry for the late reply.

DirectPHP uses the standard PHP eval() function. Please change "/~trailpro" to fullpath. If you have coded using raw PHP, PHP does not know ~. So you have to use the fullpath e.g. /var/www/... or /home/domain/...

Please give it a try and see if it works.

Warm Regards,
/kksou
Reply | Reply with quote | Quote
+1 # Brown Nwankwo 2016-11-08 14:59
Hello. Cannot find item id. Where can I find it please
Reply | Reply with quote | Quote
0 # kksou 2016-11-09 04:21
Hi Brown,

The id is the article id.

If you go to your joomla backend - Content - Articles. The ID you see in the rightmost column is the article id.

In Joomla, to go to a particular Joomla article, you will need to specify these 4 parameters - option, itemid, id (which is the article id) and view.

Warm Regards,
/kksou
Reply | Reply with quote | Quote

Add comment


Security code
Refresh