Running Code during Form Entry

By: Yoni Sudwerts | Asked: 08/19/2024
ForumsCategory: Code SnippetsRunning Code during Form Entry
Yoni Sudwerts asked 4 weeks ago
So, I would like the first file in my form to ask the user for their email.
Then after they click next, I would like my system to run code (it needs to check if they have ordered a specific product in the past)
I would then the result of this query to be entered into a hidden field, and then I can use the value of this field in my condition logic to control which other questions will show up.

Can I do this with filters/hooks or do I need to do it with Javascript/Jquery ?
1 Answers
Victor Font answered 4 weeks ago
You can do this with jQuery/Ajax and a custom PHP callback.
Yoni Sudwerts replied 4 weeks ago

<p><p>that what I thought.</p><p>What event on the textbox should I use? "change"?</p><p>Will the Form wait for the response before it continues to check the condition?</p></p>

Yoni Sudwerts replied 4 weeks ago

I am sorry (I don't know why the html tags are showing in my reply)

Victor Font replied 4 weeks ago

It sounds like you may not be too familiar with Ajax. You set a listener on the change event for your email field. When the value changes, it will trigger your Ajax code to run. It have absolutely nothing to do with the form "waiting". When your AJAX RESPONSE is received from the server, you parse it and populate your hidden field.

Yoni Sudwerts replied 4 weeks ago

I understand what ajax is.

But In my case, the condition that I am testing is immediately after the user enters their email in the field.
So the hidden field essentially will not have a result from.the ajax call at the time that it's needed (unless the form waits for the result of the call to populate the hidden field. )

My probel is that the ajax call is asynchronous, I need it to be synchronous.... So it gets the result

Does that make any sense?

Victor Font replied 4 weeks ago

The form does nothing while the Ajax runs. You trigger the Ajax to retrieve the data when the email address is entered or changed. Your users will not always type their email address correctly so you put your jQuery listener on the email field's change event. As soon as the email field is populated or changes, your AJAX will run and populate the hidden field. You can also have better control over the conditionals by managing them yourself in the same jQuery function that populates the hidden field.

This is a very simple task you're trying to achieve with Formidable and custom Ajax.

Yoni Sudwerts replied 4 weeks ago

Thank you.

Can you elaborate more (or send me a link) to the last part of your comment?

"You can also have better control over the conditionals by managing them yourself in the same jQuery function that populates the hidden field."

Yoni Sudwerts replied 4 weeks ago

I am running into a problem
I am using Conversational Forms and the ".change()" jQuery does not trigger if the person enters their email and hits Enter or Next

Victor Font replied 4 weeks ago

.change() has been deprecated since jQuery 3.3. The preferred method is the change event: https://api.jquery.com/change/

How do you know the code isn't firing? How are you debugging the code?

Making the Best WordPress Plugin even better - Together

Take on bigger projects with confidence knowing you have access to an entire community of Formidable Experts and Professionals who have your back when the going gets tough. You got this!
Join the community
crossarrow-right