1

App Overview

This is a simple 'get-a-quote' page, where the user enters in a part number, quantity desired, and any optional notes.


Prior Research I made sure to thoroughly try solutions found here on StackOverflow regarding a problem similar to mine. I tried many, including the following ( I am posting this to assure you that I tried my best before posting this question ):

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

( and many more, I tried ) - That being said...


Problem

I cannot get the form values posted to my PHP process page. I am POSTing using JQuery Ajax. Specifically, I can't figure out how to access the 'parts', which may, or may not, contain more than one.

Desired Outcome

To retrieve all the form data, and email it using PHP.

I don't need assistance with the emailing portion, just the form values.

The PHP handler


// 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;


}

Values POSTED

Console1

Response From PHP page

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

Application Preview

Preview

Ideally, I would like to end up with a formatted string I can email:

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

Thanks for looking. Any help would be appreciated.

Note: I am using KnockoutJS so I can leverage their 2-way data-binding. Not sure if that impacts the answer to this question.

John S.
  • 504
  • 1
  • 3
  • 18

1 Answers1

1

There really isn't much to it. You probably will want to do that in code so as long as you have a reference to your view model, you could read the properties as needed to generate your string.

This could be in a computed observable or just a regular separate function to generate the string.

_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  ');
    }
}

I also cooked up a fiddle demonstrating it: https://jsfiddle.net/3j1arh04/1/

Jeff Mercado
  • 129,526
  • 32
  • 251
  • 272
  • Hey Jeff, thanks so much, man! Not only did that help with this immediate problem, your code also gave me some new ways to interact with my view model . Using pure classes like you did in your example seems like such a clean, modern approach. I definitely will be using that as a model to study for my future viewmodels. Thanks again, awesome solution! – John S. Oct 18 '19 at 03:30
  • Great! Glad I could help. – Jeff Mercado Oct 18 '19 at 04:32