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

前端之家收集整理的这篇文章主要介绍了获取单选按钮值并通过ajax发送到php前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我的网站上有一个民意调查,每个答案旁边都会显示单选按钮.当用户选择一个选项并提交时,我通过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')";

提前致谢!

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

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

});

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

原文链接:https://www.f2er.com/ajax/160221.html

猜你在找的Ajax相关文章