Ajax Call in Javascript Function On Button Click

时间:2017-04-21 07:33:49

标签: javascript php jquery html ajax

I am getting an error in this code which says Uncaught ReferenceError: myFunction is not defined at HTMLButtonElement.onclick.I cannot figure out where i am going wrong. Here is my code which contains a while loop which fetches data from the database and $xyz goes to the javascript function called myFunction().

Here is my file:

if($rs->num_rows>0)
{
while($row=$rs->fetch_object())
{

            $xyz=$row->judged_id;

            $my="SELECT DISTINCT first_name,picture from user1 where id='$xyz'";

            $hj=$con->query($my);

            if($hj->num_rows>0)
            {
                while($rz=$hj->fetch_object())
                {
                    echo $name=$rz->first_name;

                    $pic=$rz->picture;


                    echo"<img src='$pic' height=100 width=100>";

                    ?>
                    <button type='button' class='egf' onClick="myFunction('xyz')">Chat</button>
                    <br><br>
    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script>

  <script>
  var xyz=<?php echo $xyz; ?>;

  function myFunction('xyz')
   { 


    $.ajax({
            type: 'GET',
            url: 'chat/public/01_index.php?id2=xyz',
            success: function(data) {

    $('.chat-list').html(data);
    }
    });

    });

    }
  </script>




  <?php

            } 
        }

2 个答案:

答案 0 :(得分:1)

PHP变量$xyz的内容是什么?例如,如果它是一个字符串, 这样:

var xyz=<?php echo $xyz; ?>;

会产生类似以下的JavaScript:

var xyz=ABCDEFG;

哪个无效。相反它应该是:

var xyz = '<?php echo $xyz; ?>';

此外,你的函数定义似乎不对,应该是这样的,因为你不能指定一个字符串作为参数名称:

function myFunction(varNameHere)
{ 
    alert(varNameHere);   
}
<a href="javascript:myFunction('Test')">Click me</a>

此外,您在函数中使用了jQuery ready()函数,它可能不会随时触发。

我认为你要找的是这样的:

function myFunction(xyz)
{ 
    $.ajax({
            type: 'GET',
            url: 'https://httpbin.org/get?q=' + xyz,
            success: function(data) {
                $('.chat-list').html(data.args.q);
            }
    });

}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<a href="javascript:myFunction('stackoverflow')">Click me</a>
<div class="chat-list">

</div>

答案 1 :(得分:0)

首先:你不需要jQuery。 jQuery库只是一个用本机javascript编写的包装器。使用原生javascript会更高效。

之后我在评论中提到过,你应该在父元素上使用click事件监听器,其中包含所有按钮。请看下面的例子。

<div class="parent-element">
<?php
while ($row = $resource->fetch_object())
{
    echo '<button id="' . $row->id . '">' . $row->title . '</button>;
}
?>
</div>

<script>
    var parent = document.querySelector('.parent-element'),
        xhr = new XMLHttpRequest();

    parent.addEventListener('click', function( event ) {
        let target = event.target;
        if (target.tagName.toLower() == 'button' && target.id) {
            // your xml http request goes here
            xhr.open('GET', yoururl + target.id);
            xhr.onload(function() {
                if (xhr.status === 200) {
                    // do something with the response
                    let response = xhr.responseText;
                }
            });
            xhr.send();
        }
    }, true);
</script>

while循环将按钮添加到HTML标记中。此外,还将一个click事件侦听器添加到父div元素中。如果目标元素是按钮并且此按钮具有id属性,则每次单击它都会检查。如果是这样,则执行异步请求。