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>
dpowt82802
dpowt82802 感谢您的建议,我现在正在调查它!
接近 5 年之前 回复
dongnianwo8289
dongnianwo8289 另外,我鼓励避免使用内联JS(例如onclick='about()'),而是使用事件监听器。这将帮助您将标记与功能分开。
接近 5 年之前 回复
duanhemou9834
duanhemou9834 你可以做的是将我认为存储在变量$piece[2]中的单词存储为HTML5数据属性(例如数据字或类似单词),这样你就可以使用JS(通过.dataset)来检索它。对于AJAX调用,您可以使用本机JS,也可以依赖外部库(例如jQuery的.ajax())。
接近 5 年之前 回复

2个回答

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.

duanjia4097
duanjia4097 你不需要那个! 只需点击任何单元格的提交按钮即可提交相应的单词!
接近 5 年之前 回复
duanshan3065
duanshan3065 如何识别单击了哪个单元格?
接近 5 年之前 回复

I suggest you to create a form that contains your word. Then you can simply specify action=”yourpagewithresponse.php” onclick="submit()" in the attributes of the form. In this way you don't need the submit button, you can simply click on the word. Obviously you can iterates the form creation in php language for having more word containers.

<form method="post" action=”yourpagewithresponse.php”  onclick="submit()"><input type="hidden" name="word" value="<?php echo $piece[2];?>" /><?php echo $piece[2];?></form>
douxiawei9318
douxiawei9318 也许我无法解释我的建议。 发布将单词作为参数传递的表单的最简单方法是为每个单词创建一个表单,包括名为'word'的隐藏输入,其值为'yourfirstword ...',并在每个单词中添加onclick =“submit()” form,with action =“yourpagewithresponse.php”。 现在进入yourpagewithresponse.php写<?php echo $ _POST ['word']; ?>。 现在,如果你单击主页面中的单词,将打开yourpagewithresponse.php,其中包含你在里面点击的单词。
接近 5 年之前 回复
douhuan1380
douhuan1380 你会知道单击了哪个单词,因为你在帖子中传递给yourpagewithresponse.php单词本身:如果你在yourpagewithresponse.php中你写了$ pippo = $ _POST [`word'],$ pippo将包含你点击的单词。
接近 5 年之前 回复
douli1872
douli1872 我会在回复页面上要求什么? 或者换句话说,我如何知道单击了哪个单元格? (_REQUEST [''])
接近 5 年之前 回复
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!