简体   繁体   中英

ajax function not opening a page

i don't understand why ajax is not working. my code:

<script type="text/javascript" src="js/jquery-1.9.1.js"></script>
    <script type="text/javascript">
    function edit_row(id)
    {
        $.ajax({
            method:'get',
            url:'form.php',
            success:function(data)
            {
              $('#form_div').html(data);
            }
        });

    }
<?php
   echo '<td style='.$style.'>'.$status.'<a href="" title="Edit"  onClick=edit_row('.$data['type_id'].')><img src="images/pencil.png" width="30px" height="30px"></a></td></tr>';
?>

its not opening form.php onclick what is the problem please help me!!!

You click the link. The JavaScript runs. The link is followed. A new page (with the same URL because you have href="" ) loads. The JavaScript stops because its environment has gone away.

Use a button instead.

you have to put

href="javascript:void(0)"

instead of

href=""

else it will reload the page without calling the ajax

You are not passing the 'id' to you form.php I guess you need to pass it:

<script type="text/javascript" src="js/jquery-1.9.1.js"></script>
    <script type="text/javascript">
    function edit_row(id)
    {
        $.ajax({
            method:'get',
            url:'form.php',
            data: {id: id },
            success:function(data)
            {
              $('#form_div').html(data);
            }
        });

    }
</script>

I would do something like this (does not use onClick attribute):

<script type="text/javascript" src="js/jquery-1.9.1.js"></script>
<script type="text/javascript">
    $(function() {
        $('a.edit').click(function(e) {    // or: $.on('click', 'a.edit', function(e) {
            e.preventDefault();
            var id = $(this).attr('id');
            $.ajax({
                method:'get',
                url: 'form.php',
                data: {id:id},
                success: function(data) {
                    $('#form_div').html(data);
                }
        });
    });
</script>

<?php
    echo '<td style="$style">$status
          <a href="#" id="' . $data['type_id'] . '"  title="Edit" class="edit">
          <img src="images/pencil.png" width="30px" height="30px">
          </a></td></tr>';
?>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM