4

I have a form that needs to post to another form (which is an outside form). I have the 1st form set up with the action to the url and method post. However, it is using JForm. It is working fine on the 1st form and saving to the database and opening up for editing. However, there is a second action via javascript that POSTs to another page with the new form.

After the page load, I inspect the POST and it shows:

Headers   Post    HTML   Cache   Cookies

Parts multipart/form-data

jform[id]   0
jform[state]    1
jform[created_by]   23
jform[grant_type]   A
jform[cycle]    
jform[salutation]   M
jform[first_name]   vrvervtervtgbver
jform[last_name]    rtrgver
jform[title]    rvert
jform[company_name] trvrvetrb
jform[email_address]    trbrtbeb@fgrewgrg.com

I can not for the life of me, grab this post data. I have tried everything I can think of using jinput and $_POST, but nothing seems to work. It either displays Array(0){[id]=>"" ... , or NULL, or just ARRAY. I can not use the Database, for this action as it needs come over via post.

Does anyone have any suggestions?

Here's my code as of right now:

<?php
/**
 * @version     1.2.0
 * @package     com_sfinnovate
 * @copyright   Copyright (C) 2014. All rights reserved.
 * @license     GNU General Public License version 2 or later; see LICENSE.txt
 * @author      Matt Dray <matt@sooyco.com> - http://www.aespire.com
 */
// no direct access
defined('_JEXEC') or die;

$doc =& JFactory::getDocument();
$doc->addStyleSheet('components/com_sfinnovate/innovatestyle.css');

JHtml::_('behavior.keepalive');
JHtml::_('behavior.tooltip');
JHtml::_('behavior.formvalidation');
JHtml::_('formbehavior.chosen', 'select');

//Load admin language file
$lang = JFactory::getLanguage();
$lang->load('com_sfinnovate', JPATH_ADMINISTRATOR);
$doc = JFactory::getDocument();
$doc->addScript(JUri::base() . '/components/com_sfinnovate/assets/js/form.js');

if($this->item->id) {
    $canState = JFactory::getUser()->authorise('core.edit.state','com_sfinnovate.innofundapp');
} else {
    $canState = JFactory::getUser()->authorise('core.edit.state','com_sfinnovate.innofundapp.'.$this->item->id);
}

$jinput = JFactory::getApplication()->input;
$fooValues = $jinput->get('jform', NULL, NULL);

var_dump($fooValues);
?>

<script type="text/javascript">
    getScript('//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js', function() {
        jQuery(document).ready(function() {
            jQuery('#form-innofundapp').submit(function(event) {

            });
        });
    });
</script>

<div class="innofundapp-edit front-end-edit">
    <?php if (!empty($this->item->id)): ?>
        <h1>Edit <?php echo $this->item->id; ?></h1>
    <?php else: ?>
        <h1>Add</h1>
    <?php endif; ?>
  • I think you need $jinput->get('jform', NULL, 'ARRAY');, but I'm not sure I'm following everything... – David Fritsch Aug 27 '14 at 15:22
  • No I tried it. it doesn't work. It seels like it should, but it's not. I am inspecting it in firebug and I can see the POST data. It's probably something really simple and i'm just skipping it over. I don't know what though. – user3597234 Aug 27 '14 at 17:30
  • So I'm not sure that I'm following the actual flow. The form gets submitted the standard way, and you can see the post data in firebug, but then when the form reloads you can't see the post data? – David Fritsch Aug 27 '14 at 17:34
  • I can see the post data in the reload as well. I recieve a POST then a GET and a GET. the post has all the data in it. but even when I var_dump ($_post) all I get is Array(). – user3597234 Aug 27 '14 at 17:38
  • I'm sorry, but I'm still not actually understanding where the issue is and what you are asking, I'll be in the chat if you want to go back and forth on this a bit. – David Fritsch Aug 27 '14 at 17:44
3

When you submit a form using the standard admin style of Joomla, this will create a POST request to the server. The server will then process the task of the form (which is usually 'save' or something similar) to save the form into the database.

At the end of the save, Joomla automatically does a redirect either back to the form view or the list view (depending on whether this was a "save and close" or "save" action). Once this redirect happens, you are no longer in a POST request, but now you are actually in a GET request.

You can check that this has happened by refreshing the resulting page. Did your browser ask you to resubmit form data? If no, then there was a redirect between your post and the response that you ultimately got from the server. Once that redirect happens, no data will be accessible by $_POST or any other method on the resulting page.


So, if you really need an exact copy of the $_POST in the resulting form, your best bet would be to push it into the session during the save process. Then pull from the session instead of from the post data (since that no longer exists).

In the save function, do this:

$session = JFactory::getSession();
$session->set('post-data', $_POST);

Then in your form, you can grab this like this:

$session = JFactory::getSession();
$post = $session->get('post-data');

// clear the data so we don't process it again
$session->clear('post-data');
  • Yea, It's throwing a 500 error with the $session = JFactory::getSesssion(); $session->set('post-data', $_POST); WHich seems odd. Don't know why i'd be getting that. . – user3597234 Aug 27 '14 at 18:44
  • Got It!!! Thanks! I used this but bypassed a created form in my component and just placedd it in a custom mod. Works thanks. – user3597234 Aug 27 '14 at 19:00
  • Awesome! I fixed the misspelling in getSession, which is what was likely causing the 500 error. Only two ss in a row... – David Fritsch Aug 27 '14 at 19:01
  • Brilliant answer David, showcasing the flow of data when submitting forms. +1 – FFrewin Aug 27 '14 at 19:06
1

Extending on david's answer. Following technique uses the method that was specifically designed for the problem at hand. The solution is to use JApplication::setUserState() and JApplication::getUserState() methods. Following code demonstrates the usage

//In the save function, do this:
JFactory::getApplication()->setUserState('post-data', $_POST, array());

 

//In the form , do this to retrieve the value:
JFactory::getApplication()->getUserState('post-data');

 

//do following to clear the value
JFactory::getApplication()->setUserState('post-data', null);

You can also use JApplication::getUserStateFromRequest(). This function prefers to load the value from request, and then saves it in user session as well. So next time you call this function without having data in request, it'll load the data from session.

https://docs.joomla.org/How_to_use_user_state_variables

I hope it helps someone.

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.