Programmatically setting a checkbox

This section is for posting questions which are not directly related to WYSIWYG Web Builder.
Examples of off topics: web server configuration, hosting, programming related questions, third party scripts.

Note that these questions will generally not be answered by the administrators of this forum.
Post Reply
Dan Pahl
 
 
Posts: 3
Joined: Wed Aug 10, 2022 9:37 pm

Programmatically setting a checkbox

Post by Dan Pahl »

I am working on a web page with just one form. In it I have several text boxes and many checkboxes. I am using PHP. I need to be able to programmatically set all the form values to have an edit web page. I have stripped my test page down to 1 editbox, 2 checkboxes and the submit button.

When I go to the Editbox Object properties->General tab->Initial value, I can set the value for that particular box using PHP. I just add “<?php echo $nameOfVideoFile ;?>” in the ‘initial value’ box. It works as I would expect.

The problem I am having is with the checkbox. I have tried the above process in many of the properties boxes for the check box. Nothing works. I have read that javascript might work and unfortunately, I am not a javascript programmer. If javascript is the workaround, where could I get a copy of that script? I will take it from there and make it work (I hope).

I can send the wbs file just in case I am missing something.

Thanks dan
User avatar
Pablo
 
Posts: 21569
Joined: Sun Mar 28, 2004 12:00 pm
Location: Europe
Contact:

Re: Programmatically setting a checkbox

Post by Pablo »

Via JavaScript you can set the value of a checkbox like this:
https://www.w3schools.com/jsref/prop_ch ... hecked.asp

Or with PHP, via Object HTML -> Inside tag.

Code: Select all

<?php echo $isChecked ? "checked" : ""; ?>
Note that this is not specific to WWB, it is standard HTML functionality.
Dan Pahl
 
 
Posts: 3
Joined: Wed Aug 10, 2022 9:37 pm

Re: Programmatically setting a checkbox

Post by Dan Pahl »

Thanks Pablo. I guess my real question is this:
At the top of my code, I get the checkbox value of ‘Alex’ from a data base. I need to set the checkbox value before the page loads.
I have looked at and implemented the javascript code you referenced. The only problem with that is it is executed when the page is already loaded and the person clicks the button.

I need to be able to programmatically checked, or unchecked within WWB by having “<?php echo $Alex ;?>” in the checkbox property box somewhere. This can be done with the text boxes.

I have tried executing the javascript from php (below) but it is not working.

I kind of gather that is may be a server side/client side issue.

Here is a stripped down html page. Any help would be appreciated.
Thanks again
Dan

<!doctype html>
<html>
<head>
<script>
function alexcheck() {document.getElementById("Checkbox5").checked =TRUE;}
function alexUNcheck() {document.getElementById("Checkbox5").checked =FALSE;}
</script>
</head>
<body>
<?PHP
echo 'executing php code<br>';
// the ALEX checkboxbox value was gotten from the data base
$Alex=TRUE;
// $Alex=FALSE; // just for testing

// this code should execute the javascript to set or unset the checkbox as
// the html page is being rendered. Correct?

if($Alex==TRUE){ echo '<script type="text/javascript">alexcheck();</script>'; }
else{ echo '<script type="text/javascript">alexUNcheck();</script>'; }
?>

<form name="PrivateEntryForm" method="post" action="" target="_blank" id="Form1">
<div id="wb_Checkbox5" >
<input type="checkbox" id="Checkbox5" name="Alex1" value="on" title=" ">
<label for="Checkbox5">check box 1</label>
</div>
<input type="submit" id="Button2" name="sumbit" value="sumbit" >
</form>
123
</html>
User avatar
Pablo
 
Posts: 21569
Joined: Sun Mar 28, 2004 12:00 pm
Location: Europe
Contact:

Re: Programmatically setting a checkbox

Post by Pablo »

If you use

Code: Select all

<?php echo $isChecked ? "checked" : ""; ?>
then you can set the value before the page loads.

Note that I cannot help you with programming related questions because for me it may also take a lot of time to write and test code. This is beyond the scope of this forum.
I was just trying to point you in the right direction.
Dan Pahl
 
 
Posts: 3
Joined: Wed Aug 10, 2022 9:37 pm

Re: Programmatically setting a checkbox

Post by Dan Pahl »

Thanks.
One more question. I know i can add that code to the checkbox statement after I compiling it.
However, does WWB allow me to add it to the checkbox property box somewhere during design?
User avatar
Pablo
 
Posts: 21569
Joined: Sun Mar 28, 2004 12:00 pm
Location: Europe
Contact:

Re: Programmatically setting a checkbox

Post by Pablo »

You can add the code via Object HTML -> Inside tag
Post Reply