1

Here my dynamic script:

<a href="inv-controller.php?res=yes&id_job=<?=$myjobs['id_job'];?>" class="btn btn-primary">Accept And Send Invite To All Students</a>
<a href="inv-controller.php?res=no&id_job=<?=$myjobs['id_job'];?>" class="btn btn-default">Reject And Delete</a>

From the above two links there are two dynamic parameters which i want to send using ajax call how can i do that..?

There are two buttons there Accept and reject. i can do this with core php and i want to do this without refreshing the page.

Here what i have tried.

<script type="text/javascript">
    function jobResponse(jobId){
        var jobid = $(jobId).attr('id');  // im confusing in this line
        $.ajax({
            method: "POST",
            url: 'inv-controller.php',
            data: {action: "jobreplay", value: jobid},
            dataType: "json",
            success: function(response) {
                //blah blah blah
            }   
        });
    }
</script>       

how can i rewrite the two <a> tags according to ajax.

Veerendra
  • 2,562
  • 2
  • 22
  • 39
Mr world wide
  • 4,696
  • 7
  • 43
  • 97
  • 1
    you can use javascript in href, just like `Reject And Delete)"` –  Nov 21 '16 at 11:20
  • will that work with out refreshing the page and how can i get the respone whether it is success or not – Mr world wide Nov 21 '16 at 11:23
  • 2
    @CrisimIlNumenoreano you can, but please don't. It's not the 90s any more. Use unobtrusive event handlers – Rory McCrossan Nov 21 '16 at 11:24
  • @RoryMcCrossan i'm going OT, but read [this](http://stackoverflow.com/questions/1070760/javascript-function-in-href-vs-onclick) –  Nov 21 '16 at 11:40
  • @CrisimIlNumenoreano That just backs up my original point - JS in `href` or `on*` attributes is outdated and you should use unobtrusive handlers instead - regardless of if you use jQuery or any other library – Rory McCrossan Nov 21 '16 at 11:41

3 Answers3

3
<a href="javascript:;" class="btn btn-primary" onclick="jobResponse("yes",<?php echo $myjobs['id_job'];?>)">Accept And Send Invite To All Students</a>
<a href="javascript:;" class="btn btn-default" onclick="jobResponse("no",<?php echo $myjobs['id_job'];?>)">Reject And Delete</a>

and get in function like this

function jobResponse(type,jobId){

var frm_data = { type : type,
                 jobId : jobId
                }
    $.ajax({
        method: "POST",
        url: 'inv-controller.php',
        data: frm_data,
        dataType: "json",
        success: function(response) {
            //blah blah blah
        }   
    });
}

you can get in your ajax php file. frm data as post .

Amit Chauhan
  • 682
  • 7
  • 22
2

It'll be better to use data-* attributes like :

<a href="inv-controller.php" data-res="yes" data-job-id="<?=$myjobs['id_job'];?>" class="btn btn-primary inv-controller">Accept And Send Invite To All Students</a>
<a href="inv-controller.php" data-res="no" data-job-id="<?=$myjobs['id_job'];?>" class="btn btn-default inv-controller">Reject And Delete</a>

Then attach click event to the links a with class inv-controller :

$('a.inv-controller').on('click', function(e){
  e.preventDefault();

  var res = $(this).data('res');
  var job_id = $(this).data('job-id');
  var url = $(this).attr('href');

  $.ajax({
      method: "POST",
      url: url,
      data: {action: "jobreplay", value: job_id},
      dataType: "json",
      success: function(response) {
          //blah blah blah
      }   
  });
})

Hope this helps.

Zakaria Acharki
  • 66,747
  • 15
  • 75
  • 101
1

You Can do like this , Look at the parameter jobId passed to it in value: jobId

     <a href="javascript:jobResponse(<?=$myjobs['id_job'];?>)" class="btn btn-primary">Accept And Send Invite To All Students</a>


<script type="text/javascript">
function jobResponse(jobId) {
    $.ajax({
        method: "POST",
        url: 'inv-controller.php',
        data: { action: "jobreplay", value: jobId },
        dataType: "json",
        success: function (response) {
            //blah blah blah
        }
    });
}

Vaibhav shetty
  • 372
  • 1
  • 4
  • 15