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";

    ?>
14
  • Are you doing anything to save the check state of the checkboxes to re-populate them? Commented Jan 29, 2019 at 5:57
  • No i have simple array of checkbox checkselect[] i want to save those selected values across all pages Commented Jan 29, 2019 at 6: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: stackoverflow.com/questions/10930048/… Commented Jan 29, 2019 at 6:10
  • Thanks for helping i will try this now. :) Commented Jan 29, 2019 at 6:12
  • Or, skip the jQuery part of that example and just do something like this : <input type="checkbox" id="checkselect" name="checkselect[]" value="<?php echo $row['car_booking_id'] . '"'; if (in_array($row['car_booking_id'], $_SESSION['checked_checkbox_values'] )) { echo "checked"; } ?>> Commented Jan 29, 2019 at 6:18

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.