Where to put a simple callable php script in Jooml

2019-08-06 23:33发布

问题:

I have some Joomla HTML content which contains a simple HTML form which results should available to a simple PHP script.

Basically the HTML form looks like:

<form action="action.php" method="post">

The question is where can/should I put my action.php script in the Joomla project directory to be

  1. callable for my form action and
  2. not overwritten by Joomla updates

回答1:

I would recommend creating a component for forms. You probably need to handle the input in your form, and do some action based on what is in the form, and return the form if there are errors, etc.

Simplest way to make a component:

  • add a folder in components: /components/com_whatever
  • Add php-file: whatever.php in folder com_whatever
  • Add xml-file: whatever.xml

Content of whatever.xml:

<?xml version="1.0" encoding="utf-8"?>
<extension type="component" version="3.2.0" method="upgrade">
    <name>Whatever</name>
    <version>1.0.0.0</version>
    <files folder="site">
        <filename>whatever.php</filename>
    </files>
    <administration>
    </administration>
</extension>

Content of whatever.php:

<?php
defined('_JEXEC') or die;
echo "HELLOOOO";
// or rather some code handling your form data

Install the component by going to yoursite/administrator/index.php?option=com_installer&view=discover

Now you can reach whatever.php from the form, using:

<form action="index.php?option=com_whatever" method="post">

There is lots of stuff you can and probably should do in your component, but this is the bare minimum.

PS: You could install a form component, there are lots. Alternatively you could use a component-builder to create your form, then you also get admin-tools to handle the incoming data.



回答2:

For that you have to create joomla content plugins, using content plugin you can handle form submit action.

For Reference: https://docs.joomla.org/J3.x:Creating_a_content_plugin