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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15814369/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 10:01:02  来源:igfitidea点击:

Getting radio button value and sending through ajax to php

phpjqueryhtmlajaxradio-button

提问by Paul 'Macca' McGill

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.

我的网站上有一项民意调查,每个答案旁边都显示单选按钮。当用户选择一个选项并提交时,我会通过 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:

阿贾克斯:

    $("#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!

提前致谢!

回答by StuR

$("#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”。

回答by MrCode

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_optionas the key in your data object. Also, the value is stored in optionnot $optionIDas you were trying to use.

在您正在阅读的 PHP 中,$_POST['poll_option']因此您必须将其poll_option用作数据对象中的键。此外,值存储在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.

回答by Naryl

  $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 $.

我不太明白你在这里尝试做什么 oO,在 javascript 中你没有使用$.

data: { optionID : option}

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

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

$option_value=$_POST['optionID'];