1

Background:

I have created a very simple front end, where the user can input strings. Once entered and "check" button is clicked, I would like to pass this string as a JSON to a python string where it will do a SQL look up. Based on the SQL look the python script should pass a boolean value which should change the ? to a ✔ or a ✘.

Question:

How can I pass on a string once the "check' button is pressed as a JSON to a Python script, and pass a Boolean from Python to HTML to change the ? to a ✔ or a ✘?

Research:

<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<style>
body {font-family: Arial, Helvetica, sans-serif;}
* {box-sizing: border-box;}

input[type=text], select, textarea {
  width: 100%;
  padding: 12px;
  border: 1px solid #ccc;
  border-radius: 4px;
  box-sizing: border-box;
  margin-top: 6px;
  margin-bottom: 16px;
  resize: vertical;
}

input[type=submit] {
  background-color: #4CAF50;
  color: white;
  padding: 12px 20px;
  border: none;
  border-radius: 4px;
  cursor: pointer;
}

input[type=submit]:hover {
  background-color: #45a049;
}

.container {
  border-radius: 5px;
  background-color: #f2f2f2;
  padding: 20px;
}


  h3 {text-align: center;}
  .center {
            display: flex;
            justify-content: center;
            align-items: center;
            }
    </style>

</style>
</head>
<body>

<h3>My Request</h3>

<div class="container">
  <form action="/action_page.php">
  
<label for="account_name">? Account Name:</label>
<input type="text" id="fname" name="firstname" placeholder="Account Name..">
<input type="submit" value="Check Account"><br><br>

<label for="contact_name">? Contact Name:</label>
<input type="text" id="lname" name="lastname" placeholder="Contact Name..">
<input type="submit" value="Check Contact"><br><br>


<label for="reseller">? Reseller:</label>
<input type="text" id="lname" name="lastname" placeholder="Reseller..">
<input type="submit" value="Check Reseller"><br><br>

<label for="issue_date">? Issue Date:</label><br>
<input type="date" id="start" name="trip-start" value="" min="2018-01-01" max="2100-12-31">

 
<br>
<div class="center">
    <input type="submit" value="VERIFY ALL">
</div>
  </form>
</div>

</body>
</html>
3kstc
  • 1,871
  • 3
  • 29
  • 53

1 Answers1

1

This is just a sample code, which will make you understand how to pass values from client to server as well as server to client.

Asumption: 'Flask' is you current working directory

Please follow the below steps:

  1. Install Flask

Run the below command

pip install Flask
  1. Create a python file app.py, copy paste the below content into this file.
from flask import Flask, render_template, request

app = Flask(__name__)


@app.route('/')
def index():
    return render_template('index.html')


@app.route('/greet', methods=['POST'])
def greet():
    name = request.form['name']
    return render_template('greet.html', name=name)


if __name__ == '__main__':
    app.run()
  1. Create a file index.html in location '/Flasak/templates' as
<h1>Welcome</h1>
<form action="http://localhost:5000/greet" method="POST">
    Name: <input type="text" name="name"> <button type="submit">Submit</button>
</form>
  1. Create a file greet.html in location '/Flasak/templates' as
<h2>Have a good day, {{name}}</h2>
  1. Run the python file like below
python app.py
  1. Open a browser and hit http://localhost:5000, it will display 'welcome', along with a field to input name. Provide your name and hit Submit. Like this you can send the values to server from client.

  2. After pressing Submit, redirection will happen, the server will receive name and send it again to client. Now you should be able to see Have a good day, along with the name provided.

Just for your reference the project's directory will be looking like below:

Flask
  |
  |-> templates
  |    |
  |    |-> greet.html
  |    |-> index.html
  |
  |-> app.py

Note: For better understanding, I would recommend you to go through tutorials. I hope this was helpful to you.

ngShravil.py
  • 4,742
  • 3
  • 18
  • 30
  • How would I make it so that the `

    Have a good day, {{name}}

    ` happens in the `index.html`when the user "submits" a name, and if they dont't enter a "name" but push submit, `

    Didn't get your name

    ` appears instead?
    – 3kstc May 19 '20 at 05:18
  • In that case, you don't need to send the data to server. So, it will be out of scope of the original question. Could you post another question and let me know. – ngShravil.py May 19 '20 at 07:17
  • I've created another question [here](https://stackoverflow.com/questions/62078985/updating-the-same-site-with-pythons-flask-module). It looks simple, but I just can't get my head around it. – 3kstc May 29 '20 at 04:57