0

I'm new in joomla, I created a module for learning purpose, let name it mod_form1, I don't know what framework or class in joomla i should use for this so I use plain HTML. this is my tmpl/default.php code

<?php 
// No direct access
defined('_JEXEC') or die; ?>
<?php echo '<br><b>'.$hello.'</b>';

//action file resides in "D:\XamppInstalation\htdocs\joomla\modules\mod_form1\tmpl"
//joomla component access = <yoursite>/joomla/index.php?option=com_<component_name>

$actionpath = '/'.basename(getcwd()).'/modules/mod_form1/tmpl/myaction.php';
echo '<br>actionpath - '.$actionpath;
echo '<br>basename() = '.basename(getcwd());
//echo '<br>current url = '. $_SERVER['REQUEST_URI'];
$currentURL = $_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
echo '<br>current url = '.$currentURL;
?>
<form name="indexForm1" method="post" action="<?php echo $actionpath ?> enctype="text/plain" accept-charset="UTF-8" target="_self" id="indexForm1">
<br>Your name <input type="text" id="indexEditbox1" name="name" value="<?php echo $actionpath ?>" spellcheck="false">
<br>Your Age<input type="text" id="indexEditbox2" name="age" value="" spellcheck="false">
<br><input type="submit" id="indexButton1" name="" value="Submit"> 
<br>using regular way to submit directly (form above) to bypass JOOMLA doesn't work, 
<br>so use javascript instead to submit form directly to ACTION php file like form bellow.
</form>
<br><a href="<?php echo $actionpath ?>"><?php echo $actionpath ?></a>

<p>Enter some text in the fields below, then press the "Submit form" button to submit the form.</p>

<form id="myForm" method="post" action="<?php echo $actionpath ?>">
  First name: <input type="text" name="fname"><br>
  Last name: <input type="text" name="lname"><br><br>
  <input type="hidden" name = "backurl" value="<?php echo $currentURL ?>">
  <input type="button" onclick="myFunction()" value="Submit form">
</form>

<script>
function myFunction() {
  document.getElementById("myForm").submit();
}
</script>

Script above use myaction.php to take action to handle Form, button SUBMIT seem doesn't work so I use javascript to do SUBMIT. this is my 'tmpl/myaction.php`

<?php
echo '<br>form submitted';
echo '<br>MOD_FORM1 handled successfully';
$requestURL = $_POST['backurl'];
echo '<br>backURL = '.$requestURL;

?>
<script>

  //setTimeout(function(){ window.location ="localhost"); }, 3000);
  //setTimeout(function(){ alert("Hello"); }, 3000);
  setTimeout(function(){ window.location = "http://<?php echo $requestURL ?>"; }, 3000);
  //window.location = "https://www.example.com";

</script>

This module works as expected but I think it is not Good Practice because myaction.php work outside Joomla environment. can anyone please show me the right way to create something similiar to my lame module above ?, what framework I should read ?

this is my entire module

0

Agree with @sharkey send your request via ajax the link above also has a link to a Demo module

You may also want to take a look at the below which shows how to use the JResponseJson which will be useful in returning a response and has some good examples that should help https://docs.joomla.org/JSON_Responses_with_JResponseJson

  • Rather than just referring people to the documentation, it is always more generous/helpful to demonstrate how to implement the necessary technique. If there is insufficient detail to provide this level of insight, please ask for greater detail from the OP. This means more effort from the asker and volunteers, but once a direct, reproducible script is on offer, future researchers will be very likely to benefit and upvote. – mickmackusa Sep 30 '20 at 10:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.