How to display and process forms in a Joomla article using DirectPHP?
Written by kksou   
Thursday, 30 June 2011

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="task" value="view">

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

Submitted Form Value

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

Please enter some values and click the submit button above.
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
  • task
  • 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="task" value="view">

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, task and lang), and it should work just like a standard PHP/HTML page.


 

Add comment


Security code
Refresh

< Prev   Next >

Blog - Forum - Privacy Policy - Contact Us
Links - Classes - Social Business - BPM - Web - General
Copyright © 2006-2013. kksou.com. All Rights Reserved