dtbonklcs575884485 2013-04-22 11:47
浏览 36
已采纳

如何在不使用Ajax重新加载页面的情况下更新数据库和div

I am very new to programming. I made a page with drop-downs, and by selecting the options I wish to update a field(score) in MySQL database and also in the div of the same page without actually reloading the whole page. Can someone please show me an example of how I can do it with Ajax? -Thanks

while($row=mysql_fetch_assoc($result_arrays)){
        $userid=$row['userid']; 
        $score=$row['score'];
        echo "                      
             <p><div class='user'><a href='#'><b>$userid</b></a></div>
             <p><div class='score'>Score: $score</div>   
             <form action ='' method = 'POST'>
               <select name='userscore' id='user_scores'>
               <option>1</option><option>2</option><option>3</option><option>4</option>
               </select>                   
               <input type='submit' value = 'Update Score' id= 'submit-score'/>
             </form>                         
             <br />
             ";
           }
  • 写回答

4条回答 默认 最新

  • doune1000 2013-04-22 11:54
    关注

    using JQuery

    <script>
    $("#user_scores").change(function () {
      var str = "";
      $("#user_scores option:selected").each(function () {
            str += $(this).text() + ",";
          });
      $.ajax({
      type: "POST",
      url: "some.php",
      data: { values: str }
    }).done(function( msg ) {
      alert( "Data Saved: " + msg );
    });
    })
    .trigger('change');
    </script>
    

    more on http://api.jquery.com/jQuery.ajax/

    some.php is your php file with script.

    remember to bind "change" event after document is ready or your select code is loaded.

    In some.php you should get these values in that way

    $values = explode(",",$_POST['values']);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥15 stata安慰剂检验作图但是真实值不出现在图上
  • ¥15 c程序不知道为什么得不到结果
  • ¥40 复杂的限制性的商函数处理
  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题