Page 1 of 1

Trying to grab value from a checkbox

Posted: Tue Apr 25, 2023 1:09 am
by tsignadze
Hi, I have an input field on a form and it is a checkbox, I am trying to run two different queries depending on if checkbox is ticked or not,
basically I want to input two different values into a table. I have tried with this method:

Code: Select all

$integrated = isset($_POST['Integrated']) ? 'Integrated' : 'NonIntegrated';

// Construct the SQL statement
$sql = "INSERT INTO your_table (Integrated) VALUES (?)";
did not work, I was trying to use nuGetValue, did not manage to grab it either.

Please someone help :) Thanks in advance!

Re: Trying to grab value from a checkbox

Posted: Tue Apr 25, 2023 1:49 am
by tsignadze
EDIT

got it working but there must be an easier and more elegant way

Code: Select all

define("checkboxChecked", 100);

$checkboxChecked = isset($_POST['Integrated']) && $_POST['Integrated'] == 'on';

if (checkboxChecked) {

Re: Trying to grab value from a checkbox

Posted: Tue Apr 25, 2023 8:02 am
by kev1n
Like this?

Code: Select all

$checkboxChecked = isset($_POST['Integrated']) && $_POST['Integrated'] === 'on';

if ($checkboxChecked) {

// ...
}

Re: Trying to grab value from a checkbox

Posted: Tue Apr 25, 2023 9:54 am
by tsignadze
Thanks for answer Kev1n.
Here is a video of my case:
https://streamable.com/a1jpji

Re: Trying to grab value from a checkbox

Posted: Tue Apr 25, 2023 10:00 am
by kev1n
Your code doesn't appear to match my suggestion.

Re: Trying to grab value from a checkbox

Posted: Tue Apr 25, 2023 10:07 am
by tsignadze
Sorry, missed $.

I have copy pasted your code now and it looks like this:
Also attached my object
Issue still remains. Always says Not ticked.

Code: Select all

define("checkboxChecked", 100);

$checkboxChecked = isset($_POST['Integrated']) && $_POST['Integrated'] === 'on';

if ($checkboxChecked) {
 $js = "nuMessage('Ticked');";
} else {
$js = "nuMessage('Not Ticked');";
}
nuJavaScriptCallback($js);
Screenshot 2023-04-25 120602.png

Re: Trying to grab value from a checkbox

Posted: Tue Apr 25, 2023 10:14 am
by kev1n
To pass values from JS to PHP, use nuSetProperty() in JS and nuGetProperty() in PHP.
If you use nuBuilder objects, just retrieve the value of a checkbox using the object id.

E.g.

Code: Select all

if ('#checkboxId#' == '1') {
  // checkbox checked
}
or

Code: Select all

if (nuGetProperty('checkboxId') == '1') {
  // checkbox checked
}

Re: Trying to grab value from a checkbox

Posted: Tue Apr 25, 2023 10:16 am
by tsignadze
THANK YOU! That was what I was trying to do :D

I will retry to run my queries but I think there is some other problem I have described in my other post and can not seem to understand. Hopefully I will be able to trace it, if not, will continue in other post.

Thank you very much!