Getting radio button value and sending through ajax to php(获取单选按钮值并通过 ajax 发送到 php)
问题描述
我的网站上有一个投票,每个答案旁边都会显示单选按钮.当用户选择一个选项并提交时,我通过 ajax 运行一个 php 脚本以将值或选定的单选按钮插入到表中.
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 正在运行,但当前每行插入一个 0 行,因此它没有从单选按钮中获取值.任何帮助将不胜感激.
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'> Arts</label><br />
<input type="radio" name="poll_option" value="2" id="poll_option" /><label for='2'> Film</label><br />
<input type="radio" name="poll_option" value="3" id="poll_option" /><label for='3'> Games</label><br />
<input type="radio" name="poll_option" value="4" id="poll_option" /><label for='4'> Music</label><br />
<input type="radio" name="poll_option" value="5" id="poll_option" /><label for='5'> Sports</label><br />
<input type="radio" name="poll_option" value="6" id="poll_option" /><label for='6'> Television</label><br />
<input type="submit" value="Vote →" 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",
url: "ajax_submit_vote.php",
data: $('#poll_form').serialize(),
success: function( response ) {}
});
});
然后,您应该可以在 PHP 脚本中访问 POST 变量poll_option".
You should then have the POST variable "poll_option" accessible in your PHP script.
这篇关于获取单选按钮值并通过 ajax 发送到 php的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:获取单选按钮值并通过 ajax 发送到 php
基础教程推荐
- 在 Woocommerce 中根据运输方式和付款方式添加费用 2021-01-01
- Libpuzzle 索引数百万张图片? 2022-01-01
- XAMPP 服务器不加载 CSS 文件 2022-01-01
- 如何在 PHP 中的请求之间持久化对象 2022-01-01
- 在 PHP 中强制下载文件 - 在 Joomla 框架内 2022-01-01
- 通过 PHP SoapClient 请求发送原始 XML 2021-01-01
- 超薄框架REST服务两次获得输出 2022-01-01
- WooCommerce 中选定产品类别的自定义产品价格后缀 2021-01-01
- 在多维数组中查找最大值 2021-01-01
- mysqli_insert_id 是否有可能在高流量应用程序中返回 2021-01-01