2013-11-26 26 views
2

我有一个显示民意调查流的页面(每个民意调查最多有4个答案选项)。这个页面是从一个数据库动态生成的,因此它将从mysqli_fetch_array中抽取每个轮询问题和选项。在一页上进行多轮询的流 - 如何插入?

我很难与如何让用户投票投票。我将每个民意调查作为自己的形式(带有选项的单选按钮),并将其设置为单击其中一个单选按钮时提交表单的位置。问题是,它最终会提交页面上的每个答案选项(我假设它是来自$ row ['option'])。

实施这种事情的最佳策略是什么,只有投票投票将被提交?如果可能的话,希望使用ajax。

回答

0

你能否显示你的html代码,以及你如何激发你的click_event?

阿贾克斯与 - jQuery的:

<!-- in your head-section include jQuery --> 
<head> 
    <script src ="//code.jquery.com/jquery-1.10.2.min.js"> </script> 
</head> 
<!-- your forms --> 
<form> 
    <input type="radio" name="radio1" value="foo"> Foo<br> 
</form> 
<form> 
    <input type="radio" name="radio2" value="bar"> Bar<br> 
</form> 
<form> 
    <input type="radio" name="radio3" value="foobar"> Foobar 
</form> 

<script > 
//wait until document is ready 
$(function() { 
    //when these are the only radio-buttons on page you can select all and bind an click-event to it, 
    //else add a class to all radio-buttons and select like: $(".your_class") 
    $(":radio").click(function() { 
    //get the url from the action-attribute of the form 
    var value = $(this).val(), 
     radioName = $(this).attr("name"); 
    $.ajax({ 
     type: "POST", 
     url: "/your_file.php",//the URL to your php-File 
     data: { name: radioName, data: value} 
    }) 
    .done(function(msg) { 
     alert("Data Saved: " + msg); 
    }); 
    }); 
}); 
</script> 

在你的PHP文件,你可以现在$ _ POST [ '名']和$ _ POST通常保存POST [ '数据'。 您在此文件中输出的所有内容都将成为警报中的“msg”输出(在ajax请求中)。 你可以像也输出的JSON字符串:

echo json_encode(array("error" => TRUE, "text" => "Data could not be saved!")); 

然后在你完成功能做:

if(msg.error) 
    alert("ERROR: " + msg.text); 
else 
    alert("Your data has been saved :)"); 
相关问题