将php值传递给点击(getFunction(this.value))问题
passing php value to on click (getFunction(this.value)) problems
我正在尝试从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文件,但它不起作用,任何关于如何使其可行的建议。
调试后,我意识到数组已经通过,但它并没有给出正确的值——有什么线索吗?
我建议你研究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] ;
这样尝试而不是
onclick="getVote(this.value)"
使用
onclick="getVote(this)"
在javascript中,尝试像下面的一样
var val = item.value;
var testsplit = val.split(" ");
相关文章:
- $(this).prop('property') vs. this.property
- Twitter Bootstrap typeahead:使用“this”获取上下文/调用元素
- 在另一个函数中使用变量this
- this.router在AngularJS 2中未定义
- reactjs this.refs vs document.getElementById
- JavaScript 中的嵌套函数和 “this” 关键字
- React+Meteor:this.ops返回未定义
- javascript中对象构造函数中的var属性与this.properties
- 函数中this和var之间的区别
- jQuery(this).Pparent().Pparente().Prent().find()在iPad上不起作用
- Understanding Javascript scope with "var that = this&qu
- 定义this.properties或objectName.properties的javascript JSON对象
- Object.prototype using 'this'
- 在JavaScript类型的函数中避免self-this
- 如何使用(this)访问Angular 2 http rxjs catch函数中的对象属性
- Javascript 对象和 this 关键字
- jQuery使用ajax自动完成问题-TypeError:this.source不是函数
- reactjs无法将this.pops传递到react图表列表中
- 为什么在这种情况下,“This”确实充当javascript函数中的私有成员
- 将php值传递给点击(getFunction(this.value))问题