【问题标题】:Radiobutton value to mysql without submit单选按钮值到mysql而不提交
【发布时间】:2013-04-29 11:05:07
【问题描述】:

我绝不是 javascript 程序员,所以如果有人能帮我解决这个问题,那就太好了。如果有多个这些评级部分,如下例所示,它们位于同一页面上。

现在我需要将被点击的单选按钮的值发送到我的数据库,而不需要提交按钮,最好不要刷新。

<body>
    <section class="rating">
        <input type="radio" class="radio twenty" name="progress" value="twenty" id="twenty" <?php if($num === 1) { echo 'checked'; } ?>>
        <label for="twenty" class="label">1</label>

        <input type="radio" class="radio fourty" name="progress" value="fourty" id="fourty" <?php if($num === 2) { echo 'checked'; } ?>>
        <label for="fourty" class="label">2</label>

        <input type="radio" class="radio sixty" name="progress" value="sixty" id="sixty" <?php if($num === 3) { echo 'checked'; } ?>>
        <label for="sixty" class="label">3</label>

        <input type="radio" class="radio eighty" name="progress" value="eighty" id="eighty"<?php if($num === 4) { echo 'checked'; } ?>>
        <label for="eighty" class="label">4</label>

        <input type="radio" class="radio onehundred" name="progress" value="onehundred" id="onehundred" <?php if($num === 5) { echo 'checked'; } ?>>
        <label for="onehundred" class="label">5</label>

        <div class="progress">
            <div class="progress-bar"></div>
        </div>
    </section>
</body>

我已经设置了对选中单选按钮的检索,使其对应于 1 到 5 的评分,所以我目前正在使用 testdata 来显示 1 到 5 的评分。

我不一定需要所有细节,需要完成的步骤的概述已经非常有帮助了。

【问题讨论】:

    标签: php javascript jquery mysql ajax


    【解决方案1】:

    应该这样做

    DEMO

    在 jQuery 包含后添加到头部 我将类更改为进度条的 id

    $(function() { // when document has loaded
      $(".radio").on("click",function() { // all radios with class="radio ..." 
        // ajax get or post someserver.php?value=twenty/fourty...
        $.get("someserver.php", {value:this.value},function(data) { 
          // add the returned value to a container
          $("#progress-bar").append("Updated "+ data); 
        });
      });
    });
    

    【讨论】:

    • 我想说“做改变”,但后来意识到一切都会触发
    • 那么如果有&lt;a href="example.com" class="radio"&gt;...&lt;/a&gt;,这不会发送example.comnullsomeserver.php 吗?
    • 是的,您要么不需要将类 radio 赋予其他项目,要么将 input[type=radio] 或 input[name=progress] 添加到选择器
    • 非常感谢,我已经稍微改变了它以使用 laravel 控制器,但它就像一个魅力。再次感谢您!
    【解决方案2】:

    您可以使用 AJAX。如果您使用的是 Jquery,则可以使用点击处理程序来检测您的元素点击并启动一个函数调用,该函数调用进而可以调用 AJAX 函数与您的服务器端代码交互并将值发送到数据库中。

    【讨论】:

    • 再详细一点会很有帮助哈哈。我目前正在从 lynda.com 下载 javascript 和 ajax 教程。我目前甚至不知道要采取的确切步骤。
    【解决方案3】:
    • 将 onclick 或 onchange 操作添加到使用适当参数发送 ajax 请求的输入中
    • 接收请求,验证数据
    • 插入/更新行

    【讨论】:

      猜你喜欢
      • 2013-04-10
      • 2015-09-19
      • 2020-11-13
      • 1970-01-01
      • 2018-05-12
      • 1970-01-01
      • 2016-11-22
      • 2011-04-20
      • 2015-06-30
      相关资源
      最近更新 更多