【问题标题】:Select multiple replace one word with every value in an array选择多个用数组中的每个值替换一个单词
【发布时间】:2022-01-03 14:26:42
【问题描述】:

好的,所以我有一个联系表单,我希望人们选择多个项目,一旦他们提交联系表单,我希望它使用 html 模板发送电子邮件。我已将其设置为字符串替换 html 模板中的数据,但每次我尝试执行数组时,它都会说数组或仅显示已选择的多个项目之一。

这是我确保添加 [] 以将名称放入数组的 HTML 选择代码。

<select class="js-example-multiple js-states form-control" multiple="multiple" name="product[]">
   <option value="Baby Shark Castle 15ft x 18ft">Baby Shark Castle 15ft x 18ft</option>
   <option value="Pirate's assault course 12ft x 25ft">Pirate's assault course 12ft x 25ft</option>
   <option value="Yellow Mega Slide 18ftx18ft">Yellow Mega Slide 18ftx18ft</option>
   <option value="18ft x 18ft Disco Dome Lights &amp; Speaker">18ft x 18ft Disco Dome Lights &amp; Speaker</option>
   <option value="Assault Course 35ft Long 12 ft Wide">Assault Course 35ft Long 12 ft Wide</option>
   <option value="Inflatable Nightclub 12ft x 15ft">Inflatable Nightclub 12ft x 15ft</option>
   <option value="40ft Assault course 15ft x 40ft">40ft Assault course 15ft x 40ft</option>
   <option value="Inflatable Pub 17x17 - Holds 20 People">Inflatable Pub 17x17 - Holds 20 People</option>
</select>

这是 php 代码,我已经成功替换了其他单个值,但是当我尝试用多个值替换一个时,它只显示一个值。我尝试了一个 foreach 循环,但这仅在我回显 $product 值时才有效。我想将所有选定的项目串起来,而不仅仅是一个,我有一个我希望它看起来像下面的示例。

    // Bring in the email template here
    $html = file_get_contents('template.html');

    // You only need to modify the following three lines of code to customise your form to mail script.
    $email_to = "aaron@pugmanmedia.co.uk";          // Specify the email address you want to send the mail to.
    $email_from = "info@pugmanmedia.co.uk";  //Specify the email address that you want to send the email from. This needs to be Fasthosts hosted,
    $email_subject = "Website Contact Form";    // Set the subject of your email.
    // Specify a page on your website to display a thankyou message when the mail is sent
    $thankyou_url = "../thankyou.html";

    // Get the details the user entered into the form
    $name = $_POST["name"];
    $reply_to = $_POST["email"];
    $number = $_POST["number"];
    $date = $_POST["date"];
    $message = $_POST["message"];
    $products = $_POST["product"];   
    

    // Validate the email address entered by the user
    if(!filter_var($email_from, FILTER_VALIDATE_EMAIL)) {
        // Invalid email address
        die("The email address entered is invalid.");
    }

    // Replacing the details in the template the above variables
    $html =  str_replace("{{username}}",$name,$html);
    $html =  str_replace("{{email}}",$reply_to,$html);
    $html =  str_replace("{{number}}",$number,$html);
    $html =  str_replace("{{date}}",$date,$html);
    $html =  str_replace("{{message}}",$message,$html);
    foreach($products as $product){
        $list = $product . "<br> test <br>";
        $html =  str_replace("{{list}}",$list,$html);
    };

这是html模板代码

 <div style="margin: 0px; padding: 0px;">
    <p style="margin: 0px; padding: 0px;">
        {{list}}
    </p>
 </div>

这是我得到的结果

 <div style="margin: 0px; padding: 0px;">
    <p style="margin: 0px; padding: 0px;">
        Pirate's assault course 12ft x 25ft
    </p>
 </div>

我想要的最终结果是当人们选择任意数量的项目使其全部出现在这样的一个地方而不是上面

<div style="margin: 0px; padding: 0px;">
    <p style="margin: 0px; padding: 0px;">
       Baby Shark Castle 15ft x 18ft
       Assault Course 35ft Long 12 ft Wide
       Pirate's assault course 12ft x 25ft
       and so on
    </p>
 </div>

【问题讨论】:

    标签: php html arrays email str-replace


    【解决方案1】:

    你需要implode

    替换

    foreach($products as $product){
        $list = $product . "<br> test <br>";
        $html =  str_replace("{{list}}",$list,$html);
    };
    

    $list = implode("<br>",$products);
    $html =  str_replace("{{list}}",$list,$html);
    

    【讨论】:

    • 谢谢你,先生,你是一个英雄,它现在工作得很好我尝试使用 implode 但它倒退了或其他什么。再次感谢您
    【解决方案2】:

    当你这样做时:

    foreach($products as $product){
        $list = $product . "<br> test <br>";
        $html =  str_replace("{{list}}",$list,$html);
    };
    

    您实际上正在替换 n 次,但只有第一个会起作用,因为之后没有更多的“{{list}}”要替换。这不是错误,只是不是您所期望的。

    试试这样:

    $html =  str_replace("{{message}}",$message,$html);
    $list = '';
    foreach($products as $product){
        $list .= $product . "<br>";
    };
    $html =  str_replace("{{list}}",$list,$html);
    

    现在您正在构建一个包含所有选定产品的临时字符串变量,然后您只需将其替换一次。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-03-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-08-11
      相关资源
      最近更新 更多