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 keil的map文件中Image component sizes各项意思
  • ¥30 BC260Y用MQTT向阿里云发布主题消息一直错误
  • ¥20 求个正点原子stm32f407开发版的贪吃蛇游戏
  • ¥15 划分vlan后,链路不通了?
  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据
  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 Centos / PETGEM
  • ¥15 划分vlan后不通了
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)