获取单选按钮值并通过ajax发送到php

时间:2022-12-05 08:03:16

I have a poll on my website which displays radio buttons next to each answer. When the user selects an option and submits, im running a a php script via ajax to insert the value or the selected radio button into a table.

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

My Ajax is running but is currently inserting a 0 row each row, so it's not picking up the value from the radio button. Any help would be appreciated.

我的Ajax正在运行,但目前每一行都插入了0行,所以它没有从单选按钮获取值。如有任何帮助,我们将不胜感激。

HTML:

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:

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: (shortened version)

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')";

Thanks in advance!

提前谢谢!

3 个解决方案

#1


9  

$("#submit_vote").click(function(e){ 

    $.ajax( {
      type: "POST",
      url: "ajax_submit_vote.php",
      data: $('#poll_form').serialize(),
      success: function( response ) {}
    });

});

You should then have the POST variable "poll_option" accessible in your PHP script.

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

#2


2  

var option = $('input[type="radio"]:checked').val();

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

In the PHP you are reading $_POST['poll_option'] therefore you must use poll_option as the key in your data object. Also, the value is stored in option not $optionID as you were trying to use.

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

The $ is a valid character in variable names in Javascript, it doesn't do anything special itself but some coders prefix anything that is a jQuery object with $ so they can glance through code and easily see what variables already have the jQuery wrapper.

$是Javascript中变量名中的一个有效字符,它本身并没有做任何特别的事情,但是一些编码者前缀任何一个带有$的jQuery对象,这样他们就可以浏览代码,并且很容易地看到哪些变量已经有了jQuery包装器。

For example:

例如:

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.

#3


0  

  $optionID = "="+optionID;

I don't quite understand what you are trying to do here o.O, in javascript you don't define your variables using $.

我不太明白你在这里想做什么。在javascript中,不能使用$来定义变量。

data: { optionID : option}

using it like this should work. You would retrieve it like this in PHP:

像这样使用它应该是可行的。在PHP中可以这样检索:

$option_value=$_POST['optionID'];

#1


9  

$("#submit_vote").click(function(e){ 

    $.ajax( {
      type: "POST",
      url: "ajax_submit_vote.php",
      data: $('#poll_form').serialize(),
      success: function( response ) {}
    });

});

You should then have the POST variable "poll_option" accessible in your PHP script.

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

#2


2  

var option = $('input[type="radio"]:checked').val();

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

In the PHP you are reading $_POST['poll_option'] therefore you must use poll_option as the key in your data object. Also, the value is stored in option not $optionID as you were trying to use.

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

The $ is a valid character in variable names in Javascript, it doesn't do anything special itself but some coders prefix anything that is a jQuery object with $ so they can glance through code and easily see what variables already have the jQuery wrapper.

$是Javascript中变量名中的一个有效字符,它本身并没有做任何特别的事情,但是一些编码者前缀任何一个带有$的jQuery对象,这样他们就可以浏览代码,并且很容易地看到哪些变量已经有了jQuery包装器。

For example:

例如:

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.

#3


0  

  $optionID = "="+optionID;

I don't quite understand what you are trying to do here o.O, in javascript you don't define your variables using $.

我不太明白你在这里想做什么。在javascript中,不能使用$来定义变量。

data: { optionID : option}

using it like this should work. You would retrieve it like this in PHP:

像这样使用它应该是可行的。在PHP中可以这样检索:

$option_value=$_POST['optionID'];