ExpressionEngine CMS
Open, Free, Amazing

Thread

This is an archived forum and the content is probably no longer relevant, but is provided here for posterity.

The active forums are here.

New input class

March 11, 2011 12:12am

Subscribe [4]
  • #1 / Mar 11, 2011 12:12am

    DougH

    205 posts

    The new input class has my head spinning.

    In v1.7, I used the following code to collect info passed through a form:

    <?php
    
    global $IN;
    
    $value = ( ! $IN->GBL('avatar_filename', 'POST')) ? 'Default Value' : $IN->GBL('avatar_filename', 'POST');
    
    ?>

    How do I collect the same information in v2.1.3?

    I’ve tried:

    <?php
    
    $value = $this->EE->input->post('avatar_filename');
    
    ?>

    and every other imaginable combination but nothing is giving me the value of $value.

    Help?

  • #2 / Mar 11, 2011 5:21am

    Mark Bowen

    12637 posts

    Hiya,

    I just tried this out, albeit in 2.1.4, though I doubt much will have changed between that version and yours and the code you posted above worked fine for me. I also tried :

    <?php
    
    $value = $_POST['avatar_filename'];
    echo "Test - ".$value;
    
    ?>

    which seemed to work fine too. How are you testing the value at the other end as you don’t have an echo statement in there at the moment.

    Best wishes,

    Mark

  • #3 / Mar 11, 2011 9:27am

    DougH

    205 posts

    Hey Mark,

    I actually was using an echo but it returned a blank.

    Here’s what I’m trying to do:

    I created a channel for users to upload profile photos. In a certain template, a user can view all of the profile photos he/she has uploaded. I wrote a plugin to check each photo’s filename against the user’s current avatar.

    if ($photo_filename == $row['avatar_filename'])
    { 
    $avfilename = $row['avatar_filename'];
    $this->return_data = "<input type=\"radio\" name=\"avatar_filename\" value=\"$avfilename\" class=\"profile_pic_btn\" checked=\"checked\">";
    }
    else
    {
    $avfilename = $row['avatar_filename'];
    $this->return_data = "<input type=\"radio\" name=\"avatar_filename\" value=\"$avfilename\" class=\"profile_pic_btn\">";
    }

    If the 2 filenames match, a radio button returns as checked. If they don’t match, a radio button returns as unchecked. Basically anytime a user goes to this page, the radio buttons indicate which photo is their current avatar. The user can click a radio button to choose a different profile photo and hit the submit button which then redirects them to another template that updates ‘avatar_filename’ in exp_members.

    The template being redirected to is the one giving me a problem. I’m not getting ‘avatar_filename’ from the form.

    <?php $value = $this->EE->input->post('avatar_filename'); ?>

    doesn’t work. Neither does

    $value = $_POST('avatar_filename');

    which gives me a “Fatal error: Function name must be a string”.

    Could it be because ‘avatar_filename’ is coming from a plugin???

  • #4 / Mar 11, 2011 9:47am

    Mark Bowen

    12637 posts

    Hmm not too sure on that one as you’re using a plugin. Didn’t know that’s what you were doing.

    I tested this out with just a bog standard form with a text input set to have the name of avatar_filename, filled in the field and submitted the form of which the post data was then picked up on the next page.

    As to what’s going on with your setup though I’m not sure as I don’t have access to all the code that you’re using so it’s a bit difficult to say really :-(

    Have you turned on template debugging to see if everything is being spat out to the page correctly?

    Also if you make a bog standard form like I did (hard-coded) and submit that do you get the output that you’re after? If you don’t get it with a standard form then there’s something else afoot here. If you do then you’d need to break down your code piece by piece and see where it’s failing.

    Best wishes,

    Mark

  • #5 / Mar 11, 2011 2:35pm

    Rick Jolly

    729 posts

    $_POST isn’t a function, it’s an array. Note the square brackets in Mark’s example:

    $_POST['avatar_filename'];

    However, you should be able to use the input class. You are probably forgetting to set the EE instance first:

    $this->EE =& get_instance();
    $this->EE->input->post('avatar_filename');
  • #6 / Mar 11, 2011 8:39pm

    DougH

    205 posts

    Cue the “Hallelujah Chorus”! I’ve got everything working.

    I dumped the plugin and ended up writing a couple of queries in PHP to make all the elements get along.

    Thanks Mark and Rick.

  • #7 / Mar 12, 2011 3:00am

    Mark Bowen

    12637 posts

    Excellent news. Thank Rick though, not me as he saw what was going on.

    On a related note Rick I was just wondering. I myself did put in the

    $this->EE =& get_instance();

    code when I first tried testing the code and it worked fine but then I also noticed it worked without it too and that’s why I didn’t mention it in my code above. Is there a reason why it works without it as I noticed in the documentation that you’re meant to have that in there?

    I’m really not even too sure what that’s all for as it’s all a bit new to me the new language that is.

    I was told that in a plugin for example you only have to put that in once in a constructor? for it to work through the whole plugin although the other day I tried converting a plugin that I’d made a while back and found out that I needed that line of code in every function in order for it to work. Do you know why that would be at all.

    Sorry for poaching your post like this Doug 😉 it’s just it was related and I thought you wouldn’t mind now that you’re up and running? 😉

    Best wishes,

    Mark

  • #8 / Mar 12, 2011 11:36am

    John Fuller

    779 posts

    Mark, I think you are looking too far into what happened in this post.  Doug never mentioned what was wrong, he simply threw out what he had and took a different approach.

    Is there a reason why it works without it as I noticed in the documentation that you’re meant to have that in there?

    Obviously there is a big difference between a template and a plugin. 

    The template is just a string which is parsed by the Template class.  When PHP is found in a template it’s put in a buffer and run through the evaluate (eval) method of the Functions class.  These classes have already created a reference to the EE super object, so you don’t have to create another reference in your PHP template  

    A plugin is it’s own class and needs to make it’s own reference to the EE super object so that it can access all the ExpressionEngine resources, otherwise it can’t do much.

  • #9 / Mar 12, 2011 11:54am

    Mark Bowen

    12637 posts

    Mark, I think you are looking too far into what happened in this post.

    Hopefully not. I was just trying to understand it all a bit better myself really.

    Is there a reason why it works without it as I noticed in the documentation that you’re meant to have that in there?

    A plugin is it’s own class and needs to make it’s own reference to the EE super object so that it can access all the ExpressionEngine resources, otherwise it can’t do much.

    I kind of understand that but I was more referring to if you have a plugin which has a class and then say 10 functions within that class, someone told me that you only have to have that reference to the super object inside the class once and you don’t need to reference it again in each of the 10 functions.

    I tried converting a plugin the other day which has a lot of functions in it and none of them would work unless I placed that super object reference in each and every one of them.

    I was just wondering why it wasn’t working really.

    Again Doug, sorry for hijacking the thread.

    Best wishes,

    Mark

  • #10 / Mar 12, 2011 9:30pm

    John Fuller

    779 posts

    I was told that in a plugin for example you only have to put that in once in a constructor?

    Right, if you created a reference to the EE super object in the constructor then that should have been good enough.  Take a look at any EE 2.x plugin as an example.  Perhaps your syntax for the constructor was incorrect?  Maybe post an example of the constructor syntax that you were using.

  • #11 / Mar 13, 2011 8:00am

    John Fuller

    779 posts

    What version of PHP is this?  Did you try the old school method?  That is, you have a function with the same name as the class as you would do with the 1.x plugins.

  • #12 / Mar 13, 2011 8:42am

    Mark Bowen

    12637 posts

    PHP V5.2.11

    I actually just noticed a bug report referencing this problem, or at least I think it does. Not sure what it all means though.

    I did go back to having a function with the same name yes and that works fine but it’s a bit strange as the other plugin which does work uses the new method and works fine. All very strange.

    Just wondering though, what exactly (once it’s working correctly) is the __construct() function for? All I’ve ever seen referencing it is it having that one line of code in it. I’m sure it’s probably for more than just that though?

    Thanks again for all the help with me understanding this. It’s very much appreciated.

    Best wishes,

    Mark

  • #13 / Mar 13, 2011 8:54am

    Mark Bowen

    12637 posts

    Hi John,

    I was shown to do this :

    class My_class {
    
        var $return_data = '';
    
    
        /** -------------------------------------
        /**  Constructor
        /** -------------------------------------*/
        function __construct()
        {
            $this->EE =& get_instance(); // Only need to call this once
        }
    
        function one()
            {
                echo "Something";
            }
    
        function two()
            {
                echo "Something else";
            }
    
    }

    That worked in the plugin that someone helped me out with but as soon as I tried that in another plugin that I had created and was converting none of the functions worked unless I specifically placed that super object line into each and every function.

    Best wishes,

    Mark

  • #14 / Mar 13, 2011 1:15pm

    John Fuller

    779 posts

    Ah, nice find on that bug.  It will be fixed in the next release according to the status.

    A constructor is basically just a method which is automatically run when instantiating the class.

  • #15 / Mar 13, 2011 1:34pm

    Mark Bowen

    12637 posts

    A constructor is basically just a method which is automatically run when instantiating the class.

    Ah right, thanks for that. Still don’t quite understand why it worked in one plugin and not another when they were both on the same environment. Weirdness.

    Best wishes,

    Mark

.(JavaScript must be enabled to view this email address)

ExpressionEngine News!

#eecms, #events, #releases