简体   繁体   English

多个单选按钮ajax发布

[英]Multiple radio buttons ajax post

I'm new in web programming and I really need your help. 我是网络编程的新手,我真的需要你的帮助。 I have a form with several radio buttons and I want to insert them into mysql through an ajax post. 我有一个带有几个单选按钮的表单,我想通过ajax帖子将它们插入到mysql中。 I can do it for a single button but for more than one I don't have idea how to do it. 我可以用一个按钮来做,但是不止一个我不知道怎么做。

This is a part of my html and jquery: 这是我的html和jquery的一部分:

<html>
    <script>
        $(document).ready(function () {
            $("#submit").click(function () {
                var q1 = $('input[type="radio"]:checked').val();
                if ($('input[type="radio"]:checked').length == "0") {
                    alert("Select any value");
                } else {
                    $.ajax({
                        type: "POST",
                        url: "ajax-check.php",
                        data: "q1=" + q1,
                        success: function () {
                            $("#msg").addClass('bg');
                            $("#msg").html("value Entered");
                        }
                    });
                }
                return false;
            });
        });
    </script>
    </head>
    <body>
        <div id="msg"></div>
        <form method="post" action="">
            <div class="Clear">
                question1:bla bla bla
                <input class="star required" type="radio" name="q1" value="1" />
                <input class="star" type="radio" name="q1" value="2" />
                <input class="star" type="radio" name="q1" value="3" />
                <input class="star" type="radio" name="q1" value="4" />
                <input class="star" type="radio" name="q1" value="5" />
            </div>
            <br />
            <div class="Clear">
                question2:bla bla bla
                <input class="star required" type="radio" name="q2" value="1" />
                <input class="star" type="radio" name="q2" value="2" />
                <input class="star" type="radio" name="q2" value="3" />
                <input class="star" type="radio" name="q2" value="4" />
                <input class="star" type="radio" name="q2" value="5" />
            </div>
            <input type="submit" name="submit" id="submit" />
        </form>
    </body>
</html>

And here is how I insert the button in mysql: 以下是我在mysql中插入按钮的方法:

<?php
$query = mysql_connect("localhost", "root", "");
mysql_select_db('db', $query);

if (isset($_POST['q1'])) {
    $choice1 = $_POST['q1'];
    $choice2 = $_POST['q2'];
    mysql_query("INSERT INTO tb VALUES('','$choice1')");
}
?>

I've tried to make a var for each button but dosen't worked. 我已经尝试为每个按钮制作一个var但是没有工作。 How could I post all values in php and what should I change into my ajax and php? 我怎么能在php中发布所有值,我应该更改为我的ajax和php? Thanks! 谢谢!

This is how I did the .php 这就是我做.php的方式

<?php
$query=mysql_connect("localhost","root","");
mysql_select_db('cosmote',$query);
$q = array();
for ($i = 1; $i <= 2; $i++) {
  $q[$i] = isset($_POST['q'+$i]) ? mysql_real_escape_string($_POST['q'+$i]) : 0;
}
mysql_query("INSERT INTO tabel (q1,q2) VALUES ('$q[1]','$q[2]')");
?>

OK, here is how to do it. 好的,这是怎么做的。 First, add an id to the form: 首先,在表单中添加一个id

<form id="myform" method="post" action="">

This will make it easier to access via jQuery. 这将使通过jQuery访问更容易。 Then, pass the serialized form as the POST data to your PHP script: 然后,将序列化的表单作为POST数据传递给PHP脚本:

$(document).ready(function () {
    $("#submit").click(function () {                
        if ($('input[type="radio"]:checked').length == "0") {
            alert("Select any value");
        } else {
            $.ajax({
                type: "POST",
                url: "ajax-check.php",
                data: $("#myform").serialize(),
                success: function () {
                    $("#msg").addClass('bg');
                    $("#msg").html("value Entered");
                }
            });
        }
        return false;
    });
});

After that, you can get the radio button values from the $_POST array in your PHP script: 之后,您可以从PHP脚本中的$_POST数组中获取单选按钮值:

$_POST['q1'] // value of q1, can be 1-5
$_POST['q2'] // value of q1, can be 1-5

EDIT: The problem with your PHP code is that you used + instead of . 编辑:您的PHP代码的问题是您使用+而不是. for concatenating strings. 用于连接字符串。 Write this instead: 写这个:

$q[$i] = isset($_POST['q'.$i]) ? mysql_real_escape_string($_POST['q'.$i]) : 0;

After this, I'm pretty sure it will work. 在此之后,我很确定它会起作用。

您可以在表单上使用.serialize() ,它会为您提供表单的值,就像您定期提交data: $('form').serialize(),

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM