Home › Toolset Professional Support › [Resolved] two submit buttons
This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.
Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.
Sun | Mon | Tue | Wed | Thu | Fri | Sat |
---|---|---|---|---|---|---|
- | 10:00 – 13:00 | 10:00 – 13:00 | 10:00 – 13:00 | 10:00 – 13:00 | 10:00 – 13:00 | - |
- | 14:00 – 18:00 | 14:00 – 18:00 | 14:00 – 18:00 | 14:00 – 18:00 | 14:00 – 18:00 | - |
Supporter timezone: Asia/Kolkata (GMT+05:30)
This topic contains 8 replies, has 2 voices.
Last updated by Jennifer 2 years, 2 months ago.
Assisted by: Minesh.
Tell us what you are trying to do?
I need a form with two submit buttons (disagree and agree) that store different values for a field.
With the agree-button, the value "p" should be transferred to the field 'probant-n-p'.
With the disagree-botton, the value "n".
Here is the code, that I'm using:
Post Form:
[cred_field field='form_submit' output='bootstrap' value='disagree' class='btn btn-primary btn-lg '] [cred_field field='form_submit' output='bootstrap' value='agree' class='btn btn-primary btn-lg '] -->
PHP Code:
// whethter the probant agrees or disagrees with the given thesis - depending on the button, that he has clicked to submit the form $probant_n_p = ''; if (isset($_POST['form_submit_disagree'])) { $probant_n_p = 'n';} // form_submit_disagree => Disagree-Button if (isset($_POST['form_submit_agree'])) { $probant_n_p = 'p';} // form_submit_agree => Agree-Button
If I give the button a different name e.g. form_submit_disagree , the buttons are no longer displayed.
[cred_field field='form_submit_disagree' output='bootstrap' value='disagree' class='btn btn-primary btn-lg '] [cred_field field='form_submit_agree' output='bootstrap' value='agree' class='btn btn-primary btn-lg '] -->
Is there a solution to the problem without using two different forms?
Is there any documentation that you are following?
https://toolset.com/forums/topic/multiple-submit-buttons-on-cred-form/
Is there a similar example that we can see?
What is the link to your site?
hidden link
Hello. Thank you for contacting the Toolset support.
You should use the Toolset form hook "cred_save_data" in order to check what submit button is set.
Where you should keep the submit button as given under:
[cred_field field='form_submit' output='bootstrap' value='disagree' class='btn btn-primary btn-lg '] [cred_field field='form_submit' output='bootstrap' value='agree' class='btn btn-primary btn-lg ']
For example - you should try to add the following hook code to the "Custom Code" section offered by Toolset:
=> https://toolset.com/documentation/programmer-reference/adding-custom-code/using-toolset-to-add-custom-code/#adding-custom-php-code-using-toolset
add_action('cred_save_data', 'func_check_submit_button',10,2); function func_check_submit_button($post_id, $form_data) { if ($form_data['id']==9999) { $probant_n_p = ''; if (isset($_POST['form_submit_1'])) { $probant_n_p = 'n'; } // form_submit_disagree => Disagree-Button if (isset($_POST['form_submit_2'])) { $probant_n_p = 'p'; } } }
Where:
- Replace 9999 with your original Toolset form ID
Hi Minesh,
many thanks for your answer : )
Yes, I'm using the "cred_save_data" hook to check it. I have attached an extract of the code, that I am using below.
It does submit the form - everthing works fine, except that neither if (isset($_POST['form_submit_1'])) { $probant_n_p = 'n';} nor if (isset($_POST['form_submit_2'])) { $probant_n_p = 'p';} will be executed. Both if-conditions return false. Therefore I assume that neither isset($_POST['form_submit_1']) nor isset($_POST['form_submit_2']) is the "thing" that should be within the if(...) brackets. What else can I use there instead?
add_action('cred_save_data', 'lk_create_these_probant_intermediary',10,2); function lk_create_these_probant_intermediary ($post_id, $form_data) { $forms = array( 212 ); if ( in_array( $form_data['id'], $forms )) { $intermediary_post_id = get_the_ID(); // whethter the probant agrees or disagrees with the given thesis - depending on the button, that he has clicked to submit the form $probant_n_p = 'test'; if (isset($_POST['form_submit_1'])) { $probant_n_p = 'n';} // form_submit_disagree => Disagree-Button if (isset($_POST['form_submit_2'])) { $probant_n_p = 'p';} // form_submit_agree => Agree-Button // Update intermediary -- insert new values $intermediary = array( 'ID' => $intermediary_post_id, 'meta_input' => array ( 'wpcf-these-n-p' => $_POST['these-n-p'], 'wpcf-probant-n-p' => $probant_n_p, 'wpcf-these-zahl' => $_POST['these-zahl'], 'wpcf-these-position' => $_POST['these-position'], 'wpcf-these-zufall' => $_POST['these-zufall'], ), ); // Update intermediary -- execute update wp_update_post ($intermediary); } }
Kind regards
Jennifer
I will require problem URL from where you are trying to submit the form as well as admin access details.
*** Please make a FULL BACKUP of your database and website.***
I would also eventually need to request temporary access (WP-Admin and FTP) to your site. Preferably to a test site where the problem has been replicated if possible in order to be of better help and check if some configurations might need to be changed.
I have set the next reply to private which means only you and I have access to it.
I will require FTP access details in order to make edits to plugin files where you added the code.
Can you please send me FTP/SFTP access details.
I have set the next reply to private which means only you and I have access to it.
The reason why it was not working is - you set your form to submit with the AJAX with the following form setting:
- Submit this form without reloading the page (use AJAX)
With AJAX submit form there is no way to identify what button is clicked as it does not post the submit button data.
To make this work you will have to use non-AJAX form and that is why I have just unchecked the form submit with AJAX setting and saved the form:
=> hidden link
As you can see with the above form the setting for "Form submit:" is unchecked.
Now with normal submit form we can have a way to identify what button is clicked and the code you added to your plugin works.
Can you please check now.
Hey Minesh,
Thanks for the tip - I didn't think of that 😉
I tested it with your change and it works wonderfully, but only for the first two posts that are created with the form on that page.
In the further posts that are created, it no longer transferred the values n and p.
Why can that be?
The issue was you have added the form in to the loop so the button names are dynamic.
I've adjusted the code you added to your "cred_save_data" hook added to your plugins file:
add_action('cred_save_data', 'lk_create_these_probant_intermediary',99,2); function lk_create_these_probant_intermediary ($post_id, $form_data) { $forms = array( 212 ); if ( in_array( $form_data['id'], $forms )) { // id of probandt and these $id_probant = $_POST['probandt-id']; $id_these = get_the_ID(); $found_agree = array_search("agree",$_POST); $found_disagree = array_search("disagree",$_POST); if(!empty($found_agree)) { $clicked_button = $_POST[$found_agree]; }else if(!empty($found_disagree)){ $clicked_button = $_POST[$found_disagree]; } // whethter the probant agrees or disagrees with the given thesis - depending on the button, that he has clicked to submit the form $probant_n_p = ''; if ($clicked_button == 'disagree' ) { $probant_n_p = 'n';} // form_submit_disagree => Disagree-Button if ($clicked_button == 'agree') { $probant_n_p = 'p';} // form_submit_agree => Agree-Button // search if a connection between this probant and the thesis already exist $query = new WP_Query( array( 'post_type' => 'probant-these', 'posts_per_page' => -1, 'toolset_relationships' => array( array( 'role' => 'intermediary', 'related_to' => $id_probant, 'role_to_query_by' => 'parent', 'relationship' => 'probant-these' ), array( 'role' => 'intermediary', 'related_to' => $id_these, 'role_to_query_by' => 'child', 'relationship' => 'probant-these' ) ), //'meta_key' => 'wpcf-genre', //'orderby' => 'meta_value', //'order' => 'ASC', ) ); $intermediary_posts = $query->posts; $loop_iteration = 1; //if it returns some posts if($intermediary_posts){ //now get the single posts fields values foreach($intermediary_posts as $intermediary ){ //get each Posts post data $intermediary_post_data = get_post($intermediary); //get each ID $intermediary_post_id = $intermediary_post_data->ID; //get each posts field value if ($loop_iteration == 1) { //------------------------------------------------------ first loop interation // Update intermediary -- insert new values $intermediary = array( 'ID' => $intermediary_post_id, 'meta_input' => array ( 'wpcf-these-n-p' => $_POST['these-n-p'], 'wpcf-probant-n-p' => $probant_n_p, 'wpcf-these-zahl' => $_POST['these-zahl'], 'wpcf-these-position' => $_POST['these-position'], 'wpcf-these-zufall' => $_POST['these-zufall'], ), ); // Update intermediary -- execute update wp_update_post ($intermediary); // increase $loop_iteration by 1 $loop_iteration += 1; } else { //------------------------------------------------------ if there is more than one intermediary -> delete it wp_delete_post( $intermediary_post_id, $force_delete = true); // increase $loop_iteration by 1 $loop_iteration += 1; }}} else { //------------------------------------------------------ if both pots are not related/connected with each other yet //make new connextion and new intermediary //connect Probant and These $relationship_response = toolset_connect_posts( $relationship = 'probant-these', $id_probant, $id_these ); $intermediary_post_id = $relationship_response['intermediary_post']; // Update intermediary -- insert new values $intermediary = array( 'ID' => $intermediary_post_id, 'meta_input' => array ( 'wpcf-these-n-p' => $_POST['these-n-p'], 'wpcf-probant-n-p' => $probant_n_p, 'wpcf-these-zahl' => $_POST['these-zahl'], 'wpcf-these-position' => $_POST['these-position'], 'wpcf-these-zufall' => $_POST['these-zufall'], ), ); // Update intermediary -- execute update wp_update_post ( $intermediary ); } } }
The part that is changed to check the clicked button is as given under with reference to above code:
$found_agree = array_search("agree",$_POST); $found_disagree = array_search("disagree",$_POST); if(!empty($found_agree)) { $clicked_button = $_POST[$found_agree]; }else if(!empty($found_disagree)){ $clicked_button = $_POST[$found_disagree]; } // whethter the probant agrees or disagrees with the given thesis - depending on the button, that he has clicked to submit the form $probant_n_p = ''; if ($clicked_button == 'disagree' ) { $probant_n_p = 'n';} // form_submit_disagree => Disagree-Button if ($clicked_button == 'agree') { $probant_n_p = 'p';} // form_submit_agree => Agree-Button
Can you please check now. It should be working as expected now.
My issue is resolved now. Thank you!