I want to run two ajax functions on click event of checkbox.
When I do implement second second ajax functions, first one does not runs.

Here is the code
Check Box

<input type="checkbox" value="<?php if(isset($_POST['inactive_check'])) echo $_POST['inactive_check']; ?>" [B]onclick="disablebatch(); selectinactivecourse(this.checked); allbatches(this.value); "[/B] id="inactive_check" name="inactive_check" <?php if(isset($_POST['inactive_check'])) echo "checked";?>></div><div style=" float:left; padding-top:2px; padding-left:5px;">

In the bold, its ok to call more two AJAX functions ??

6 Years
Discussion Span
Last Post by diafol

well, I see three function calls bolded, but what you need to do is call just ONE function and from that function you do your first ajax call. On that first ajax call, you should have a callback function - the function that executes upon completion of the ajax request. WITHIN THAT callback function call your other ajax function - ex:

function clickHandler(cb){
  //assuming that the following two are making the ajax calls
  //then calling these sequentially will still give you problems
  //  selectinactivecourse(cb.checked); 
  //  allbatches(cb.value);
  //instead call the first one, passing the checkbox reference
  //(intead of just true/false):

function selectinactivecourse(checkBox)
  //here you checkBox.checked will evaluate to true or false
  //depending on the state of the checkbox. So by passing the reference
  //to the checkbox you still can get the needed info you had previously
  var isChecked=checkBox.checked;

   //doyour ajax stuff
  //here you have your anonymous callback function
     if( xhr.readyState==4)

<input type="checkbox" value="<?php if(isset($_POST['inactive_check'])) echo $_POST['inactive_check']; ?>" onclick="clickHandler(this)" id="inactive_check" name="inactive_check" <?php if(isset($_POST['inactive_check'])) echo "checked";?>></div><div style=" float:left; padding-top:2px; padding-left:5px;">

You can use jQuery to ease the pain of ajax calls. Also, writing inline js is to be discouraged. Much better to put a listener into the head or an external file.

Also there seems to be a lot of 'inline php', easier to process all the $_POST stuff above the DTD and apply simply later on. Just tidies up your html, it doesn't change the functionality:

$ic = "...";//default value for checkbox
  $ic = $_POST['inactive_check'];
  //this should be validated and cleaned for use in the form - you can't trust this
  $ic_chk = ' checked = "checked"';
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.6.1/jquery.min.js"></script>
<script src="js/myajax.js"></script> <!-- your ajax file with all your functions -->
<script type="text/javascript">
      alert('do what you want here'); 
      /* call functions */
<input type="checkbox" value="<?php echo $ic;?>" id="inactive_check" name="inactive_check"<?php echo $ic_chk";?> />
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.