Jump to content
itsberni

Checkbox with dynamic value

Recommended Posts

Hi Guys,

i use the embed-method "D" in Formbuilder. In the form i use some checkboxes for calculating. To achieve this i set the value of the checkbox like this:

 

<input name="myCheckbox" type="checkbox" value="<?= $someValue ?>" />

When i send the Form, the value changes into the entry, i made in formbuilder in the Detail-Section of the checkbox-field.

does anybody know, how to accomplish, that the Form sends my specific value?

many thanks!

Share this post


Link to post
Share on other sites

I'm confused. Are you using checkboxes or radio buttons?

Unless you don't check the relevant radio button, you won't get any value after submitting the form. Or maybe I don't understand what you're really asking?

Share this post


Link to post
Share on other sites
4 minutes ago, dragan said:

I'm confused. Are you using checkboxes or radio buttons?

Unless you don't check the relevant radio button, you won't get any value after submitting the form. Or maybe I don't understand what you're really asking?

Sry for confusion.... corrected the Post.

Share this post


Link to post
Share on other sites

something for further explanation:
I want to calculate a price using checkboxes: e.g.
checked state

<input name="myCheckbox" type="checkbox" value="15" />

unchecked

<input name="myCheckbox" type="checkbox" value="0" />

the change of the value happens via js.

now, in case of submission, the value in the Database is that one ( Detail - Tab of the FB-Field )

621401838_Bildschirmfoto2019-10-16um05_55_06.png.debf88f54d04b3518844c55833bf7c0e.png

These entrys appear as well in a page ( i create a page with this data ) as in the admin-Email.

it seems to be, that FB ignores my specific value in case of submission. i also tried to let this fields empty to force the use of my values but the first one is mandatory. that works neither.

 

Some ideas.....?

Share this post


Link to post
Share on other sites

I don't use Form builder, and maybe I don't understand your problem, but - in case of unchecked field - on server side you will not have any value ( just like that field doesn't exist).

On 10/15/2019 at 2:07 PM, itsberni said:

When i send the Form, the value changes into the entry

 

On 10/16/2019 at 6:04 AM, itsberni said:

the change of the value happens via js.

I understand this like that you - on submit event - change/set checkbox value? If this is the case, do you 1) prevent default submission event, 2) change checkbox value, and after that, 3) proceed submission? Something like this:

// basic example
$("form").on("submit", function(e) {    
    // stop submission
    e.preventDefault();
    
    // set checkbox value
    // ...
    
    // proceed submission    
    $(this).submit(); // or return true or...
});

But if not, than form will not send checkbox value because it's happens later (after submit).

If all this is not a problem, maybe you can try to use different approach? As example, use hidden field - and - use checkbox change event to set it's value.
Or, another could be to use select field and maybe you can think about that option?

Regards.

Share this post


Link to post
Share on other sites

Thanks for sharing your thoughts. i'll try to solve it with a hidden field for each checkbox.

Share this post


Link to post
Share on other sites

@itsberni,

I'm not sure I understand your specific use case completely. When you say "Form sends my specific value", there are only two values for a checkbox when submitted: It's either present or not. For example:

if( isNull($input->post('YourCheckboxFieldNameHere')) ) {
// User did not check checkbox, so set a default or whatever.
}

If your specific value (used in your computations) is numeric, ie, a dollar amount, or temperature, or distance measurement, etc., then you will need to use an input type that accepts numeric entries. The checkbox is not the correct input type for your data. I don't use form builder so I can't provided specific instructions. In HTML it would be the input type text.

Can you be more elaborate with what you are trying to accomplish? I'm sure someone here will have the answer you seek.

 

Share this post


Link to post
Share on other sites

Hi Rick,

this was my misunderstanding. I had the opinion that i could set a specific value to a checkbox.

my usecase is a form, where you can choose ( via checkbox ) different "products". Every product has, of course, it‘s own price. When Checkbox is Chosen and the form is sent the value of the checkbox should be submitted - that was my thought - misbelief 😉

thank you though....

Share this post


Link to post
Share on other sites

If you allow the user to select one or more products, you can use a select field to present the options to the user. Of course it depends on the number of available products. If there are a large number of products, you may want to separate them by category. The select field's options allow for values that you require.

  • Like 1

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


  • Recently Browsing   0 members

    No registered users viewing this page.

×
×
  • Create New...