0

I'm still fairly green with PHP and I'm trying to allow persisting data for my checkboxes and radios. I have attempted to try and do this myself, but I feel I may be missing somthing. I have tried to implement a similar solution I found here but to no avail.

Edit: What I'm asking is; is there a better way to capture persisting data for checkboxes, if so how would I go about it using chk[]. < This is my question not the syntax.

Here's what I have;

PHP / HTML - (located in index.php)

<input type="checkbox" id="chk-1" class="ca" name="chk[]" value="JETvarnish 3D Evo" 
<?php if (in_array('JETVarnish 3D Evo', $chk)) { echo 'checked'; } ?>
/>
<label for="chk-1">JETvarnish 3D Evo</label>
<br/>

PHP - (located in frmPost.php)

// set post data as array.
  $_SESSION['post-data'] = $_POST;

// checkbox as array.
  $selectedProjects  = 'None';
  if(isset($_POST['chk']) && is_array($_POST['chk']) && count($_POST['chk']) > 0){
      $selectedProjects = implode(', ', $_POST['chk']);
  }

Any assistance I could get with this would be greatly appreciated.

Community
  • 1
  • 1
Lewis
  • 1,945
  • 5
  • 26
  • 52
  • Looks okay to me. Whats' the problem first? What's happening? The question is totally unclear, or there's no question at all. – Praveen Kumar Purushothaman May 03 '16 at 12:07
  • @PraveenKumar my apologies if I wasn't clear. The problem is that I cannot get the code I found to work with my code. I believe its having trouble finding or accesseing the `chk[];` array. If the array is built using `name="chk[]"` I wouldn't need to declare `$chk` as an array at the top of index.php should I? – Lewis May 03 '16 at 12:09
  • My problem is I'm infarmiliar with php arrays, and how to access them etc. I'm scraping by on others examples and kind individuals like yourself. – Lewis May 03 '16 at 12:10
  • Let's assume I put the maximum post data in the POST request and you save all of it to the Session. After 1 hour, I have filled your complete hard disc with garbage. It's also dangerouz because of `unserialize()`. – Daniel W. May 03 '16 at 12:18
  • @DanFromGermany Im not following, please elaborate.? – Lewis May 03 '16 at 12:19
  • 1
    You allow the post data to be saved in the session (`$_SESSION['post-data'] = $_POST;`). When I put 1 MB garbage in the POST request, get a new session, after 1000 request I already use 1 GB. – Daniel W. May 03 '16 at 12:20
  • Correct, but I'm destroying the session after index.php load, Isn't that the correct method, for a simple email form? – Lewis May 03 '16 at 12:22
  • 1
    The session is, depending on the server configuration, not deleted from the hard disc (or memory, it all depends!) immediately after the PHP session gets destroyed. It's just bad practice and not safe to do this. Read also many vulnerabilities regarding `unserialize()` here https://www.google.com/search?q=php+unserialize+vulnerability – Daniel W. May 03 '16 at 12:25
  • @DanFromGermany I'll do a little light reading, thanks for the heads up. :) – Lewis May 03 '16 at 12:28
  • You're misusing the ternary operator, it doesn't make sense. You are basically saying "if X is in array, then 'checked'". See the error? PHP doesn't know what `checked` means so it fails. You obviously want PHP to **echo** it, but you don't imply that anywhere, so you can change the `if` to `echo` instead so the ternary operator will understand it correctly. `` – KEK May 03 '16 at 12:49

1 Answers1

2

The error you're getting is syntax.

Instead of

<?php if (in_array('JETVarnish 3D Evo', $chk)) ? 'checked' : ''; ?>

do

<?php if (in_array('JETVarnish 3D Evo', $chk)) { echo 'checked'; } ?>

I can't guarantee the rest of your mechanism will work.

Alec McGail
  • 1,301
  • 1
  • 8
  • 7
  • That has indeed fixed the error as listed but I have a further question to your answer. To pick up on one of my previous comments 'If the array is built using `name="chk[]"` I wouldn't need to declare `$chk` as an array at the top of `index.php` should I?' – Lewis May 03 '16 at 12:49
  • 1
    nah it should show up in $_POST['chk']. – Alec McGail May 03 '16 at 12:50
  • 1
    to check what's going on, you can put a at the top of your site. This will print all the contents of $_POST and very often helps me to debug – Alec McGail May 03 '16 at 12:51
  • As I thought, so have you any idea why I might be receiving, `Notice: Undefined variable: chk`? – Lewis May 03 '16 at 12:52
  • I've been using `//echo '
    ', print_r($_SESSION), '
    ';`
    – Lewis May 03 '16 at 12:53
  • 1
    Have you considered a client-side solution? Might be simpler. I'll update my answer with an example. – Alec McGail May 03 '16 at 12:58
  • It had crossed my mind, I figured Might as well attempt it in php to further my knowledge. – Lewis May 03 '16 at 13:01
  • hm it's not coming out as fast as I figured and I gotta run to work. sorry I couldn't be of more help – Alec McGail May 03 '16 at 13:02
  • All good bud, I'll keep digging around, I'll stumble across the answer eventually. Thanks for the assistance. – Lewis May 03 '16 at 13:03