【问题标题】:Need to retrieve input names from dynamic form inputs. The names of the input comes from the database需要从动态表单输入中检索输入名称。输入的名称来自数据库
【发布时间】:2019-08-31 12:20:49
【问题描述】:

好的,所以我得到了这个带有成对字段的表单...一个是选择,另一个是数字输入。如果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>

简而言之,我需要插入

  1. 选择字段的名称和值;
  2. 数值输入(不带字段名)

进入数据库..


到目前为止我已经尝试过什么......

  1. 我已尝试发布所有值并将其拆分为四个不同的数组。但它没有按预期工作,因为不需要数组中的第三个元素,并且由于其他一些原因..

  2. 我在 html 中创建了一个隐藏元素,该元素使用字段名称和数组值进行更新;具有用于选择和数字输入的变化功能。然后我将它与其他数据一起发布。但它会产生比我们解决的错误更多的错误,因为不同的用户以不同的方式与表单交互..(请注意,没有一个字段是必需的..它们都是可选的..)

  3. 我用 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>";
        }
    }
?>

【问题讨论】:

  • phpmyadmin 只是您与数据库交互的接口。就这些

标签: php jquery ajax


【解决方案1】:

如果您将表单内容发送到 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>";
}
?>

【讨论】:

  • 嘿,非常感谢?..这正是我想要的..除此之外,还有什么方法可以获取选择字段的名称吗?由于所有名称属性都来自数据库,因此不可能在 PHP 中对其进行硬编码。因此,当我在 PHP 中获取这两个值时,我需要知道这个答案属于哪个问题...为此,我需要获取选择字段的名称属性(来自 SQL,它可能会更改每个时间取决于使用应用程序的管理员)
  • 我也更新了我的答案来回答这个问题。如果这是解决您问题的答案,请将其标记为有效答案,或投票。非常感谢。
  • 这解决了我的问题.. 非常感谢,过去 2 天我一直在尝试这样做.. :)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-09-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-02-17
  • 2013-05-13
相关资源
最近更新 更多