简体   繁体   中英

Add href in OnClick attribute in html form

I want to redirect to the python flask route home after showing the alert window.

<input type="submit" value="Register" onclick="call(); log();"></input>

Here are both functions.

<script>

  function call()
  {
    window.alert("Congragulations! You Registerd Sucessfully ");
  }
  
</script>
<script>

  function log()
  {
    <a href="/home" > </a>
  }
  
</script>

Maybe you can use backticks for your HTML code ``

If I understood your project correctly, you want to forward the user after he has registered. At the same time, a message should appear that the process was successfully completed.

I think your javascript approach is not sufficient for this, since the data must first be sent from the client to the server so that the process is complete. Here the inputs should also be validated and will likely be processed or stored in some way.

I have written a small example for you, which accepts form data and checks it. If the verification is successful, the user is redirected and a message appears. To forward and display the message I use redirect and flash from the flask package.

Python Flask

import re
from flask import Flask
from flask import (
    flash,
    redirect,
    render_template,
    request,
    session,
    url_for
)

app = Flask(__name__)
app.secret_key = b'your secret here'

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

@app.route('/register', methods=['GET', 'POST'])
def register():
    if request.method == 'POST':
        username = request.form.get('username')
        if username and re.match(r'^[a-z][a-z0-9\-\_]+$', username, re.IGNORECASE):
            session['username'] = username
            flash('Congragulations! You Registerd Sucessfully.')
            return redirect(url_for('home'))
    return render_template('register.html')

HTML (index.html)

<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
    <title></title>
  </head>
  <body>
    <nav>
      <ul style="list-style: none;">
        <li><a href="{{ url_for('register') }}">Register</a></li>
      </ul>
    </nav>

  {% with messages = get_flashed_messages() -%}
    {% if messages -%}
    <ul class="flashes">
      {% for message in messages -%}
      <li>{{ message }}</li>
      {% endfor -%}
    </ul>
    {% endif -%}
  {% endwith -%}

    <h1>Welcome {{ session.get('username', 'Guest') }}!</h1>
  </body>
</html>

HTML (register.html)

<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
    <title>Register</title>
  </head>
  <body>
    <form method="post">
      <div>
        <label for="username">Username</label>
        <input type="text" name="username" id="username" />
      </div>
      <input type="submit" value="Register" />
    </form>
  </body>
</html>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM