Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issue with checkbox and hidden fields #85

Closed
lgremme opened this issue Jan 16, 2021 · 8 comments
Closed

Issue with checkbox and hidden fields #85

lgremme opened this issue Jan 16, 2021 · 8 comments

Comments

@lgremme
Copy link

lgremme commented Jan 16, 2021

Thank you for very much for your great work.

In the last days I update a project to the newest version 1.2.2.
I have an issue with the creation of a checkbox:

require_once('Formr/class.formr.php');
$form = new Formr\Formr('bootstrap');
$form->open();
$form->csrf();
$form->checkbox('agree','I Agree','agree','agree','','','checked');
$form->close();

I miss the checkbox, it's similar to the issue #83.
The version 1.2.1 will work. At the moment I have downgrade to 1.2.1.

Another issue (version 1.2.1/1.2.2):

require_once('Formr/class.formr.php');
$form = new Formr\Formr('bootstrap');
$form->open();
$form->csrf(); // presents a hidden box in the source code
$form->hidden('Name','value'); // presents nothing in the source code
$form->close();

The CSRF hidden box will be present in the source code of the page and the hidden box with the Name will be not present. I can't find anything.

Please check this and have a nice weekend.

@timgavin
Copy link
Contributor

timgavin commented Jan 18, 2021

What do you mean by "I miss the checkbox?"

Your code outputs the following

<div id="_agree" class="form-check">
    <input checked type="checkbox" name="agree" id="agree" value="agree" class="form-check-input">
    <label class="form-check-label" for="agree"> I Agree</label>
</div>

I don't see an issue here...

The hidden issue has been fixed, thanks.

@lgremme
Copy link
Author

lgremme commented Jan 18, 2021

The issue with the checkbox:
PHP:

require_once('Formr/class.formr.php');
$form = new Formr\Formr('bootstrap');
$form->open();
$form->csrf();
$form->checkbox('agree','I Agree','agree','agree','','','checked');
$form->close();

HTML

<form action="/test.php" id="formr" method="post" accept-charset="utf-8">

<input type="hidden" name="csrf_token" value="437fbd30cc56f27301f1b13b9ac2ff87b8452eb23b0d125eca75907f8f443d22">

<div id="_agree" class="form-check
<input checked type=" checkbox"="" name="agree" value="agree">
<label class="form-check-label" for="agree"> I Agree
</label>
</div>

</form>

This issue is fixed with commit: ba65356
Thank you for your great work!

@lgremme lgremme closed this as completed Jan 18, 2021
@lgremme lgremme reopened this Jan 18, 2021
@lgremme
Copy link
Author

lgremme commented Jan 18, 2021

The issue with checkbox isn't fixed complete.
I use the following PHP code:

<?php
if (isset($_POST['agree'])) {
$setting_checked = "1";
} else {
$setting_checked = "0";
}

// Later in the code
require_once('Formr/class.formr.php');
$form = new Formr\Formr('bootstrap');
$form->open();
$form->csrf();
if($setting_checked == "1" ) {
$checked = 'checked';
$checked2 = 'checked';
} else {
$checked = '';
$checked2 = 'unchecked';
}
echo '<p>State of the box: '. $checked2 . '</p>';
$form->checkbox('agree','I Agree','1','agree','','',$checked);
$form->submit_button('Submit Form');
$form->close();
?>

After the first run with V 1.2.3 I get the following (box checked):

<form action="/test.php" id="formr" method="post" accept-charset="utf-8">

<input type="hidden" name="csrf_token" value="5fd0f36ed17c796a746cf79262fd2fa4b5843005a00dc1047d42979d6237c06f"><p>State of the box: checked</p>

<div id="_agree" class="form-check">
<input type="checkbox" name="agree" id="agree" value="1" class="form-check-input">
<label class="form-check-label" for="agree"> I Agree
</label>
</div>


<div id="_submit" class="form-group">
<label class="sr-only" for="submit"></label>
<button type="submit" name="submit" id="submit" class="btn btn-primary">Submit Form</button>
</div>

</form>

