Form to Retrieve Data

HTML code

<html>
<head>

<style>

body {
  background-color: yellow;
   font-family:Lucida Sans Unicode;
  }

.center {
  text-align: center;
  color: Brown;
}

ul {
  list-style-type: none;
  margin: 0;
  padding: 0;
  overflow: hidden;
  background-color: orange;
}

li {
  float: left;
}

li a {
  display: block;
  color: brown;
  text-align: center;
  padding: 14px 16px;
  text-decoration: none;
}

li a:hover {
  background-color: yellow;
}

</style>
</head>

<h1>

</h1>

<h1>Click on  Bagel for today's Price Information</h1>

<br>
<br>


<form method="POST" action="https://j7qtefdvoh.execute-api.us-east-1.amazonaws.com/prod">

   <label>Bagel:</label>
<select name="my-field">
        <option value="plain">plain</option>
            <option value="wheat">wheat</option>
           
</select>
<BR>

    <input type="submit" value="Submit form">
<br>
<br>
<img src="images/bakery.jpg" alt="Plain Bagel" width="1200" height="400">
</form>
</html>


Lambda function to retrieve dynamodb data and display web page

const AWS = require('aws-sdk');
const querystring = require('querystring');
global.my_field;
global.docCleint;
let docClient = new AWS.DynamoDB.DocumentClient({region: 'us-east-1'});

// Set this to the region you upload the Lambda function to.
AWS.config.region = 'us-east-1';

exports.handler = function(evt, context, callback) {
    // Our raw request body will be in evt.body.
    const params = querystring.parse(evt.body);

    // Our field from the request.
     let my_field = params['my-field'];
 
    var prec = {
        TableName: 'Products',
        Key: {
            "bname": my_field
        }
       
        };
    docClient.get(prec, function(err, data){
        if (err){
            callback(err, null);
        }else{
            // JSON.stringify(data, null, 2)
           
            const html = `<!DOCTYPE html><html>
           <head>
<style>
body {
  background-color: yellow;
   font-family:Lucida Sans Unicode;
  }

.center {
  text-align: center;
  color: Brown;
}
</style>
</head>
<h1>

</h1>
<br>
<br>
<br>
<h1 class="center">Today's Bagel Price</h1>


<p>Price Per Dozen  ` + data.Item.price + `</p>`;
            callback(null, html);
        }
        });
        };
       
       

No comments:

Post a Comment

To All, If you are working in starter accounts please move the work into the classroom account so I can review. For the previous ...