0

I am trying to access PHP's $_POST array from a template, using PHP within the template and have the expiration_date of the entry updated. The $_POST is empty, although the form is submitting to the same URL.

This is the EE template:

 {exp:channel:form channel="auction" return="auction/new"}  
    <input name="title" type="text" />
    <input name="auction_description" type="text" />
    <input name="auction_price" type="text" />
    <input type="submit" value="Submit" />
 {/exp:channel:form}

This is the HTML of the Form:

<form id="publishForm" method="post" action="http://123.123.123.123/auction/new"  enctype="multipart/form-data" >
<input type="hidden" name="RET" value="http://123.123.123.123/auction/new" />
<input type="hidden" name="URI" value="auction/new" />
<input type="hidden" name="return_url" value="auction/new" />
<input type="hidden" name="author_id" value="1" />
<input type="hidden" name="channel_id" value="1" />
<input type="hidden" name="entry_id" value="0" />
<input type="hidden" name="ACT" value="8" />
<input type="hidden" name="return" value="auction/new" />
<input type="hidden" name="site_id" value="1" />
<input name="title" type="text" />
<input name="auction_description" type="text" />
<input name="auction_price" type="text" />
<input type="submit" value="Submit" />

This is the PHP code:

<?php
if (!empty($_POST['title'])) {
ee()->db->update(
    'exp_channel_titles',
    array(
        'expiration_date'  => '123'
    ),
    array(
        'entry_date' => $_POST['entry_date']
    )
);
}
?>

.htacess:

<IfModule mod_rewrite.c>
        RewriteEngine On
        RewriteBase /

        # Removes index.php from ExpressionEngine URLs
        RewriteCond %{THE_REQUEST} ^GET.*index\.php [NC]
        RewriteCond %{REQUEST_URI} !/system/.* [NC]
        RewriteRule (.*?)index\.php/*(.*) /$1$2 [R=301,NE,L]

        # Directs all EE web requests through the site index file
        RewriteCond %{REQUEST_FILENAME} !-f
        RewriteCond %{REQUEST_FILENAME} !-d
        RewriteRule ^(.*)$ /index.php/$1 [L]
</IfModule>

The PHP parse stage is set to Input and the template's name is the same as the channel's action.

Any help would be highly appreciated. I've been struggling with this for almost a week now.

2
  • I'm not seeing a CSRF token in your outputted form. EE by default requires a CSRF token for form submittal. Hrmm..
    – jrothafer
    Feb 8, 2016 at 23:48
  • I removed the CSRF token field and the meta field from the post for better view, but they are both outputted.
    – gonn
    Feb 9, 2016 at 7:00

1 Answer 1

0

It may be posting to the same URL, that is true - but when you end up back at that URL it's actually the result of a post-submission redirect, hence your POST array being empty. (The presence of an ACT parameter in the POST request triggers the actual form submission process inside EE, which then performs the redirect.)

What you really want to do is write a simple extension which uses the before_channel_entry_save (or if you're on EE2, channel_form_submit_entry_start) hook to look at the data (using the hook's passed data, not $_POST) and do what you need to do.

2
  • Thanks for your reply. So it is not possible to fetch the passed POST vars directly from the view?
    – gonn
    Feb 10, 2016 at 11:55
  • No, because as I said, you're not posting directly to that view. You're getting there via a redirect. A simple extension will do the trick though. Feb 13, 2016 at 19:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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