将php值传递给点击(getFunction(this.value))问题

passing php value to on click (getFunction(this.value)) problems

本文关键字:getFunction this value 问题 值传 php      更新时间:2023-09-26

我正在尝试从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(" ");