duancu4731 2019-04-06 15:29
浏览 54

如果在不加载整个页面的情况下按下另一个按钮,我如何更改按钮的颜色并加载问题?

I am making an online exam website where users can log in give exams.
I have a panel beside the question where different question number button are given if you click on the buttons the question will appear from the database without loading the whole page.I am running into the problem of how do I do that?

THE HTML CODE-

**<div>
<button type="button" class="btn btn-info custom" style="margin-right:16px">13</button>
<button type="button" class="btn btn-info custom" style="margin-right:16px">14</button>
<button type="button" class="btn btn-info custom" style="margin-right:16px">15</button>
</div>**

How to write the jquery ajax and php code?

  • 写回答

1条回答 默认 最新

  • douzhan1868 2019-04-06 15:50
    关注

    I have created php file (test.php) and the client side which executes the ajax call and gets the question according to question id.Code is below

    PHP file code (test.php)

    <?php 
    //it's used because the server is local, without it throws console error
    //and doesn't allow to proceed with the operation.
    //No need for remote servers
    header('Access-Control-Allow-Origin: *');
    
    if(isset($_GET['questionId'])){
        $qId = $_GET['questionId'];
    
        if($qId == 12){
          echo "Question 1";
        }
    
        if($qId == 13){
          echo "Question 2";
        }
    }
    
    ?>
    

    $(".btn > button").click(function(){
        //get button text and send it to server file to give you back response accordingly
        var qId = $(this).text();
        alert("Question ID:"+qId);
        
        /*$.ajax({
           method:'get',
           url:'http://localhost/test.php'
           //send question ID to server file
           data:{questionId: qId }
        }).done(function( data ) {
           console.log( "Sample of data:"+data);
        }).fail(function( jqXHR, textStatus ) {
           alert( "Request failed: " + textStatus );
        });*/
      
    });
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
    <div class="btn">
    <button type="button" class="btn btn-info custom" style="margin-right:16px">13</button>
    <button type="button" class="btn btn-info custom" style="margin-right:16px">14</button>
    <button type="button" class="btn btn-info custom" style="margin-right:16px">15</button>
    </div>

    </div>
    
    评论

报告相同问题?

悬赏问题

  • ¥15 微信小程序协议怎么写
  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看