【问题标题】:How to retrieve Posted JSON , construct string, and email in PHP如何在 PHP 中检索 Posted JSON、构造字符串和电子邮件
【发布时间】:2020-02-14 21:41:31
【问题描述】:

应用概览

这是一个简单的“get-a-quote”页面,用户在其中输入零件编号、所需的数量,以及任何可选的注释。


先前的研究 对于与我类似的问题,我确保彻底尝试在 StackOverflow 上找到的解决方案。我尝试了很多,包括以下内容(我发布这个是为了向你保证,在发布这个问题之前我已经尽力了):

  1. php: loop through json array
  2. How to loop through json data php
  3. loop through JSON string in php

(还有更多,我试过了)- 话虽这么说......


问题

我无法将表单值发布到我的 PHP 流程页面。我正在使用 JQuery Ajax 发布。具体来说,我不知道如何访问“部分”,它可能包含也可能不包含多个。

期望的结果

检索所有表单数据,并使用 PHP 发送电子邮件。

我不需要电子邮件部分的帮助,只需要表单值。

PHP 处理程序


// Receive JSON
$json = file_get_contents('php://input');

// Converts it into a PHP object
$data = json_decode($json, true);

foreach($data as $key => $value) 
{
    echo $key.' value of the key is:'.$value;


}

已发布的值

来自 PHP 页面的响应

name value of the key is:Johnemail value of the key is:john@gmail.comphone value of the key is:808-333-3333<br />
<font size='1'><table class='xdebug-error xe-notice' dir='ltr' border='1' cellspacing='0' cellpadding='1'>
<tr><th align='left' bgcolor='#f57900' colspan="5"><span style='background-color: #cc0000; color: #fce94f; font-size: x-large;'>( ! )</span> Notice: Array to string conversion in C:\wamp64\www\123NEW\process.php on line <i>11</i></th></tr>
<tr><th align='left' bgcolor='#e9b96e' colspan='5'>Call Stack</th></tr>
<tr><th align='center' bgcolor='#eeeeec'>#</th><th align='left' bgcolor='#eeeeec'>Time</th><th align='left' bgcolor='#eeeeec'>Memory</th><th align='left' bgcolor='#eeeeec'>Function</th><th align='left' bgcolor='#eeeeec'>Location</th></tr>
<tr><td bgcolor='#eeeeec' align='center'>1</td><td bgcolor='#eeeeec' align='center'>0.0006</td><td bgcolor='#eeeeec' align='right'>406152</td><td bgcolor='#eeeeec'>{main}(  )</td><td title='C:\wamp64\www\123NEW\process.php' bgcolor='#eeeeec'>...\process.php<b>:</b>0</td></tr>
</table></font>
parts value of the key is:Array

应用预览

理想情况下,我希望得到一个可以通过电子邮件发送的格式化字符串:

Name: John
Email: john@gmail.com
Phone: 800-555-1212
Parts: 1.  PartNumber:p1 -- Qty: 1 -- Notes: No notes
       2.  PartNumber:p2 -- Qty: 3 -- N/A

感谢收看。任何帮助将不胜感激。

注意: 我正在使用KnockoutJS,所以我可以利用他们的 2 路数据绑定。不确定这是否会影响对这个问题的回答。

【问题讨论】:

    标签: php json knockout.js


    【解决方案1】:

    真的没什么。您可能希望在代码中执行此操作,只要您有对视图模型的引用,就可以根据需要读取属性来生成字符串。

    这可以在一个计算的 observable 中,或者只是一个常规的单独函数来生成字符串。

    _emailContent() {
        const {name,email,phone,parts} = this.customer();
        return `Name: ${name()}
    Email: ${email()}
    Phone: ${phone()}
    Parts:
      ${outputParts(parts)}
    `;
        function outputParts(parts) {
            return ko.utils.arrayMap(parts(), ({partNumber,qty,notes}, i) =>
                `${i+1}.\tPartNumber: ${partNumber()} -- Qty: ${qty()} -- Notes: ${notes()}`
            ).join('\n  ');
        }
    }
    

    我还制作了一个小提琴来演示它:https://jsfiddle.net/3j1arh04/1/

    【讨论】:

    • 嘿,杰夫,非常感谢,伙计!这不仅有助于解决这个紧迫的问题,而且您的代码还为我提供了一些与我的视图模型交互的新方法。像您在示例中那样使用纯类似乎是一种干净、现代的方法。我肯定会用它作为模型来研究我未来的视图模型。再次感谢,很棒的解决方案!
    • 太棒了!很高兴我能帮上忙。
    猜你喜欢
    • 2013-12-11
    • 2023-04-07
    • 2016-06-17
    • 2023-03-12
    • 2017-04-03
    • 2013-03-12
    • 1970-01-01
    • 1970-01-01
    • 2013-02-09
    相关资源
    最近更新 更多