I miss the value for checked="".
Thank you in advance for your help.

@timgavin
Copy link
Contributor

timgavin commented Jan 18, 2021

The 7th parameter of the checkbox method is boolean; it's true or false but you're passing it a string, so it will always be true.

Regardless, if we strip your code down to the basics, we're basically telling the checkbox is if it should be checked or not. We don't need to pass a 'checked' string, as Formr will take care of this for us, all we need to do is pass it a TRUE or FALSE.

$checked = 0; 
$form->checkbox('agree','I Agree','1','agree','','',$checked);

Produces the following HTML (the checkbox is unchecked)

<label for="agree">
    <input type="checkbox" name="agree" id="agree" value="1"> I Agree
</label>

If we change $checked to 1 and run it...

$checked = 1; 
$form->checkbox('agree','I Agree','1','agree','','',$checked);

we get the following (the checkbox is checked)

<label for="agree">
    <input checked type="checkbox" name="agree" id="agree" value="1"> I Agree
</label>

It's working as it should.

EDIT: to make your code more streamlined you could just omit all the checks, and since $setting_checked I basically true or false, just pass it directly to the element, like so:

$form->open();
$form->csrf();
$form->checkbox('agree', 'I Agree', '1', 'agree', '', '', $setting_checked);
$form->submit_button('Submit Form');
$form->close();

@lgremme
Copy link
Author

lgremme commented Jan 18, 2021

I can't agree that's working.

After the first submit the state is checked, but the checkbox is unchecked.
I think, that there is an issue with the posted value.

Please test the script with checked and unchecked button.
My test:

  • check the checkbox and submit -> not working
    • Output: Checkbox is unchecked, value=1 is set in $_POST['agree']
  • uncheck the checkbox -> is unchecked

If I use the more forms for setting site (last function with the commit on 24 May), the site will work. After the upgrade to Version 1.2.3 the checkbox will not work.

Suspicious is, that the forms with more elements returned after the second submit the correct value.
Thank you for the hint, with the short version of the checks.

@timgavin
Copy link
Contributor

timgavin commented Jan 19, 2021

Let's boil this down to the most basic elements.

$form = new Formr\Formr(');
require 'Formr/class.formr.php';
$form = new Formr\Formr();
$form->open();
$form->checkbox('agree', 'I agree to the terms', 'agree');
$form->submit_button();
$form->close();

If you tick the checkbox and submit the form, the checkbox is checked. If you uncheck the checkbox and submit, it's no longer checked. It's working as intended, therefore, the issue is mostly likely with your code.

Are you using a $_SESSION?

@lgremme
Copy link
Author

lgremme commented Jan 19, 2021

With a session cookie isn't it better. I use a htpasswd authentication (basic) for the authentification in apache2.
This code works correct:

$form = new Formr\Formr(');
require 'Formr/class.formr.php';
$form = new Formr\Formr();
$form->open();
$form->checkbox('agree', 'I agree to the terms', 'agree');
$form->submit_button();
$form->close();

The output of this code:

$checked = 1; 
$form->checkbox('agree','I Agree','1','agree','','',$checked);

this is my issue. After the submit, I get the checkbox unchecked back. After every submit is the checkbox unchecked (with checked before).

$checked = 0; 
$form->checkbox('agree','I Agree','1','agree','','',$checked);

The code of the testpage:

<?php
session_start();
ini_set('error_reporting', E_ALL);
ini_set('display_errors', TRUE);

if (isset($_POST['agree'])) {
$setting_checked = 1;
echo "checked";
} else {
$setting_checked = 0;
}

// Later in the code
require 'Formr/class.formr.php';
$form = new Formr\Formr();
$form->open();
$form->csrf();
$checked = 1; 
$form->checkbox('agree','I Agree','1','agree','','',$checked);
$form->submit_button('Submit Form');
$form->close();
?>

PHP Version: 7.3.19 - apache2 - Raspbian OS

@timgavin
Copy link
Contributor

I found the problem. It's not actually with the checkboxes, it's with csrf. If you remove that the checkbox will work as intended. I'll work on this today and push an update. Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants