dprxj1995 2015-12-14 18:06
浏览 53
已采纳

jQuery AJAX函数没有做任何事情

I have a form and I want to insert the value of this form as soon as someone clicks a radio button. No better excuse than that I am not good at jQuery at all. This is what I've got so far.

HTML:

<form>
    <input type="radio" checked="true" id="q1r1" name="q1" value="Awesome">
    <label class="button1" for="q1r1">Awesome</label>

    <input type="radio" id="q1r2" name="q1" value="Ok">
    <label class="button2" for="q1r2">Ok</label>

    <input type="radio" id="q1r3" name="q1" value="Awful">
    <label class="button3" for="q1r3">Awful</label>
</form>

get.JSON:

$('#q1r1').on('click',function(){
    var get_var_name = $(this).val();
        $.getJSON('json/question1.php?team=<?php echo $teamid ?>',{q1:get_var_name},function(data,status){
            console.log(data);
            if(status == 'success'){
                console.log(data);
            } else {
                alert('Status: ' + status);
            }
            });
        });

...json/question1.php:

<?php
    require('db_connect.php');
    session_start();
    $uid = $_SESSION['uid'];

    if($_GET['q1']) {

        $teamid = $_GET['team'];
        $insertq1 = $_GET['q1'];

        $sql = "UPDATE result 
        SET q1='$insertq1'
        WHERE userid='$uid' AND teamid='$teamid' ";

        $result = $mysqli->query($sql);

    }
?>

As you probably see, I'm really no good at this at all, but this is what I had planned to happen:

  1. The logged in user selects an option, Awesome, OK or Awful.
  2. Without the page refreshing or a submit button being clicked, the get.JSON will automatically nudge the question1.php with the values teamid (saved in $teamid) and q1 (the value of the users choice).
  3. The php file silently updates the users database with the values, everybody happy.

Thing is that nothing happens when I click on "Awesome" as of right now, and I don't know why. I've been trying to follow multiple guides, but I'm just not sure how to go about solving this one. Any advice or help is much appreciated.

Kind regards, Mo.

P.s!

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script> 

is included.

  • 写回答

1条回答 默认 最新

  • dphj737575 2015-12-14 18:51
    关注

    just add onchange() in your input(radio type in this case) to call a javascript function that get data from url with no need to refresh the page, i've modified your code and tested it with a php of my own.

    your html must be like

      <form>
          <input type="radio" checked="true" id="q1r1" name="q1" value="Awesome" onchange="GrabData(this)">
          <label class="button1" for="q1r1">Awesome</label>
    
          <input type="radio" id="q1r2" name="q1" value="Ok" onchange="GrabData(this)">
          <label class="button2" for="q1r2">Ok</label>
    
          <input type="radio" id="q1r3" name="q1" value="Awful" onchange="GrabData(this)">
          <label class="button3" for="q1r3">Awful</label>
      </form>
    

    then you can either write this javascript code inside the html by adding

    <script>.......</script>
    

    or put it in another file .js (dont forget to call it from you html page)

    JS CODE-->

    function GrabData(Passeddata){
      var radioValue = Passeddata.value;
      var URL = "json/question1.php?q1="  + radioValue + "&teamid="<?php echo $teamid; ?>;
      $.ajax( {
          url : URL,
          type : "GET",
          dataType: 'json',
          success : function(data) {
            if (data == "") {
              alert("data is empty");
            }else{
              console.log('got your data back sir');
            }
          }
      });
    }
    

    please let me know if it worked or if there is some other problems

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 GDI处理通道视频时总是带有白色锯齿
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)
  • ¥15 自适应 AR 模型 参数估计Matlab程序
  • ¥100 角动量包络面如何用MATLAB绘制
  • ¥15 merge函数占用内存过大
  • ¥15 Revit2020下载问题
  • ¥15 使用EMD去噪处理RML2016数据集时候的原理
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大
  • ¥15 单片机无法进入HAL_TIM_PWM_PulseFinishedCallback回调函数