0

I have one simple form which has: From date, To date When i click on submit i get list of car bookings on ajax load. In each row there is a checkbox to select that row and after multiple selection of row a common button is clicked to add bill on next page. Now my problem is I want to select multiple rows and again fill from date, to date and then submit form to get other list to again select other rows to add bill, now the problem is when I again submit form the old selected rows does not get saved so that i can get all rows selected all together on bill page. Right now below snippets are all working correctly but i have no idea how to do this when form is filled again and selection is done. Below code is only doing selection of current page. 1. This is ajax script

<script> $("#button").click(function(){
document.getElementById("loading").style.display = "block"; 
document.getElementById("overlay").style.opacity = '0.2';
var toDate = $("#datepicker2").val();
var fromDate = $("#datepicker").val();  
// var comp = $("#comp").val();
var dataString = 'fromDate='+ fromDate + '&toDate='+ toDate;
$.ajax({
  url: 'CloseBookingload.php',
  data:dataString,
  type: 'POST',
  success: function(result){
          // console.log(result);
           $("#load").show();
          $("#resultData").html(result);
           document.getElementById("loading").style.display = "none"; 
           document.getElementById("overlay").style.opacity = '1';
        }
      }); });</script>

This is my ajax page checkbox field after loads

 <input type="checkbox" id="checkselect" name="checkselect[]" value="<?php echo $row['car_booking_id'];?>">

This where checkbox values are fetched

    $checkselect=array();
if(!empty($_POST['checkselect']))
{
    $checkselect=$_POST['checkselect']; 
    $nc = count($checkselect);
    for($i=0;$i<$nc;$i++)
    {
    $dids = $checkselect[$i];
    }
    $ids = join(",",$checkselect);
    $booking_car = $db->prepare("SELECT * FROM `car_booking`
    left join driver_master on driver_master.driver_id = car_booking.driver_id
    left join vendor_master on vendor_master.vendor_id=car_booking.vendor_name
    left join company_master on company_master.company = car_booking.company
    inner join city_master on city_master.city_id=car_booking.action
    WHERE booked_status=1 and car_booking.slip=1 and car_booking.car_booking_id in ($ids)  order by  str_to_date( booking_date,'%d/%m/%Y') asc ");
    $booking_car->execute();
    $MyArr=$booking_car->fetchall();
    // print_r($MyArr);
    // die;
    $booking_car2 = $db->prepare("SELECT * FROM `car_booking`
    left join driver_master on driver_master.driver_id = car_booking.driver_id
    left join vendor_master on vendor_master.vendor_id=car_booking.vendor_name
    left join company_master on company_master.company = car_booking.company
    inner join city_master on city_master.city_id=car_booking.action WHERE booked_status=1 and car_booking.slip=1 and car_booking.car_booking_id in ($ids) order by  str_to_date( booking_date,'%d/%m/%Y') asc ");
    $booking_car2->execute();
    $r2=$booking_car2->fetch();
}

for($i=0;$i<count($MyArr);$i++){

    $NewIDArr[$i][0]=$MyArr[$i]['car_booking_id'];
}
$Mixed = $NewIDArr;
$r = json_encode($Mixed);
$RequestText = urlencode($r);
?>
    <input type="hidden" name="RequestText" value="<?php echo $RequestText; ?>"> 
    <input type = "submit" name = "update" value = "Update">

First Page IMAGE Main Page IMAGE

The page where actual values are fetched and updated

<?php
                require_once '../dbconfig.php';
                $r = urldecode($_REQUEST['RequestText']);
                $Mixed = json_decode($r);
                $bill=$_POST['bill'];
                $remark=$_POST['remark'];

                //$RequestText = $_POST['RequestText'];
                $singleArray = []; 
                foreach ($Mixed as $childArray) 
                { 
                    foreach ($childArray as $value) 
                    { 
                    $singleArray[] = $value; 
                    } 
                }


                // print_r($singleArray);
                // print_r( $bill);
                // print_r($remark);
                // die;7615, 7631

                    $nc = count($singleArray);
                    //print_r($nc);
                        for($i=0;$i<$nc;$i++)
                        {
                        $did = $singleArray[$i];
                        $data=array('bill_no'=>$bill,'remark'=>$remark);
                        $where = array('car_booking_id' =>$did);    
                        $update = $db->update('car_booking',$data,$where)- 
                       >affectedRows();

                        }
                        header("Location:close_booking.php");
                            //echo "Updated";

    ?>
swe
  • 1
  • 4
  • Are you doing anything to save the check state of the checkboxes to re-populate them? – tshimkus Jan 29 '19 at 05:57
  • No i have simple array of checkbox checkselect[] i want to save those selected values across all pages – swe Jan 29 '19 at 06:04
  • Since you are already using PHP my thought is to use PHP sessions to hold an array of all the checkboxes with a checked status (by its "value" attribute). You could then recall the session on the page with the checkboxes and repopulate them using something like this: https://stackoverflow.com/questions/10930048/get-checkbox-with-specific-value – tshimkus Jan 29 '19 at 06:10
  • Thanks for helping i will try this now. :) – swe Jan 29 '19 at 06:12
  • Or, skip the jQuery part of that example and just do something like this : `>` – tshimkus Jan 29 '19 at 06:18
  • Is there anymore code involved to perform the actual saving? The current code obviously does not save anything. Additionally, have you checked where the problem lies - in sending the proper data to the backend, or in handling it properly? – Nico Haase Jan 29 '19 at 08:53
  • I have tried this but it is showing error in syntax. – swe Jan 29 '19 at 08:54
  • Yes There is code for saving mulitple values of selected rows by checkbox – swe Jan 29 '19 at 08:55
  • I have added more code to this so that i can explain my problem. I have to refresh by ajax to get another list to select... so when after all selections are done it only gets current page selected rows and not all together. – swe Jan 29 '19 at 09:14
  • @tshimkus the code is showingb some error in syntax – swe Jan 29 '19 at 10:10
  • Got any specifics? Did you include `` at the very top of the page before any whitespace? The PHP session only works if the `session_start()` method is called before any DOM rendering. If there is even a single byte or space before the call it will not work. – tshimkus Jan 29 '19 at 10:17
  • You will also have to assign the `$_POST['variable_name']` to `$_SESSION[['variable_name']`. Arrays are allowed, you just need to loop through all the checked checkbox fields and add them to the array. You may also need to remove them from the array as they are posted as unchecked – tshimkus Jan 29 '19 at 10:24
  • @nico-haase yes i have added code for this – swe Jan 29 '19 at 11:39
  • @tshimkus i have tired this thing but when i am again reloading page to select other rows.. it is not getting into bill page.. Only current page selected rows are fetched – swe Jan 30 '19 at 04:35

1 Answers1

0

Now when you will send selected rows in arrays check id from session values of closed bookings and do your manipulation without hitting again and proceed for bill page. Create your session with closed bookings before sending response back to client server and store like as below:-

Array
(
[closed_bookings] => Array
    (
        [123] => Array
            (
                [name] => abc
                [company_name] => abc pvt ltd
                [id] => 123
            )

        [1234] => Array
            (
                [name] => xyz
                [company_name] => abc pvt ltd
                [id] => 1234
            )

    )

)

Selected Rows values will be like..

Array
(
[0] => 123,
[1] => 1234
)

Now use foreach Loop with selected rows and get values from closed bookings with that key(value of selected value ex 123) and do you manipulation.

SINGH
  • 397
  • 1
  • 4
  • 16