我正在尝试从MYSQL接收数据,用于投票调查 对于2个特定的列,我将它存储在一个表中,然后我添加了单选按钮,如是或否,但由于多个单选按钮(显示指定列的每一行),我必须有唯一的标识符并在投票后我需要知道用户点击哪里更新数据库。
echo '<td><div id="H'.$ingID.'"></div><form id ="H1'.$ingID.'"> Yes: <input type="radio" name="halal" value="halal_yes '.$ingID." ".$vegiVote.'" id="halal_yes" onclick="getVote(this.value)">';
echo '<br>No: <input type="radio" name="halal" value="halal_no '.$ingID." ".$vegiVote.'" id="halal_no" onclick="getVote(this.value)"> </form></td>';
echo '<td>'.($row["vegi?"]).'</td></tr>';
}else if (is_null($row["vegi?"]) && !is_null($row["halal?"])){
echo '<td>'.($row["halal?"]).'</td>';
echo '<td><div id="V'.$ingID.'"></div><form id ="V1'.$ingID.'"> Yes: <input type="radio" name="vegi" value="vegi_yes '.$ingID." ".$vegiVote.'" id="vegi_yes" onclick="getVote(this.value)">';
echo '<br>No: <input type="radio" name="vegi" value="vegi_no '.$ingID." ".$vegiVote.'" id="vegi_no" onclick="getVote(this.value)"> </form></td></tr>';
}else {
echo '<td><div id="H'.$ingID.'"></div><form id ="H1'.$ingID.'"> Yes: <input type="radio" name="halal" value="halal_yes '.$ingID." ".$vegiVote.'" onclick="getVote(this.value)">';
echo '<br>No: <input type="radio" name="halal" value="halal_no '.$ingID." ".$vegiVote.'" id="halal_no" onclick="getVote(this.value)"> </form></td>';
echo '<td><div id="V'.$ingID.'"></diV><form id ="V1'.$ingID.'"> Yes: <input type="radio" name="vegi" value="vegi_yes '.$ingID." ".$vegiVote.'" onclick="getVote(this.value)">';
echo '<br>No: <input type="radio" name="vegi" value="vegi_no '.$ingID." ".$vegiVote.'" id=vegi_no onclick="getVote(this.value)"> </form></td></tr>';
我认为因为我传递php变量作为对象传递并给我错误
终极希望我想实现的是这样的东西,即使它不起作用,虽然值是通过alert(item)检查传递的 警报(testsplit [0])
'<script type="text/javascript">
function getVote(item) {
var testsplit = item.split(" ");
var hingID= "";
var vingID = "";
var voteyes = "";
var voteno = "";
var no=true;
if (testsplit[0]=="halal_yes") {
hingID= testsplit[1];
voteyes = 1 + testsplit[2];
voteno = testsplit[3];
no = false;
}else if (testsplit[0]=="halal_no"){
hingID= testsplit[1];
voteyes = testsplit[2];
voteno = 1 + testsplit[3];
}else if(testsplit[0]=="vegi_yes"){
vingID= testsplit[1];
voteyes = 1 + testsplit[2];
voteno = testsplit[3];
no = false;
}else if(testsplit[0]== "vegi_no"){
vingID= testsplit[1];
voteyes = 1 + testsplit[2];
voteno = testsplit[3];
}
alert(testsplit);
if(!hingID && no==true){
document.getElementById("V1"+vingID).remove();
var wsize=100*Math.round(voteno/(voteno+voteyes);
var ihtml = "<img src="img/poll.gif" width ="+wsize+height="20">+wsize+"%"";
document.getElementById("V"+vingID).document.createElement("ihtml");
}else if (!hingID && no == false){
document.getElementById("V1"+vingID).remove();
var wsize=100*Math.round(voteyes/(voteno+voteyes);
var ihtml = "<img src="img/poll.gif" width ="+wsize+height="20">+wsize+"%"";
document.getElementById("V"+vingID).document.createElement("ihtml");
}else if (!vingID && no==true){
document.getElementById("H1"+hingID).remove();
var wsize=100*Math.round(vote/(voteno+voteyes);
var ihtml = "<img src="img/poll.gif" width ="+wsize+height="20">+wsize+"%"";
document.getElementById("H"+hingID).document.createElement("ihtml");
}else if (!vingID && no==false){
document.getElementById("H1"+ingID).remove();
var wsize=100*Math.round(voteyes/(voteno+voteyes);
var ihtml = "<img src="img/poll.gif" width ="+wsize+height="20">+wsize+"%"";
document.getElementById("H"+hingID).document.createElement("ihtml");
}
}
</script>
我不擅长使用javascript并且更喜欢php,因此我也尝试将值传递给另一个php文件,但它不起作用,任何建议我如何使这个可行。
经过调试后,我意识到数组已经通过,但它没有给出正确的值任何线索?
答案 0 :(得分:1)
我建议你研究json_encode() - 当发送一组数据(PHP)时,使用此函数将其转换为JSON对象
通过上面的链接,因为它会让你的生活更轻松一旦你掌握了它......
尝试简化代码..让单选按钮使用如下所示的函数,在提交时将已检查的值添加到数组中
<div id="H-id"><h1>Halal Poll</h1></div>
<form id="H1-id">
<input type="radio" name="halal" value="halal_yes " id="halal_yes"><label>Yes</label>
<input type="radio" name="halal" value="halal_no" id="halal_no"><label>No</label>
</form>
<div id="V-id"><h1>Vegi Poll</h1></div>
<form id="V1-id">
<input type="radio" name="vegi" value="vegi_yes" id="vegi_yes"><label>Yes</label>
<input type="radio" name="vegi" value="vegi_no" id="vegi_no"><label>No</label>
</form>
<button id="getValues">getValues</button>
// On submit
$("#getValues").click(function(){
var Halal_Answer = document.getElementsByName('halal'); // Halal Radios
var Vegi_Answer = document.getElementsByName('vegi'); // Vegi Radios
var Answers = {};
// Loop through the set of radios
var radios = document.getElementsByTagName('input');
var Answers = [];
for (var i = 0; i < radios.length; i++) {
if (radios[i].type === 'radio' && radios[i].checked) {
// get value, set checked flag or do whatever you need to
Answers.push(radios[i].value);
}
}
console.log(Answers); // eg array with values selected ["halal_yes ", "vegi_no"]
});
您可以另外简化条件陈述,这样您就不会重复:
var ihtml = "<img src='img/poll.gif' width =' + wsize + height='20'> + wsize + '%'" ;
var wsize = function(x) { // Where x = the variable no
if(x){
return 100 * Math.round( voteno / (voteno + voteyes );
} else {
return 100 * Math.round( voteyes / (voteno + voteyes );
}
}
if(!hingID){
document.getElementById("V1" + vingID).remove();
document.getElementById("V" + vingID).document.createElement("ihtml");
wsize(no);
} else if(!vingID ){
document.getElementById("H1"+hingID).remove();
document.getElementById("H"+hingID).document.createElement("ihtml");
wsize(no);
}
轮询答案存储在数组Answers
中,然后您可以将其发送给您的服务器并处理信息。例如,使用AJAX(你需要Jquery)
// Call this function instead of console.log in the above example
$.ajax({
method:"POST",
url:"Answers.php", // path to your php file
data: {data:Answers},
success: function(){
// function to run when results are submitted
alert("Answers Submitted. Thank you..")
},
error: function(){
// handle form submission errors here..
alert('There was an error processing your Poll. Try again..')
}
});
然后在您的Answers.php
文件中尝试添加var_dump($_POST);
,这样您就可以看到所发布数据的结构,在这种情况下:
array(1) {
["data"]=>
array(2) {
[0]=>
string(10) "halal_yes "
[1]=>
string(7) "vegi_no"
}
}
// $Halal = $data[0] ;
// $Vegi = $data[1] ;
答案 1 :(得分:0)
尝试这样而不是
onclick="getVote(this.value)"
使用
onclick="getVote(this)"
并在javascript中尝试如下所示
var val = item.value;
var testsplit = val.split(" ");