dongqing483174 2014-11-26 23:02
浏览 34

PHP POST和Javascript?

This may sound confusing, but I am unsure as where to start looking for an answer. This is the scenario: I have a webpage with a table created using PHP and inside each cell is a randomly selected word. What I would like to do is allow a user to click one of the cells and it would return the definition of the word, and refresh the table/page. From what I found so far was to make use of _POST/_REQUEST, however I am unsure how to find out what the user clicked, and pass that into a function to find the definition. Is my logic correct here, how would you go about this? I was thinking of having an onclick function to identify the element clicked, but don't know how to handle it.

<body>
<form method="post" action="
<table border="1">

<?php

$f="/words.txt";    //definitions also included in this file
$o=file($f);
$len=count($o);

$i=0;
while( $i < 18){
    $rnum= rand(2,$len);
    $rword= $o[$rnum];
    $piece= explode(" ",$rword);      //get just the word on the line

    if($i%3==0){
            echo "<tr>";
    }
echo "<td id='$i' onclick='about()'>".$piece[2]."</td>";
    $i++;
    if($i%3==0){
        echo "</tr>";
    }
}
?>

</table>
</body>
</html>
  • 写回答

2条回答

  • dqe9657 2014-11-26 23:29
    关注

    The simplest thing you may done without need to any javascript is to make number forms equals to the number of cells you have. It is something like the following:

    //Remove the form tag
    <table border="1">
    
    <?php
    
    $f="/words.txt";    //definitions also included in this file
    $o=file($f);
    $len=count($o);
    
    $i=0;
    while( $i < 18){
        $rnum= rand(2,$len);
        $rword= $o[$rnum];
        $piece= explode(" ",$rword);      //get just the word on the line
    
        if($i%3==0){
                echo "<tr>";
        }
    ?>
    <td id='<?php echo $i; ?>'><form method="post"><input type="hidden" name="word" value="<?php echo $piece[2];?> /><input type="submit" value="<?php echo $piece[2];?> /></form></td>;
    <?php
        $i++;
        if($i%3==0){
            echo "</tr>";
        }
    }
    ?>
    
    </table>
    

    By this way you have a submit button for each word you have that submit its own form's hidden element with the name word to be precessed on the server side.

    评论

报告相同问题?

悬赏问题

  • ¥15 求差集那个函数有问题,有无佬可以解决
  • ¥15 【提问】基于Invest的水源涵养
  • ¥20 微信网友居然可以通过vx号找到我绑的手机号
  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名
  • ¥65 汇编语言除法溢出问题