获取单选按钮的价值,并通过AJAX发送到PHP发送到、单选、按钮、价值

2023-09-10 13:19:37 作者:良人唤不回i

我已经在我的网站的一项民意调查显示其单选按钮旁边的每个答案。当用户选择一个选项,提交,即时通过AJAX运行AA PHP脚本插入到表中的值或所选择的单选按钮。

我的阿贾克斯在运行,但目前插入0行的每一行,所以它不是拿起从单选按钮的值。任何帮助将是AP preciated。

HTML:

 <形式ID =poll_form方法=后接收字符集=utf-8>
    <输入类型=无线电名称=poll_option值=1ID =poll_option/><标签='1'>&安培; NBSP;艺术与LT; /标签>< BR /&GT ;
    <输入类型=无线电名称=poll_option值=2ID =poll_option/><标签='2'>&安培; NBSP;电影< /标签>< BR /&GT ;
    <输入类型=无线电名称=poll_option值=3ID =poll_option/><标签='3'>&安培; NBSP;游戏< /标签>< BR /&GT ;
    <输入类型=无线电名称=poll_option值=4ID =poll_option/><标签='4'>&安培; NBSP;音乐< /标签>< BR /&GT ;
    <输入类型=无线电名称=poll_option值=5ID =poll_option/><标签='5'>&安培; NBSP;体育和LT; /标签>< BR /&GT ;
    <输入类型=无线电名称=poll_option值=6ID =poll_option/><标签='6'>&安培; NBSP;电视< /标签>< BR /&GT ;
    <输入类型=提交值=投票和放大器; RARR; ID =submit_vote级=poll_btn/>
< /形式GT;
 

AJAX:

  $(#submit_vote)。点击(函数(五)
    {
    VAR选项= $('输入[类型=电台]:选中)VAL()。
    $ optionID ==+ optionID;

    $阿贾克斯({
        键入:POST,
        网址:ajax_submit_vote.php
        数据:{optionID:$ optionID}
    });
});
 

PHP:(缩短版)

 如果($ _ SERVER ['REQUEST_METHOD'] ==POST){

    //从提交的表单获得价值
    $选项= $ _ POST ['poll_option'];

    //插入到数据库
    $ insert_vote =INSERT INTO民意调查(USERIP,类别id)VALUES('$ IP,$选项');
 
ajax 自动执行 按钮

在此先感谢!

解决方案

  $(#submit_vote)。点击(函数(五){

    $阿贾克斯({
      键入:POST,
      网址:ajax_submit_vote.php
      数据:$('#poll_form')序列化()。
      成功:函数(响应){}
    });

});
 

您应该然后在你的PHP脚本的POST变量poll_option访问。

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.

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.

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)

    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!

解决方案

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