简体   繁体   English

在 html 形式的 OnClick 属性中添加 href

[英]Add href in OnClick attribute in html form

I want to redirect to the python flask route home after showing the alert window.在显示警报 window 后,我想重定向到 python flask 路由home

<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 ``也许你可以为你的 HTML 代码使用反引号``

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.我认为您的 javascript 方法不足以解决此问题,因为数据必须首先从客户端发送到服务器才能完成该过程。 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.为了转发和显示消息,我使用了来自 flask package 的redirectflash

Python Flask 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) 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) 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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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