javascript - How can you pull a PHP value from a table row in JQuery to use later?

969

I have a Button that gets populated with ever row in the table. I want to be able to put the value of the row "$row[auditID]" and use it in my modal to be able to make this delete button work. The alert pops up, but the Modal won't hide and my delete action doesn't work (doesn't delete the audit).

<a href=\"#\" id=\"$row[auditID]\" class='btn btn-danger btn-xs' role='button' data-toggle='modal' data-target='#question_alert' Title=\"Remove\" >Remove</span>  </a>";

<!-- modal pop up for Deleting a audit -->
<div  id="question_alert" role="dialog">
  <div >
    <div >
      <div >
      <button type="button" data-dismiss="modal" aria-hidden="true"></button>
      <h4 >Audit Delete Confirmation</h4>
      </div>
      <div id="myModal1">
        <h3>Are you sure you want to <strong>DELETE</strong> this Audit? You will not be able to get it back.</h3>
      </div>
      <div >
      <form method = "POST">
        <input type="button" id="yes_delete" value="Yes " name="deleteaudit" />
        <button type="button" data-dismiss="modal">No</button>
      </form> 
      </div>
    </div>
  </div>
</div>

JQuery Script:

<script> 
 $("#yes_delete").click(function(){
var auditID = this.id;
//alert("delete test");
    $.post("edit_audits.php?action=delete&auditID="+auditID,{
    function(data){
        alert("Audit Deleted");
        $('question_alert').modal('hide');
        window.location.reload();
        }
        }); 
    }); 
</script>
306

Answer

Solution:

Something like this?

Put your PHP value into the a tag like this:

    <a href="#" data-mylink="<?php echo $yourPHPVar; ?>" class='btn btn-danger  btn-xs' role='button' data-toggle='modal' data-target='#question_alert' Title="Remove" ><span>Remove</span> </a>




150
votes

Answer

Solution:

I would do something like this

<a href="javascript:void(0)" class='btn btn-danger btn-xs btn-remove' data-id="<?=$row["auditID"]?>" role='button' data-toggle='modal' data-target='#question_alert' Title="Remove" >Remove</span></a>

<!-- modal pop up for Deleting a audit -->
<div  id="question_alert" role="dialog">
  <div >
    <div >
      <div >
      <button type="button" data-dismiss="modal" aria-hidden="true"></button>
      <h4 >Audit Delete Confirmation</h4>
      </div>
      <div id="myModal1">
        <h3>Are you sure you want to <strong>DELETE</strong> this Audit? You will not be able to get it back.</h3>
      </div>
      <div >
      <form method = "POST">
        <input type="button" id="yes_delete" value="Yes " name="deleteaudit" />
        <button type="button" data-dismiss="modal">No</button>
      </form> 
      </div>
    </div>
  </div>
</div>

And This in The jQuery function. Make sure you put your jQuery script tag at the top of the page for jQuery to load before this function

<script>
var removeId = null;
$(document).ready(function(){
    $(".btn-remove").click(function(){
        removeId = $(this).attr("data-id");
    });
    $("#yes_delete").click(function(){
        $.ajax({
            url: "edit_audits.php",
            type: "POST",
            data: "action=delete&auditID="+removeId,
            success: function(){
                $('#question_alert').modal('hide');
                window.location.reload();
            }
        });
    }
});

Keep in mind, I have not ran this as I don't have all the data needed. You may have to debug this a little as it was blind coding.




People are also looking for solutions to the problem: using $this when not in object context
Source

Share


Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.


Similar questions

Find the answer in similar questions on our website.

893 javascript - Terminate a php script from another
338 javascript - how to pass a jquery variable to php and then display it
236 php - How can I save values in a array when I have a foreach?
720 How can I read all the the mails (seen/unseen) from a "particular folder" using imap in php?
475 php - Protect your option value
42 javascript - Can the laravel _token be used to avoid submitting a form twice?
812 php - Output three columns and row in loop
781 php - MailChimp sending incorrect user IDs
282 php - Format array from sql query result
438 php - Where can i configure the max_allowed_packet in hostgator or godaddy
150

Answer

Solution:

I would do something like this

<a href="javascript:void(0)" class='btn btn-danger btn-xs btn-remove' data-id="<?=$row["auditID"]?>" role='button' data-toggle='modal' data-target='#question_alert' Title="Remove" >Remove</span></a>

<!-- modal pop up for Deleting a audit -->
<div  id="question_alert" role="dialog">
  <div >
    <div >
      <div >
      <button type="button" data-dismiss="modal" aria-hidden="true"></button>
      <h4 >Audit Delete Confirmation</h4>
      </div>
      <div id="myModal1">
        <h3>Are you sure you want to <strong>DELETE</strong> this Audit? You will not be able to get it back.</h3>
      </div>
      <div >
      <form method = "POST">
        <input type="button" id="yes_delete" value="Yes " name="deleteaudit" />
        <button type="button" data-dismiss="modal">No</button>
      </form> 
      </div>
    </div>
  </div>
</div>

And This in The jQuery function. Make sure you put your jQuery script tag at the top of the page for jQuery to load before this function

<script>
var removeId = null;
$(document).ready(function(){
    $(".btn-remove").click(function(){
        removeId = $(this).attr("data-id");
    });
    $("#yes_delete").click(function(){
        $.ajax({
            url: "edit_audits.php",
            type: "POST",
            data: "action=delete&auditID="+removeId,
            success: function(){
                $('#question_alert').modal('hide');
                window.location.reload();
            }
        });
    }
});

Keep in mind, I have not ran this as I don't have all the data needed. You may have to debug this a little as it was blind coding.

People are also looking for solutions to the problem: string - PHP: replace double quotes

Source

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.