douba4275 2017-04-30 11:38
浏览 67

如何将JavaScript变量传递给PHP? 没有表格并提交

I have a simple HTML file. In this HTML file I have a table with rows and columns. In another PHP file. I have a code that takes my bank info MYSQL information Now I am trying to send from the first HTML file to PHP. Every tag has an attribute. Home and guest. Send 2 VAR to PHP and then load the PHP page with the information I sent. Now I ask you. Is my method correct? Is there any other way to do it? Thank you very much to everyone who read all the code. And trying to help me.

HTML

$("th").click(function() {
  var home = $(this).attr("home");
  var guest = $(this).attr("guest");
  $.post("getBetsTeam.php", {
      home: home,
      guest: guest
    })
    .done(function(response) {
      $("#controler").html(response);
    });
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<html>

<table>
  <tbody>
    <tr>
      <th class="without" home="Liverpool" guest="Bolton">Liverpool-Bolton</th>
      <td>3.3</td>
      <td>2.5</td>
      <td>2.8</td>
    </tr>
    <tr>
      <th class="without" home="manchster united" guest="newcasel">manchster united-newcasel</th>
      <td>2</td>
      <td>3</td>
      <td>4</td>
    </tr>
  </tbody>
</table>

PHP

<?php $link = mysqli_connect('localhost', 'root', '');
mysqli_select_db($link,'bet369');

if(!link){
    //echo "connection to db feild!";
}else{
    //echo "conection succesful!!! </br>";
}

if(mysqli_select_db($link,'bet369')){
    //echo "db selected! <br>";
} ?>
<?php
    $fr = 'Fulltime Result';
    $home = $_POST['home'];
    $guest = $_POST['guest'];
    $res = mysqli_query($link,"select * from bets where line='$fr' and home='$home' and guest='$guest'");
    $rows =mysqli_fetch_row($res);
    if ($rows[0] == 0) {
    die;
}
else{
    $res = mysqli_query($link,"select * from bets where line='$fr'");
   //if ($rows[0] == 1) {
    echo '
    <div class="headBets" style="color:white; with:100%; text-align: center; background-color: #0c15d2;">Fulltime Result</div>
    <table>';

        while($row=mysqli_fetch_array($res))
        {

echo '<tr>';
//echo '<th class="without">'; echo $row[home] . '-' . $row[guest]; echo '</th>';
echo '<td class="with" cheak="0" idbet="'.$row[idbet].'"numid="'. $row[idbet].'AAbet" a="0" b="0" c="0" d="0" e="0" f="0" z="1" value="'.$row[bethome].'"'.'team="'.$row[home].'  v  '.$row[guest].'"choice="'.$row[home].'"liga="'.$row[liga].'"line="'.$row[line].'">'.$row[home]. "   ".$row[bethome].'</td>';
echo '<td class="with" cheak="0" idbet="'.$row[idbet].'"numid="'. $row[idbet].'XXbet" a="0" b="0" c="0" d="0" e="0" f="0" z="x" value="'.$row[betdraw].'"'.'team="'.$row[home].'  v  '.$row[guest].'"choice="'.$row[draw].'"liga="'.$row[liga].'"line="'.$row[line].'">'.$row[draw]. "   ".$row[betdraw].'</td>';
echo '<td class="with" cheak="0" idbet="'.$row[idbet].'"numid="'. $row[idbet].'BBbet" a="0" b="0" c="0" d="0" e="0" f="0" z="2" value="'.$row[betguest].'"'.'team="'.$row[home].'  v  '.$row[guest].'"choice="'.$row[guest].'"liga="'.$row[liga].'"line="'.$row[line].'">'.$row[guest]. "   ".$row[betguest].'</td>';
echo '</tr>';

////////////////////////////////////////////////////////////////////////////////////


        }
        if(mysqli_query){
            //echo "all data reload !";
        }
echo    '</table>';
//  }
}
?>
</div>
  • 写回答

1条回答 默认 最新

  • duanfei9278 2017-05-03 04:57
    关注

    If your goal is to...

    "Send 2 VAR to PHP and then load the PHP page with the information I sent"

    then it sounds like you want to create a form and submit it in your <th> click handler

    const form = document.createElement('form')
    form.action = 'getBetsTeam.php'
    form.method = 'post'
    
    const homeInput = document.createElement('input')
    homeInput.type = 'hidden'
    homeInput.name = 'home'
    homeInput.value = home
    form.appendChild(homeInput)
    
    // repeat for guest
    
    document.body.appendChild(form)
    form.submit()
    
    评论

报告相同问题?

悬赏问题

  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 unity第一人称射击小游戏,有demo,在原脚本的基础上进行修改以达到要求
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line