Reputation: 355
I displayed the data's from database
{% extends "layout.html" %}
{% block content %}
<article class="media content-section">
<div class="table-responsive">
<div class="table table-striped">
<table>
<thead>
<tr>
<th>Name</th>
<th>Phone Number</th>
<th> Send SMS </th>
</tr>
</thead>
{% for detail in details%}
<tbody>
<tr>
<th>{{ detail.username }}</th>
<th>{{ detail.phonenumber }}</th>
<th><button type="button" class="btn btn-success" onclick="'{{ url_for('sendsms', phonenumber = detail.phonenumber)}}'">Send Request</button></th>
</tr>
</tbody>
{% endfor %}
</table>
</div>
</article>
{% endblock content %}
HOW TO CALL THE FUNCTION AND PASS THE PHONE NUMBER WHEN THE BUTTON IS CLICKED?
When i pressed that button the sendsms function should called and the phonenumber should passed to this function.
My Route is
def sendsms(phonenumber):
account_sid = '************************'
auth_token = '*************************'
client = Client(account_sid, auth_token)
message = client.messages.create(
from_= current_user.username,
body='i need immediately'
to= phonenumber)
print(message.sid)
Upvotes: 1
Views: 3049
Reputation: 336
Sure, let's go. First, let's add form elements to your HTML code.
{% for detail in details%}
#let's make a bunch of forms for every detail so you can send separate data for every request.
<form action="{{ url_for('sendsms') }}" method="post">
<tbody>
<tr>
<th><input type="text" name="username" value={{ detail.username }} required></th>
<th><input type="text" name="phonenumber" value={{ detail.phonenumber }} required></th>
<th><input type="submit" name="button" class="btn btn-success" value="Send Request"></a></th>
</tr>
</tbody>
</form>
{% endfor %}
Afterwards let's make our backend process POST requests.
#Let's make sure that our route handles POST requests so add POST method to route:
@app.route('/sendsms', methods=['POST'])
def sendsms():
account_sid = '************************'
auth_token = '*************************'
client = Client(account_sid, auth_token)
#You can access values you sent in form using request.form data:
phonenumber = request.form['phonenumber']
username = request.form['username']
message = client.messages.create(
from_= username,
body='i need immediately'
to= phonenumber)
print(message.sid)
P.S. This code probably should work, but since i can't check it - this will at least give you a hint. Good luck!
Upvotes: 2