1

i have MySQL looping, when i click second button to get their id and do rest process it not working. why this happen?

$(document).ready(function() {
$("#deleteSchedule").click(function (e) {
    e.preventDefault();
    var url_tna = "<?php echo $_SESSION['url_address'];?>mods/agent_management/code/";
    var deleteSchedule =$("#deleteSchedule").val(); //build a post data structure

    var JsonData = {deleteSchedule: deleteSchedule};  

    jQuery.ajax({
        type: "POST", // Post Get method
        url: url_tna+"email_template_response.php", //Where form data is sent on submission
        dataType:"text", // Data type, HTML, json etc.
        data:JsonData, //Form variables
        success:function(response){
            window.location.reload(true); // reload before append
            $("#responds").append(response);
            document.getElementById("contentText").value = "";
        },
        error:function (xhr, ajaxOptions, thrownError){
            alert(thrownError);
        }
    });
});
});
</script>



<?php $i=1; while ($row = mysql_fetch_array($result)) { ?>

             <tr>

                <td><?php  echo $i; ?></td> 
                     <td><input type="textarea" id="TitleData" name="TitleData"  value="<?php echo $row['niceDate']; ?>" ></input></td> 
                     <td><button id="deleteSchedule" name="deleteSchedule" class="del_button" value="<?php echo $row['id'];?>"><a href="#" style="display:block" class="button insertcolumn" id="<?php echo $row['id'];?>">Delete</a></button></td>
                    <? $i++;}?>
                </tr>   
        </table>

only first button work

15
  • 4
    ID of an element must be unqiue - the ID selector will return only the first element with the said id... that is why only the first element is working... use class instead of ID to group similar elements Commented Oct 9, 2015 at 3:29
  • you cannot have same id multiple times in a page. Commented Oct 9, 2015 at 3:30
  • you can add the `variable i to the id to make it unique Commented Oct 9, 2015 at 3:31
  • 1
    change $("#deleteSchedule"). to $(".del_button"). then var deleteSchedule =$("#deleteSchedule").val(); to var deleteSchedule =$(this).val(); Commented Oct 9, 2015 at 3:35
  • 1
    if the class del_button is used for other purposes also then add an additional class to the button like <button name="deleteSchedule" class="del_button deleteSchedule" then $(".deleteSchedule").click(....) Commented Oct 9, 2015 at 3:37

1 Answer 1

1

You need to use class instead of ID in your script.

In your Js:

$(document).ready(function() {
$(".deleteSchedule").click(function (e) {
    e.preventDefault();
    var url_tna = "<?php echo $_SESSION['url_address'];?>mods/agent_management/code/";
    var deleteSchedule =$(this).val(); //build a post data structure

    var JsonData = {deleteSchedule: deleteSchedule};  

    jQuery.ajax({
        type: "POST", // Post Get method
        url: url_tna+"email_template_response.php", //Where form data is sent on submission
        dataType:"text", // Data type, HTML, json etc.
        data:JsonData, //Form variables
        success:function(response){
            window.location.reload(true); // reload before append
            $("#responds").append(response);
            document.getElementById("contentText").value = "";
        },
        error:function (xhr, ajaxOptions, thrownError){
            alert(thrownError);
        }
    });
});
});
</script>

In your PHP:

<?php $i=1; while ($row = mysql_fetch_array($result)) { ?>

             <tr>

                <td><?php  echo $i; ?></td> 
                     <td><input type="textarea" id="TitleData" name="TitleData"  value="<?php echo $row['niceDate']; ?>" ></input></td> 
                     <td><button name="deleteSchedule" class="del_button deleteSchedule" value="<?php echo $row['id'];?>"><a href="#" style="display:block" class="button insertcolumn" id="<?php echo $row['id'];?>">Delete</a></button></td>
                    <? $i++;}?>
                </tr>   
        </table>
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.