2 forms on the same site?
-
I want to create 2 forms, 1 for over 18’s and 1 for under 18’s. The under 18’s form will have some extra fields that they have to fill out.
Is it possible to create 2 forms for this purpose? Or could I create a field then when selected (18 or under 18) the the extra fields are displayed?
-
Ok, So I thought I had this nailed, but ive run into 1 small problem.
i’ve got all the templates setup and created 2 separate forms. I am able to load back the relevant form fields on the edit page depending on the initial form filled out.
The issue i am having is with the post parent phone and email. By using the code above in my functions.php file i am able to post the parent info from the under 18 form into the main phone and email fields.
However this causes a problem on the over 18’s form as the email & phone fields are expecting to = the parent email and phone fields, but they do not exist on the over 18’s form.
I have edited the function to apply only to the under 18’s form using the code below. This allows the over 18 form to post without issues, but the under 18 form will not post and gives me an error for the email & phone fields.
I’m not sure how correct the code below is, i have a feeling the if page is in the wrong place or i’m missing some other code that will allow the form to post.
if (is_page('under-16-registration-form') ) { add_filter('pdb-before_submit_signup', 'xnau_copy_field_values'); function xnau_copy_field_values( $post ) { $post['email'] = $post['parent_email']; $post['phone'] = $post['parent_phone']; return $post; } }This is a common issue in writing WordPress filters, you need to understand that things are done in a specific sequence, so you can’t perform certain operations too early. You’re trying to use the “is_page” function too early. Do it like this instead:
add_filter('pdb-before_submit_signup', 'xnau_copy_field_values'); function xnau_copy_field_values( $post ) { if (is_page('under-16-registration-form') ) { $post['email'] = $post['parent_email']; $post['phone'] = $post['parent_phone']; } return $post; }I have just tried it with your code and it is still giving me an error for the hidden phone and email fields.
The Email field appears to be incorrect.
The Phone field is required.I think the hidden fields are not being filled when the form is being submitted. If I remove the “if page” line from the code the fields are being posted correctly, but then as i mentioned before the over 18’s form does not post correctly.
I have double checked the field DB values are correct in the function. Any ideas what I could try next? This is the last piece of the puzzle.
OK, you can try this: in the under-18 template, you should pre-set the hidden field values with temporary values so the form will validate.
at the top of the template, something like this:
$this->hidden_fields['email'] = 'temp'; $this->hidden_fields['phone'] = 'temp';Tried that too, the form is posting but its saving the default value “temp”.
The function doesn’t seem to want to work when applying it to that specific page. Perhaps there is another way to write the function?
it’s possible the page test isn’t working as expected. Another way to do the logic is to look at a field in the POST array, such as if the parent email is filled in, you know it came from the under-18 form.
you mean instead of checking the page, to check against a field on the page. I’m using this code and everything seems to be working as it should. I needed to add the extra = in the if section to get it to work.
add_filter('pdb-before_submit_signup', 'xnau_copy_field_values'); function xnau_copy_field_values( $post ) { if ($post['age'] == ( 'under 16' ) ) { $post['email'] = $post['parent_email']; $post['phone'] = $post['parent_phone']; } return $post; }Yes, in PHP, the = is for assigning a value. for checking an equality you need to use == or ===
glad you got it working.
Hey roland, thanks again for all the time and support. I think ive got everything figured out now.
I want to create a function, to copy the parent field values on the edit form also. I can use the same code as the first function, but What is the filter hook to apply it to the edit record page?add_filter('pdb-before_submit_signup', 'xnau_copy_field_values');Yes, put in another filter with the same callback on ‘pdb-before_submit_udate’ that should do the same thing.
thanks roland. Got that working too.
I promise this is the last thing I need to do. I have created a page template for the PDB single page and used the following code to check if the submitted form is over 18 or under 18 then output the relevant fields
<?php $participant = Participants_Db::get_participant( Participants_Db::get_participant_id( filter_input( INPUT_GET, 'pdb', FILTER_SANITIZE_STRING ) ) ); if ( $participant['age'] == "under 16" ) : echo do_shortcode ('[pdb_single groups="main,parent_or_guardian_details,personal"]') ?> <?php else : echo do_shortcode ('[pdb_single groups="main,contact_information,personal"]') ?> <?php endif ?>For some reason the IF part is not working and only seems to output the second shortcode. Do I need to use something different for the PDB single page to check against the database?
I used this code on the edit page which uses the PID in the URL, on the single page its uses the PDB value in the URL, but im not sure if that would make a difference.OK well you’re getting your record ID from the GET variable, right? The GET variable comes in through the URL and it has a different name in this case, so you need to use that name in the inout_filter function in order to get that value..
The topic ‘2 forms on the same site?’ is closed to new replies.