【问题标题】:How echo and update dynamic options in html php formhtml php表单中如何回显和更新动态选项
【发布时间】:2020-09-07 14:34:17
【问题描述】:

我想在 php 更新/编辑表单中回显和更新这些选项。

这里是html输入动态文本框选项-

这里是完整的代码参考。 https://phppot.com/php/php-contact-form-with-custom-fields/

<div class="input-row">                          
          <input style="width:49%!important; float:left;" type="text" placeholder="Option name" required class="custom-input-field" name="custom_name[]" />                           
          <input style="width:49%!important; float:right;" type="number"  placeholder="Price" min="0" step="0.01" required class="custom-input-field float-right" name="custom_value[]" />                                
 </div>

输入数据库的代码

INSERT INTO custom_contact (name,email,subject,email_content,message,options) VALUES ('$name','$email','$subject','$emailContent','$message','$options')

if(!empty($_POST["custom_name"][0])) {
       // $emailContent .= "<p><u>Custom Information:</u></p>";
        foreach($_POST["custom_name"] as $k=>$v) {            

            $emailContent .=  "" . $_POST["custom_name"][$k] . "-" . $_POST["custom_value"][$k] . ",";

        }
    }

如何在 php 中回显和编辑这些选项和字段

这是我的示例编辑/更新代码 - 我不知道在 value="" 中输入什么

例子-

<input style="width:49%!important; float:left;" type="text" placeholder="Option name" required class="custom-input-field" value="<?php echo$[0]["custom_name"];?>" name="custom_name[]" /> 

<input style="width:49%!important; float:right;" type="number" placeholder="Price" min="0" step="0.01" required class="custom-input-field float-right" value="<?php echo$["custom_value"][$k];?>" name="custom_value[]" />

【问题讨论】:

    标签: php html dynamic


    【解决方案1】:

    与您在第一部分中执行 foreach 循环的方式相同,您可以将 HTML 回显到页面上。这是一个简单的例子:

    <?php
    $arr = [0, 1, 2, 3];
    
    foreach($arr as $number){
    
    echo "<input type='text' value='".$number."' />";
    
    }
    ?>
    

    或者,如果您愿意,可以在 html 很多的情况下插入和退出 PHP 和 HTML,以便更容易键入:

    <?php
    $arr = [0, 1, 2, 3];
    
    foreach($arr as $number){
    ?>
    
    <input type='text' value='<?php echo $number; ?>' />
    
    <?php
    }
    ?>
    

    编辑:所以我不太明白你在问什么,但假设你有一个包含两个项目的数组

    $arr = [
         [ 
              “name”=>”first”,
              “value”=>1
         ],
         [
              “name”=>”second”,
              “value”=>2
          ]
    ];
    

    您可以循环访问它们并像这样访问属性:

    foreach($arr as $item)
    {
         echo $item[“name”];
         echo $item[“value”];
    }
    

    【讨论】:

    • 感谢您的回复。我仍然不知道如何将 custom_name 和 customer_value 两个框中的值分开? foreach($_POST["custom_name"] as $k=>$v) { $emailContent .= "" 。 $_POST["custom_name"][$k] 。 “-”。 $_POST["custom_value"][$k] 。 ",";
    • 数据库的输出是这样的 - green-12,blue-44,red-32, 。所以我需要在一个盒子里放绿色,在第二个盒子里放 12 个,依此类推。
    • 我并没有真正关注你,但我编辑了我的答案也许会有所帮助?
    猜你喜欢
    • 2013-04-07
    • 2017-05-11
    • 1970-01-01
    • 2013-12-14
    • 1970-01-01
    • 1970-01-01
    • 2019-10-31
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多