1

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)
2
  • You can wrap this values in an html form which will trigger a POST request to your "sendsms" endpoint on backend. This could be also done using JS. Commented Mar 9, 2019 at 12:38
  • Thank you Vladimir Vishnevskyi. May you please show any example?? Commented Mar 9, 2019 at 12:58

1 Answer 1

2

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!

Sign up to request clarification or add additional context in comments.

2 Comments

Thank you so much Vladimir Vishnevskyi
@Tamilselvan If this answer helped you, you should accept it

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.