【问题标题】:Getting radio button value and sending through ajax to php获取单选按钮值并通过 ajax 发送到 php
【发布时间】:2013-03-26 17:08:10
【问题描述】:

我的网站上有一个投票,每个答案旁边都会显示单选按钮。当用户选择一个选项并提交时,我通过 ajax 运行一个 php 脚本以将值或选定的单选按钮插入到表中。

我的 Ajax 正在运行,但当前每行插入一个 0 行,因此它没有从单选按钮中获取值。任何帮助将不胜感激。

HTML:

<form id="poll_form" method="post" accept-charset="utf-8">  
    <input type="radio" name="poll_option" value="1" id="poll_option" /><label for='1'>&nbsp;Arts</label><br />
    <input type="radio" name="poll_option" value="2" id="poll_option" /><label for='2'>&nbsp;Film</label><br />
    <input type="radio" name="poll_option" value="3" id="poll_option" /><label for='3'>&nbsp;Games</label><br />
    <input type="radio" name="poll_option" value="4" id="poll_option" /><label for='4'>&nbsp;Music</label><br />
    <input type="radio" name="poll_option" value="5" id="poll_option" /><label for='5'>&nbsp;Sports</label><br />
    <input type="radio" name="poll_option" value="6" id="poll_option" /><label for='6'>&nbsp;Television</label><br />    
    <input type="submit" value="Vote &rarr;" id="submit_vote" class="poll_btn"/> 
</form> 

AJAX:

    $("#submit_vote").click(function(e)
    { 
    var option=$('input[type="radio"]:checked').val();
    $optionID = "="+optionID;

    $.ajax({
        type: "POST",
        url: "ajax_submit_vote.php",
        data: {"optionID" : $optionID}
    });
});

PHP:(缩短版)

    if($_SERVER['REQUEST_METHOD'] == "POST"){

    //Get value from posted form
    $option = $_POST['poll_option'];

    //Insert into db
    $insert_vote = "INSERT into poll (userip,categoryid) VALUES ('$ip','$option')";

提前致谢!

【问题讨论】:

  • data: {"optionID" : $optionID} 应该是data: {optionID : $optionID}
  • 您是否从选项变量中获得了正确的值? If not try $('input[type="radio"]['name=poll_option']:checked').val(); 注意在 type="radio" 之后新添加的 ['name-poll_option']

标签: php jquery html ajax radio-button


【解决方案1】:
var option = $('input[type="radio"]:checked').val();

$.ajax({
    type: "POST",
    url: "ajax_submit_vote.php",
    data: { poll_option : option }
});

在 PHP 中,您正在阅读 $_POST['poll_option'],因此您必须使用 poll_option 作为数据对象中的键。此外,该值存储在option 而不是您尝试使用的$optionID

$ 是 Javascript 中变量名中的一个有效字符,它本身并没有做任何特殊的事情,但是一些编码人员在任何作为 jQuery 对象的前缀上加上 $,这样他们就可以浏览代码并轻松查看哪些变量已经有了 jQuery 包装器。

例如:

var $option = $('input[type="radio"]:checked'); // $option is the jQuery wrapped HTML element
var myValue = $option.val(); // we know $option already has the jQuery wrapper so no need to use the $(..) syntax.

【讨论】:

    【解决方案2】:
    $("#submit_vote").click(function(e){ 
    
        $.ajax( {
          type: "POST",
          url: "ajax_submit_vote.php",
          data: $('#poll_form').serialize(),
          success: function( response ) {}
        });
    
    });
    

    然后,您应该可以在 PHP 脚本中访问 POST 变量“poll_option”。

    【讨论】:

      【解决方案3】:
        $optionID = "="+optionID;
      

      我不太明白你在这里要做什么 o.O,在 javascript 中你没有使用 $ 定义你的变量。

      data: { optionID : option}
      

      像这样使用它应该可以工作。你可以在 PHP 中这样检索它:

      $option_value=$_POST['optionID'];
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2015-08-23
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-03-23
        • 2012-12-19
        相关资源
        最近更新 更多