vikaspa Posted December 29, 2022 Share Posted December 29, 2022 Dear All I am accepting following 2 field in a form one is checkbox and another is input <input name="weekday[]" type="checkbox" value="<?php echo $rows['id'];?>" class='weekday' /> <?php echo $rows['name'] ;?> <br>Timing <input type="text" name="timing[]" ' class='timepicker ' /> the form look like this (refer image) after submit i can get checkbox value After submit I tried $cntr=count($_POST['weekday']); for($i=0; $i < $cntr=count($_POST['weekday']) ; $i++) { echo "<br>".$i." Loop Week day =>". $_POST['weekday'][$i]." timing =>". $_POST['timing'][$i]; // Addcenterdaystime($staffcompanyid, $newid, $_POST['weekday'][$i], $_POST['timing'][$i] ) ; } However could not get values of timing pls help Quote Link to comment https://forums.phpfreaks.com/topic/315740-accept-input-with-dimensions-and-store-in-database/ Share on other sites More sharing options...
mac_gyver Posted December 29, 2022 Share Posted December 29, 2022 only checked checkboxes will be set. if no checkboxes are checked, $_POST['weekday'] won't be set and any reference to it will produce a php error. you must test if $_POST['weekday'] isset() before referencing it. next, to associate each timing[] field to the corresponding weekday[] field, you should use the id as the array index for both fields, and forget about using a value attribute for the checkboxes. after you have determined that $_POST['weekday'] isset(), you can get the ids/keys (see php's array_keys()), then loop over the ids and access the corresponding timing[] field values. Quote Link to comment https://forums.phpfreaks.com/topic/315740-accept-input-with-dimensions-and-store-in-database/#findComment-1604001 Share on other sites More sharing options...
vikaspa Posted December 29, 2022 Author Share Posted December 29, 2022 I really appreciate prompt reply As specified by you I am getting values for $_POST['weekday'][$i] code do { //reading weekday table and assigning id as index ?> <tr> <td class='text15' > <input name="weekday[<?php $rows['id'];?>]" type="checkbox" value="<?php echo $rows['id'];?>" class='weekday' /> <?php echo $rows['name'] ;?> <br>Timing <input type="text" name="timing[<?php $rows['id'];?>]" class='timepicker timing' /> </td> </tr> <?php } while ($rows=$query->fetch()); ?> after submit $cntr=count($_POST['weekday']); echo "weekday count=".count($_POST['weekday']); echo "timing count=".count($_POST['timing']); for($i=8; $i < 18 ; $i++) { echo "<br>".$i." Loop Week day =>". $_POST['weekday'][$i]." timing =>". $_POST['timing'][$i]; } output weekday count=5 8 Loop Week day => timing => 9 Loop Week day =>9 timing => 10 Loop Week day =>10 timing => 11 Loop Week day =>11 timing => 12 Loop Week day =>12 timing => 13 Loop Week day =>13 timing => 14 Loop Week day => timing => 15 Loop Week day => timing => 16 Loop Week day => timing => 17 Loop Week day => timing => We get the values for weekday[9], weekday[10], weekday[11], weekday[12], weekday[13] ------------------------------------------------------------ but not getting any values for timing[9], timing[10], timing[11], timing[12], timing[13] pls help Quote Link to comment https://forums.phpfreaks.com/topic/315740-accept-input-with-dimensions-and-store-in-database/#findComment-1604013 Share on other sites More sharing options...
mac_gyver Posted December 29, 2022 Share Posted December 29, 2022 almost none of that is what i wrote, and you must echo the id in the field array index for it to be output (check the view source of the output in your browser.) see the following example (uses made up checkbox data instead of sql query data) - // for debugging, examine the input data echo '<pre>'; print_r($_POST); echo '</pre>'; // post method form processing if($_SERVER['REQUEST_METHOD']==='POST') { // test if any checkbox is checked if(isset($_POST['weekday'])) { // get and loop over the checked checkbox ids foreach(array_keys($_POST['weekday']) as $id) { // access the timing field value echo "Weekday id: $id, Timing: {$_POST['timing'][$id]}<br>"; } } } // make up some checkbox data $days = []; $days[] = ['id'=>1,'name'=>'Monday']; $days[] = ['id'=>2,'name'=>'Tuesday']; $days[] = ['id'=>3,'name'=>'Wednesday']; $days[] = ['id'=>4,'name'=>'Thursday']; $days[] = ['id'=>5,'name'=>'Friday']; ?> <form method='post'> <button type='submit'>Submit</button><br> <?php foreach($days as $row) { $chk = isset($_POST['weekday'][$row['id']]) ? 'checked' : ''; ?> <input name="weekday[<?=$row['id']?>]" type="checkbox" <?=$chk?> class='weekday'> <?=$row['name']?> <br>Timing<br> <input type="text" name="timing[<?=$row['id']?>]" value="<?=$_POST['timing'][$row['id']]??''?>" class='timepicker'><br> <?php } ?> </form> Quote Link to comment https://forums.phpfreaks.com/topic/315740-accept-input-with-dimensions-and-store-in-database/#findComment-1604014 Share on other sites More sharing options...
vikaspa Posted December 29, 2022 Author Share Posted December 29, 2022 This solved the problem Thanks a ton Quote Link to comment https://forums.phpfreaks.com/topic/315740-accept-input-with-dimensions-and-store-in-database/#findComment-1604016 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.