如何在 PHP 中检索已发布的 JSON、构建字符串和电子邮件
How to retrieve Posted JSON , construct string, and email in PHP
应用概览
这是一个简单的“获取报价”页面,用户在其中输入零件号,需要的数量,以及任何可选的注释。
前期研究
我确保彻底尝试在 Whosebug 上找到的解决方案,了解与我类似的问题。我尝试了很多,包括以下(我发布这个是为了向你保证我在发布这个问题之前已经尽力了):
- php: loop through json array
(还有很多,我试过了)- 话虽这么说...
问题
我无法获取发布到我的 PHP 流程页面的表单值。我正在使用 JQuery Ajax 发布。具体来说,我不知道如何访问 'parts',它可能包含也可能不包含多个
期望的结果
检索所有表单数据,并使用 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\www3NEW\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\www3NEW\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 向数据绑定。不确定这是否会影响此问题的答案。
真的没什么。您可能希望在代码中执行此操作,因此只要您有对视图模型的引用,就可以根据需要读取属性以生成字符串。
这可以在计算的可观察对象中,也可以只是一个常规的单独函数来生成字符串。
_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 ');
}
}
我还做了一个 fiddle 演示:https://jsfiddle.net/3j1arh04/1/
应用概览
这是一个简单的“获取报价”页面,用户在其中输入零件号,需要的数量,以及任何可选的注释。
前期研究 我确保彻底尝试在 Whosebug 上找到的解决方案,了解与我类似的问题。我尝试了很多,包括以下(我发布这个是为了向你保证我在发布这个问题之前已经尽力了):
- php: loop through json array
(还有很多,我试过了)- 话虽这么说...
问题
我无法获取发布到我的 PHP 流程页面的表单值。我正在使用 JQuery Ajax 发布。具体来说,我不知道如何访问 'parts',它可能包含也可能不包含多个
期望的结果
检索所有表单数据,并使用 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\www3NEW\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\www3NEW\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 向数据绑定。不确定这是否会影响此问题的答案。
真的没什么。您可能希望在代码中执行此操作,因此只要您有对视图模型的引用,就可以根据需要读取属性以生成字符串。
这可以在计算的可观察对象中,也可以只是一个常规的单独函数来生成字符串。
_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 ');
}
}
我还做了一个 fiddle 演示:https://jsfiddle.net/3j1arh04/1/