好吧,所以我得到了带有成对字段的表格...一个是select,另一个是数字输入。如果选择值不为null,则输入的数字会显示在其旁边...提交后,我需要检索选择菜单的名称,即它的值和除此以外输入的数字的值...
对我来说,整个事情变得不可能了,因为所有这些字段的名称都是从SQL提取的。
这是我正在制作的页面的图像: Preview Of My Form https://drive.google.com/file/d/1Qv1Hr8tSIj95qBb98vCV1kftdftnaGGb/view?usp=sharing
以及下面给出的HTML(请注意,这些都是从浏览器源中复制的。实际上,这些都是从SQL中提取的)
<form class="predictForm" name="predictForm" id="predictForm" autocomplete="off">
<div class="form-group clearfix" data-question="1">
<div class="inner-form-group predictType">
<label>Match Winner</label>
<select class="form-control match_winner" name="match_winner" id="match_winner">
<option value=""> - Select Team - </option>
<option value="4">team4</option>
<option value="5">team5</option>
<option value="6">team6</option>
<option value="7">team7</option>
<option value="8">team8</option>
</select>
</div>
<div class="inner-form-group predictVal" style="display: block;">
<label>Points</label>
<input type="number" class="form-control match_winner_val" name="match_winner_val" id="match_winner_val" placeholder="Value">
</div>
</div>
<div class="form-group clearfix" data-question="12">
<div class="inner-form-group predictType">
<label>Best Batsman</label>
<select class="form-control best_batsman" name="best_batsman" id="best_batsman">
<option value=""> - Select Player - </option>
<option value="7" title="team6">Name 1</option>
<option value="9" title="team5">Name 2</option>
<option value="10" title="team5">Name 3</option>
<option value="12" title="team8">Name 4</option>
</select>
</div>
<div class="inner-form-group predictVal" style="display: block;">
<label>Points</label>
<input type="number" class="form-control best_batsman_val" name="best_batsman_val" id="best_batsman_val" placeholder="Value">
</div>
</div>
<div class="form-group clearfix" data-question="8">
<div class="inner-form-group predictType">
<label>Best catch done by</label>
<select class="form-control best_catch_by" name="best_catch_by" id="best_catch_by">
<option value=""> - Select Player - </option>
<option value="3" title="team5">Name 1</option>
<option value="7" title="team5">Name 2</option>
<option value="8" title="team8">Name 3</option>
</select>
</div>
<div class="inner-form-group predictVal">
<label>Points</label>
<input type="number" class="form-control best_catch_by_val" name="best_catch_by_val" id="best_catch_by_val" placeholder="Value">
</div>
</div>
<div class="form-group clearfix" data-question="4">
<div class="inner-form-group predictType">
<label>Runner Up Team</label>
<select class="form-control runner_up_team" name="runner_up_team" id="runner_up_team">
<option value=""> - Select Team - </option>
<option value="4">team4</option>
<option value="5">team5</option>
<option value="6">team6</option>
<option value="7">team7</option>
<option value="8">team8</option>
</select>
</div>
<div class="inner-form-group predictVal">
<label>Points</label>
<input type="number" class="form-control runner_up_team_val" name="runner_up_team_val" id="runner_up_team_val" placeholder="Value">
</div>
</div>
<div class="form-group clearfix" data-question="5">
<div class="inner-form-group predictType">
<label>Max Six</label>
<select class="form-control max_six" name="max_six" id="max_six">
<option value=""> - Select Team - </option>
<option value="4">team4</option>
<option value="5">team5</option>
<option value="6">team6</option>
<option value="7">team7</option>
<option value="8">team8</option>
</select>
</div>
<div class="inner-form-group predictVal">
<label>Points</label>
<input type="number" class="form-control max_six_val" name="max_six_val" id="max_six_val" placeholder="Value">
</div>
</div>
<div class="form-group clearfix">
<button type="submit">
<span class="front">
<i class="fas fa-paper-plane"></i>
<span class="value">Send <span>Predictions</span></span>
</span>
<span class="back">
<i class="fas fa-paper-plane"></i>
<span class="value">Send <span>Predictions</span></span>
</span>
</button>
</div>
</form>
简而言之,我需要插入
进入数据库。
我尝试过帐所有值并将其拆分为四个不同的数组。但这并没有按预期方式工作,因为不需要数组中的第三个元素,并且由于其他一些原因。
我在html中创建了一个隐藏元素,该元素将使用字段名称和值进行更新,并使用数组;同时具有选择和数字输入的on change功能。然后,我将其与其他数据一起发布。但这会产生比我们解决的错误更多的错误,因为不同的用户以不同的方式与表单交互。.(请注意,所有字段都不是必需的。所有字段都是可选的。)
我用Google搜索了我的问题...很多次
<?php
foreach ($_POST as $key => $value) {
if($value != "") {
echo "<table>";
echo "<tr>";
echo "<td>";
echo $key;
echo "</td>";
echo "<td>";
echo $value;
echo "</td>";
echo "</tr>";
echo "</table>";
}
}
?>
答案 0 :(得分:0)
如果您要将表单内容发送到php,并且需要获取输入的值,则它们都将出现在$ _POST数组中。
每个输入(也包括选择项)都带有其html-name属性。
对于您的表单元素“ runner_up_team”,您可以这样访问它:
$val = $_POST["runner_up_team"]; // $val contains the seleceted value from the html-select
因此,如果您想获取所选字段的编号,我建议使用类似以下的内容:
$result = new array();
foreach ($_POST as $inputName => $value) {
// We only want to inspect the selects and retrieve the number value later.
if (strpos($inputName, '_val') === FALSE) {
if ($value != '') {
// With the current select name + the suffix we get the name for the number input field
$numberInputName = $inputName . '_val';
// Now we can access the value of the number input with the name.
$numberInput = $_POST[$numberInputName];
// now you can put your data into a result array.
$result[$value] = $numberInput;
}
}
}
结果数组现在应如下所示:
["runner_up_team" => 43, "max_six" => 32]
如果您现在想循环访问它,则可以使用前面的示例,但可以使用resultArray:
<?php
foreach ($result as $key => $value) {
if($value != "") {
echo "<table>";
echo "<tr>";
echo "<td>";
echo $key;
echo "</td>";
echo "<td>";
echo $value;
echo "</td>";
echo "</tr>";
echo "</table>";
}
}
?>
更新 对于您在评论中的问题:
替换下一行
$result[$value] = $numberInput;
与
$element = new array();
$element['select_name'] = $inputName;
$element['number'] = $numberInput;
$element['select_value'] = $value;
$result[] = $element;
这会将第二个foreach更改为:
<?php
foreach ($result as $value) {
echo "<table>";
echo "<tr>";
echo "<td>";
echo $value['select_name'];
echo "</td>";
echo "<td>";
echo $value['select_value'];
echo "</td>";
echo "<td>";
echo $value['number'];
echo "</td>";
echo "</tr>";
echo "</table>";
}
?>