dsjzmrz78089 2017-04-21 07:33
浏览 199

Ajax调用Javascript函数On Button Click

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条回答 默认 最新

  • download1323 2017-04-21 07:42
    关注

    What is the content of the PHP variable $xyz? If it is a string, for example, this:

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

    Would result in a JavaScript like:

    var xyz=ABCDEFG;
    

    Which is not valid. Instead it should then be:

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

    Furthermore your function defintion seems not right, should be something like this instead, since you can not specify a string as a parameter name:

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

    Also you are using jQuery ready() function inside your function, which will probably not fire at anytime.

    I think what you are looking for is something like this:

    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>

    </div>
    
    评论

报告相同问题?

悬赏问题

  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!