使用烧瓶的html页面中有两种表单,仅一种表单提交

我在html页面中有两种形式,两种形式都是用于登录,一种是针对学生,一种是针对教师。 我正在使用Flask和HTML5创建一个网站。我的问题是,只有我的学生登录名有效,而没有老师的登录名部分可以帮助我!代码或Flask有什么问题吗?只能采用一种形式的值?

HTML代码:

<script src="/static/js/signin.js"></script>
<script src="/static/js/signinteacher.js"></script>

<div class="w3-row">
      <form class="w3-col s6 w3-padding-large w3-border-right w3-rightbar" style="height:570px" action="/validatestudentlogin" method="post">
        <h3><b>STUDENT LOGIN</b></h3>
        <label for="inputMoodle" class="w3-text-teal w3-section"><b>Moodle ID</b></label>
        <input id="number" class="w3-input w3-border w3-light-grey" type="text" name="inputMoodle" id="inputMoodle" style="width:50%" required autofocus><br>

        <label for="inputPassword" class="w3-text-teal w3-section"><b>Password</b></label>
        <input class="w3-input w3-border w3-light-grey" type="password" name="inputPassword" id="inputPassword" style="width:50%" required><br>

        <button id="btnSignIn1" class="w3-btn w3-blue-grey w3-section" type="submit">Login</button>
      </form>

      <form class="w3-col s6 w3-padding-large" >
        <h3><b>TEACHER LOGIN</b></h3>
        <label for="inputTeacher" class="w3-text-teal w3-section"><b>Teacher ID</b></label>
        <input class="w3-input w3-border w3-light-grey" type="text" name="inputTeacher" id="inputTeacher" style="width:50%" required autofocus><br>

        <label for="inputPassword1" class="w3-text-teal w3-section" for="pwd"><b>Password</b></label>
        <input class="w3-input w3-border w3-light-grey" type="password" name="inputPassword1" id="inputPassword1" style="width:50%" required><br>

        <button id="btnSignIn2" class="w3-btn w3-blue-grey w3-section" type="submit">Login</button>
      </form>

    </div>

signinteacher.js

$(function(){
    $('#btnSignIn2').click(function(){
        console.log("This is working so far");
        $.ajax({
            url: '/validateteacherlogin',
            data: $('form').serialize(),
            type: 'POST',
            success: function(response){
                console.log(response);
            },
            error: function(error){
                console.log(error);
            }
        });
    });
});

app.py

@app.route('/dashboard')
def dashboard():
    if session.get('user'):
        return render_template('dashboard.html')
    else:
        return render_template('error.html',error = 'Unauthorized Access')

@app.route('/validatestudentlogin',methods=['POST'])
def validatestudentlogin():
    try:
        _moodleid = request.form['inputMoodle']
        _password = request.form['inputPassword']        


        con = mysql.connect()
        cursor = con.cursor()
        cursor.callproc('sp_validateLogin',(_moodleid,))
        data = cursor.fetchall()

        username=data[0][1]

        if len(data) > 0:
            if check_password_hash(str(data[0][2]),_password):
                session['user'] = data[0][0]
                return render_template('dashboard.html', moodleid= username)
            else:
                return render_template('error.html',error = 'Invalid Moodle ID or  wrong Password.')
        else:
            return render_template('error.html',error = 'Invalid Moodle ID or wrong Password.')


    except Exception as e:
        return render_template('error.html',error = str(e))
    finally:
        cursor.close()
        con.close()

@app.route('/teacherportal')
def dashboard1():
    if session.get('user'):
        return render_template('teacher_home.html')
    else:
        return render_template('error.html',error = 'Unauthorized Access'

@app.route('/validateteacherlogin',methods=['POST'])
def validateteacherlogin():
    try:
        _teacherid = request.form['inputTeacher']
        _password = request.form['inputPassword1']        


        con = mysql.connect()
        cursor = con.cursor()
        cursor.callproc('sp_validateTeacherLogin',(_teacherid,))
        data = cursor.fetchall()

        username = data[0][1]


        if len(data) > 0:
            if check_password_hash(str(data[0][2]),_password):
                session['user'] = data[0][0]
                return render_template('teacher_home.html', moodleid= username)
            else:
                return render_template('error.html',error = 'Invalid Moodle ID or  wrong Password.')
        else:
            return render_template('error.html',error = 'Invalid Moodle ID or wrong Password.')


    except Exception as e:
        return render_template('error.html',error = str(e))
    finally:
        cursor.close()
        con.close()
评论
  • id_cum
    id_cum 回复

    看来您可能忘记了在HTML代码的老师登录表单中包含动作和方法属性。您需要添加这些。