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.

5
  • 1
    you can use javascript in href, just like <a href="javascript:jobRespond(<?=$myjobs['id_job'];?>);" class="btn btn-default">Reject And Delete</a>)" Commented Nov 21, 2016 at 11:20
  • will that work with out refreshing the page and how can i get the respone whether it is success or not Commented Nov 21, 2016 at 11:23
  • 2
    @CrisimIlNumenoreano you can, but please don't. It's not the 90s any more. Use unobtrusive event handlers Commented Nov 21, 2016 at 11:24
  • @RoryMcCrossan i'm going OT, but read this Commented Nov 21, 2016 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 Commented Nov 21, 2016 at 11:41

3 Answers 3

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 .

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

Comments

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.

2 Comments

Delegated-events ? Why ?
Right, updated my answer, thanls for your intervention.
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
        }
    });
}

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.