Hardcode input and post to database

WordPress Event Management, Calendars & Registration Forums General Question Hardcode input and post to database

This topic contains 10 replies, has 2 voices, and was last updated by  Alex Steer 9 years, 9 months ago.

Viewing 11 posts - 1 through 11 (of 11 total)
  • Author
    Posts
  • #17335

    Hello,

    Is it possible to hardcode an input field in a booking form and post it to the system?

    It would also need to use the validation.

    How would I go about that, is there a snippet?

    Many thanks.

    • This topic was modified 9 years, 9 months ago by  Alex Steer.
    Alex Steer
    #17343
    Stephen Harris
    #17349

    Hi Stephen,

    That worked brilliantly.

    However, I am stuck on somthing that I wouldn’t mind your advice on…

    I have created some custom fields on the event admin that populate the key => value for some radios.
    They work useing the api code you pointed me too – no problem, but I would like to add a description under each radio option.

    The description is stored in a custom field too but I am unsure of the best way to add.

    What would be the best way of acheiving this?

    Many thanks for your help.

    Alex Steer
    #17365

    The best way might be to edit the template file (see event-organiser-pro/templates/booking-form/).

    The EO_Booking_Form_Element class has set( $key, $value ) and get( $key ) methods so you can pass any data to the templates (e.g. array of descriptions indexed by option value).

    I’d recommend prefixing your key with some unique to avoid any collisions with core’s keys.

    Stephen Harris
    #17366

    Thanks Stephen,

    I managed to get somthing working in similar way using $value on the template. – It’s good to know you suggested somthing similar.

    This worked ok… The issue I am now left with relates to the api array because the key and value are stored in the custom fields.

    When I submit and trigger validation I lose my added form fields – the variables containing key and value disapear – I have also tried sessions too.

    I’m now looking at another way to make the array values populate upon condition.

    Many thanks.

    Alex Steer
    #17368

    Not sure I follow… is the key not fixed and known?

    If you need to get the event ID at any point you can use $form->get( 'event_id' )

    Stephen Harris
    #17369

    Overall i’m trying to list some radio options which are editable on the events page (in custom fileds) rather than through the booking form editor.

    I have it pulling through the data and creating the radio options (with individual descriptions) depending on whether they are populated.

    This works on the single event page but breaks when the form bounces off validation.

    I’m guessing im loosing access to the declared variables when/after posting.

    Thanks

    Alex Steer
    #17371

    How are you adding the fields? The form shouldn’t ‘forget’ fields in the same page request.

    Stephen Harris
    #17372

    I have the api code in a seperate plugin I created like suggested in the docs.

    My code:

    function my_add_element( $form, $event_id ){

        $exopone = types_render_field("price-option-one", array('output'=>'raw'));
        $exoptwo = types_render_field("price-option-two", array('output'=>'raw'));
        $exopthree = types_render_field("price-option-three", array('output'=>'raw'));
    
    //Create element to add to the field
    $element = EO_Booking_Form_Element_Factory::create( array(
         'type'     => 'radio',
         'id'       => '_supplementary_contact', //a unique ID
         'required' => 1, //make this field optional
         'options'     => array( 
                $exopone => $exopone,
                $exoptwo => $exoptwo,
                $exopthree => $exopthree
                ),
         'label'    => 'Please choose a price option'
    

    ) );

    //Add the field element, optionally specify where
    $form->add_element( $element, array( ‘at’ => 1 ) );
    }
    add_action( ‘eventorganiser_get_event_booking_form’, ‘my_add_element’, 10, 2 );
    ?>

    I was planning to unset them after depending on whether they exist – or use if else.

    Alex Steer
    #17373

    Hi think the problem is types_render_field() – you’re not passing the event ID so I’m assuming that it’s using the global post to get the ID, and since that’s not always going to available (or correct), that would explain the mixed results.

    Is there a similar function where you can pass the event ID ($event_id)

    Stephen Harris
    #17375

    Look like you were bang on.

    I swapped the code for:

    function my_add_element( $form, $event_id ){
    
        $url = explode('?', 'http://'.$_SERVER["HTTP_HOST"] . $_SERVER["REQUEST_URI"] ); 
        $ID = url_to_postid( $url[0] );
    
        $exopone = do_shortcode('[types field="price-option-one" id="'. $ID .'"]');
        $exoptwo = do_shortcode('[types field="price-option-two" id="'. $ID .'"]');
        $exopthree = do_shortcode('[types field="price-option-three" id="'. $ID .'"]');
    
        //Create element to add to the field
        $element = EO_Booking_Form_Element_Factory::create( array(
             'type'     => 'radio',
             'id'       => '_supplementary_contact', //a unique ID
             'required' => 1, //make this field optional
             'options'     => array( 
                    $exopone => $exopone,
                    $exoptwo => $exoptwo,
                    $exopthree => $exopthree
                    ),
             'label'    => 'Please choose a price option'
         ) );
    
        //Add the field element, optionally specify where 
        $form->add_element( $element, array( 'at' => 1 ) ); } 
        add_action( 'eventorganiser_get_event_booking_form', 'my_add_element', 10, 2 );
    

    and it now works as intended – it still needs testing!

    Thanks for pointing me in the right direction.

    • This reply was modified 9 years, 9 months ago by  Stephen Harris.
    Alex Steer
Viewing 11 posts - 1 through 11 (of 11 total)
To enable me to focus on Pro customers, only users who have a valid license for the Pro add-on may post new topics or replies in this forum. If you have a valid license, please log-in or register an account using the e-mail address you purchased the license with. If you don't you can purchase one here. Or there's always the WordPress repository forum.