我大概是PHP中的新手,我试图在用户选择的选择选项列表中的一个表中显示多维数组的内容。我要做的是将所选选项的数组键返回到一个新变量(如$keyref = key($myarray); )中,这样我就可以使用该键在表中显示,但我不知道如何显示。我是否在onChange=function()列表中使用javascript函数(如<select> )?
以下是代码:
<?php
$tshirt = array (
array("T-shirt A",15,"XS"),
array("T-shirt B",20,"S"),
array("T-shirt C",25,"M")
); //array("t-shirt type", price, "size")
$keyRef = 0; //the variable that will contain the selected array key
echo "<select id='shirtlist'>";
foreach($tshirt as $value){
echo "<option value='$value)'>$value[0]</option>";
}
echo "</select>";
echo "<p>Details of T-shirt:</p>";
echo "<table>";
echo "<tr>";
echo "<th>T-shirt Type</th>";
echo "<th>Price</th>";
echo "<th>Size</th>";
echo "</tr>";
echo "<tr>";
echo "<td>".$tshirt[$keyRef][0]."</td>";
echo "<td>".$tshirt[$keyRef][1]."</td>";
echo "<td>".$tshirt[$keyRef][2]."</td>";
echo "</tr>";
echo "</table>";
?>该表设法显示第一个数组的数据,但当我选择其他选项时没有发生任何事情。至于尝试javascript函数,我尝试了json_encode($myarray)方法,但它只返回数组作为字符串通知。
发布于 2020-12-12 17:30:27
首先,您必须了解PHP只在服务器端执行,这意味着PHP代码与您在浏览器中实现的操作之间没有交互。
返回PHP的唯一方法是发送一个新请求。
取决于您想要做什么以及您想要在浏览器中的哪个工作流,您应该在纯PHP (每个操作都必须通过客户机-服务器请求实现)还是使用javascript (在浏览器中直接执行)之间进行选择。
在纯PHP中,您的特性可以通过以下代码实现:
<?php
$tshirt = array (
array("T-shirt A",15,"XS"),
array("T-shirt B",20,"S"),
array("T-shirt C",25,"M")
); //array("t-shirt type", price, "size")
$keyRef = 0; //the variable that will contain the selected array key
if(isset($_GET["shortlist"]) && isset($tshirt[$_GET["shirtlist"]])) {
$keyRef = $_GET["shirtlist"];
}
echo "<form method='GET'>";
echo "<select name='shirtlist'>";
foreach($tshirt as $key => $value){
$selected = '';
if ($key == $keyRef) {
$selected = 'selected';
}
echo "<option value='$key' $selected>".$value[0]."</option>";
}
echo "</select>";
echo "<input type='submit' value='submit'/>";
echo "</form>";
echo "<p>Details of T-shirt:</p>";
echo "<table>";
echo "<tr>";
echo "<th>T-shirt Type</th>";
echo "<th>Price</th>";
echo "<th>Size</th>";
echo "</tr>";
echo "<tr>";
echo "<td>".$tshirt[$keyRef][0]."</td>";
echo "<td>".$tshirt[$keyRef][1]."</td>";
echo "<td>".$tshirt[$keyRef][2]."</td>";
echo "</tr>";
echo "</table>";
?>请注意,我使用全局$_GET从URL查询字符串获取信息。当单击表单的submit按钮时,将此信息传递给PHP,然后使用标记的name属性在其中访问select的值(<select name="shirtlist">是指当表单的方法为GET时,$_GET["shirtlist"]中的值可以访问)。
此代码要求用户使用所选内容更新页面所需的操作。
我还向selected添加了一个select属性,以便选择显示的项目(当迭代索引与请求的shirtlist值相同时)。
双重检查if(isset($_GET["shortlist"]) && isset($tshirt[$_GET["shirtlist"]])) {是为了确保所选项在列表中,因为任何人都可以访问该URL --手动更改该值是可能的,因此将表单方法更改为POST,然后使用$_POST全局数组而不是$_GET one访问该值更为安全。
发布于 2020-12-12 17:36:54
Html代码:
<?php
$tshirt = array (
array("T-shirt A",15,"XS"),
array("T-shirt B",20,"S"),
array("T-shirt C",25,"M")
); //array("t-shirt type", price, "size")
?>
<!-- here we create a select box -->
<select class="" id="shirtlist">
<option value="" selected>select</option>
<!-- add the key varible into foreach loop that give the index of the value -->
<?php foreach($tshirt as $key => $value){ ?>
<option value="<?=$key?>"><?=$value[0]?></option>";
<?php } ?>
</select>
<p>Details of T-shirt:</p>
<table>
<tr>
<th>T-shirt Type</th>
<th>Price</th>
<th>Size</th>
</tr>
<tr>
<!-- Here we are created the empty table Data will be field after user select any option -->
<td class="type"></td>
<td class="price"></td>
<td class="size"></td>
</tr>
</table>添加Jquery:
<script src="https://code.jquery.com/jquery-3.5.1.min.js" integrity="sha256-9/aliU8dGd2tb6OSsuzixeV4y/faTqgFtohetphbbj0=" crossorigin="anonymous"></script>Javascript/Jquery代码:
<script type="text/javascript">
/* we we convert the PHP array into Javascript array using JSON.parse function */
var data = JSON.parse('<?=json_encode($tshirt)?>');
/* This is the code when user change the option in select box then it will add/replace the data into table */
$('#shirtlist').change(function(e){
var key = $(this).val();
$('.type').text(data[key][0]);
$('.price').text(data[key][1]);
$('.size').text(data[key][2]);
});这样就可以完成工作了。
在“复选框更改”事件上,它将将数据添加/替换到表中。
https://stackoverflow.com/questions/65267502
复制相似问题