Skip Navigation

[Resolved] 500 Admin-Ajax Error on Form Submit

This thread is resolved. Here is a description of the problem and solution.

Problem: I am using the cred_save_data Forms API with new and edit post Forms that create or edit child posts in a O2M post relationship. The Form contains a post relationship field where I have defined the parent using a URL parameter. I get a 500 error when submitting the Form with AJAX.

Solution: Do not rely on the $_GET superglobal to access values in URL parameters. AJAX submissions do not have access to the parent page's URL params in $_GET. Instead, it is better to use a hidden field in the Form using the URL parameter option to store the parent page's URL parameter value in the Form. Then you can access that value in a cred_save_data hook in the $_POST superglobal instead. This approach will work well in either AJAX or standard Form submissions.

It is also important to note that the post relationship managed in the Form will not be available to the Post Relationships API during a cred_save_data hook. Relationships are not established by the time the hook fires, so you would have to get the relationship field value directly from $_POST in a cred_save_data hook. Or, use a cred_submit_complete hook. Post relationshps are available in the Post Relationships API at the time of cred_submit_complete.

Relevant Documentation:
https://toolset.com/documentation/programmer-reference/cred-api/#cred_save_data
https://toolset.com/documentation/programmer-reference/cred-api/#cred_submit_complete
https://toolset.com/documentation/customizing-sites-using-php/post-relationships-api/#toolset_get_related_posts

This support ticket is created 3 years, 8 months ago. There's a good chance that you are reading advice that it now obsolete.

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
8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 - -
13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 - -

Supporter timezone: America/New_York (GMT-04:00)

This topic contains 10 replies, has 3 voices.

Last updated by himanshuS 3 years, 8 months ago.

Assisted by: Christian Cox.

Author
Posts
#1990847

I am trying to submit a form and get the following error : hidden link

jquery.min.js:2 POST hidden link 500

This error does not occur on any other form submission except this one. The error occurs in both edit and new post publish form.

I can share more details of the form on a private thread but would appreciate your help.

#1990861

To provide more context, there is an error in server logs:
2021-03-17 12:36:10 UTC [nginx][error] 53783#0: *2590393 openat() "/home/u742-0y1sfigedrww/www/showprowess.com/public_html/.well-known/pki-validation/ca3-ea095c7c2a684678852f2c43520f440d.txt" failed (2: No such file or directory), client: 172.69.27.83, server: showprowess.com, request: "GET /.well-known/pki-validation/ca3-ea095c7c2a684678852f2c43520f440d.txt HTTP/1.1", host: "showprowess.com"

This error has existed before and not stopped me from submitting the form earlier. As the 500 error only happens on one toolset form, I might have to do with the form itself.

#1991693

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Hello. Thank you for contacting the Toolset support.

Based on the information you shared I do not recognise that the issue is from Toolset here as 500 internal server errors should be related to server.

Can you please check with your server authority and show them the request that returns the 500 error and if they find that Toolset is the cause of this error, I am happy to investigate this further.

#1991769

Yes, I have checked with the hosting company. As I mentioned in my note, the is no issue on any form except for one form. Also, the link I shared clearly shows that toolset ajax is causing the issue.

Finally, I am on US time so it would be good if you can transfer the case to Christian.

Thank you.

#1992239

It seems like I have been able to identify the reason for the error.
I had two post types "expert match" and "expert review" in an O2O relationship.

I am passing expert match id as a URL param and using it to store data in custom tables using cred_save_data. It all worked just fine earlier when I was not using Ajax on the form. But, when I enable ajax, the URL param was not recognized by the form or by the custom code in cred_save_data. That created a null error and stopped the code from executing.

Is this expected behavior? I have turned off ajax on all my forms after this experience. I just doesn't seem reliable.

#1992623

I am passing expert match id as a URL param and using it to store data in custom tables using cred_save_data. It all worked just fine earlier when I was not using Ajax on the form. But, when I enable ajax, the URL param was not recognized by the form or by the custom code in cred_save_data. That created a null error and stopped the code from executing. Is this expected behavior?
I'm not sure, it depends on how you are passing that URL parameter into the Form, and how you try to access that URL parameter in the cred_save_data hook. Are you using the URL parameter to set the default value of a field in the Form, or using $_GET or the PHP referrer somehow to access the URL parameter somehow in the cred_save_data hook? Please explain the flow of URL parameter data from the page containing the Form to the cred_save_data hook. If you're using the expert form builder, please include any shortcodes that reference URL parameters. If you're using the drag-and-drop form builder, please include screenshots of the field settings for any fields that reference URL parameters. Please include the cred_save_data hook you mentioned so I can see the failing code.

2021-03-17 12:36:10 UTC [nginx][error] 53783#0: *2590393 openat() "/home/u742-0y1sfigedrww/www/showprowess.com/public_html/.well-known/pki-validation/ca3-ea095c7c2a684678852f2c43520f440d.txt" failed (2: No such file or directory), client: 172.69.27.83, server: showprowess.com, request: "GET /.well-known/pki-validation/ca3-ea095c7c2a684678852f2c43520f440d.txt HTTP/1.1", host: "showprowess.com"

I'm not convinced these errors are related, this issue seems to be related to certificate validation in HTTPS sites. The .well-known directory is usually created during domain validation.

#1992693

That's helpful.

Agreed that the error is not related to the issue.

So here is the flow:

1) Expert visits the expert match post.
2) Click on submit an expert review. In the URL on the expert review button, I am passing the expert-match-id as a parameter
3) The submit expert review page opens that has the submit expert review form.
4) The form has the relationship field that is getting value from the URL parameter
5) When the user submits the cred form, I am using the cred_Save_data hook to run code that inserts data in a custom table.
6) The code in the hook uses $_GET['expert-match-id'] to get the id.

All this works great in normal form submission.

When I use ajax form submission, the form submits and the post is created but no relationship is formed between expert match and expert review post type, Also, the code in the hook does not run and the user is not directed to the post.

Form setup: hidden link
Code1 on cred_save_data: hidden link
Code2 on cred_submit_complete (uses post id and toolset_get_related_post to get expert match id): hidden link

Let me know if you need anything else

#1992759

6) The code in the hook uses $_GET['expert-match-id'] to get the id
I would not expect to find the form parent page's URL params in $_GET in a cred_save_data hook with AJAX. With AJAX, the processor URL is admin-ajax.php and none of the URL params from the parent page are automatically passed along. Without AJAX, the processor URL is the same as the page containing the form, including existing URL params.

For consistency in either AJAX or non-AJAX submissions, you should access the related post ID in $_POST using the following syntax:

$_POST['@expert-match-to-expert-review_parent']

Note that the "." character in the field name is replaced by an underscore, this is expected.

...When I use ajax form submission, the form submits and the post is created but no relationship is formed between expert match and expert review post type
After form submission, and after the cred_save_data hook has run, I expect the relationship between the parent post and the new child to be created automatically. Can you confirm a form_messages field is included in the Form builder?

[cred_field field='form_messages' class='alert alert-warning']

If not, there may be an error returned by the Form submission but not displayed obviously. You should replace this field if it was deleted. If that is not the issue I'll need to take a closer look. Please provide login credentials and show me how to get to the child post Form on the site.

Also, the code in the hook does not run...
As I said, I expect $_GET['expert-match-id'] to be null in an AJAX submission, so logically the conditional in the cred_save_data hook would prevent most of the code from being executed. You must use the $_POST['@relationship-slug_parent'] syntax instead of the $_GET syntax.

...and the user is not directed to the post
See above about the form_messages field, that could be related to what you are experiencing. Or if not, what redirection settings are applied in the Form editor for the child post Form? Do you have a cred_success_redirect hook implemented for this Form? Even if you have a custom redirection hook, you must select one of the redirection options in the Form settings to redirect to some different location than the original page containing the Form. Otherwise the hook will not work.

#1993813

Christian,

Thanks for that feedback. I previously had issues with $_POST while retrieving parent post id so I reverted to url param.
I can try that again but I was also wondering if I can use

toolset_get_related_post($post_id, 'expert-match-id-to-expert-review', 'parent', array('post_status' => 'publish') )

to the parent post id?
Is the expert review id created AND post relationship formed by the time we run cred_save_data and cred_submit_complete?

#1995363

Is the expert review id created AND post relationship formed by the time we run cred_save_data and cred_submit_complete?
The expert review ID is essentially $post_id, available in either hook as the first callback parameter. The post relationship is not yet created for cred_save_data, so you cannot use toolset_get_related_post or toolset_get_related_posts to retrieve information about the related post in a cred_save_data hook. However, the post relationship should be available in cred_submit_complete, so you can use toolset_get_related_post or toolset_get_related_posts in a cred_submit_complete hook. In other words, the post relationship is established in between the cred_save_data and cred_submit_complete hooks.

#1998121

My issue is resolved now. Thank you!