0

Im trying to link a js file in a HTML file, with the website running on localhost:5000, but it cant seem to locate the js file. Im using Flask as well, below is the HTML, python and js files: HTML:

<!DOCTYPE html>
<html>
    <script src="sign_up.js"></script>
    <head>
      <h1> You're only a few steps away now </h1>
    </head>
    <body>
    <div id="SignUpForm">
      <p id="head"></p>
      <h2>Enter your details to continue to MyRecipes</h2>
      <form action="{{url_for('addUser')}}" method ="post" onsubmit="return validateForm()">
        Full Name: <input type="text" name=""="name"><br><br>
        Email: <input type="text" name="email"><br><br>
        Username (max. 10 characters): <input type="text" name="username"><br><br>
        Password (min. 8 characters, at least 1 number and 1 upper-case letter): <input type="text" name="pass"><br><br>
        <input type="submit" value="Join MyRecipes">
      </form>
    </body>
</html>

python:

from flask import Flask, render_template, request
import sqlite3
app = Flask(__name__)

users = sqlite3.connect('users.db')
print("Opened database successfully")

@app.route("/", methods = ['GET', 'POST'])
def home():
    return render_template("index.html")

@app.route("/sign_up")
def signup():
    return render_template("signup.html")

@app.route('/addUser', methods=['POST'])
def addUser():
    passw = request.form['pass']
    print(passw)
    # your code
    # return a response
    return render_template("signup_success.html")

if __name__ == '__main__':
    app.run(debug=True)

js:

function validateForm(){
    var name = document.getElementById('name');
    if (name.value.length == 0){
        document.getElementById('head').innerText = "* All fields necessary *";
        name.focus();
        return false;
    }
    return true
}

Ive tried a lot of different other paths to src"", but nothing seems to work, can anyone see the issue here?

Edit: Forgot to say, the sign_up.js file is in the templates folder, along with the other html files.

Roark
  • 69
  • 6
  • try with `` and place it below the last `` tag. – Tony Jun 25 '21 at 11:09
  • It said `Could not build url for endpoint 'templates' with values ['filename']. Did you mean 'static' instead`, you missed a bracket in that, so placed it just before the '}}' – Roark Jun 25 '21 at 11:14
  • move the `sign_up.js` in the `static` folder where your `css` resides. Then `` – Tony Jun 25 '21 at 11:15
  • I think that worked, its found the js file, I can see it under sources when I inspect element. But im not sure if its running it. I have an `onsubmit='return validateForm()` function under `
    `, but it doesnt seem to be running.
    – Roark Jun 25 '21 at 11:21
  • When you submit the form in the terminal does it print the pass? You have `print(passw)` in the route. And I think your route should be with `request.args['pass']` – Tony Jun 25 '21 at 11:41
  • I tried args, it did not work, but yes the pass is printing in my terminal. It seems the error is in the line name.value.length, it says: `cannot read property 'value' of null'. But its giving this error before I even press submit. And when I press submit, it flows to the signup_success page. – Roark Jun 25 '21 at 11:54
  • Your `onsibmit` should be `onsubmit="validateForm()"` not `return validateForm()` – Tony Jun 25 '21 at 11:54
  • Okay yeah, now it runs when I submit, i.e. click the submit button, but its still giving teh cant read value of null error, even though a name has been entered. – Roark Jun 25 '21 at 11:59
  • look here `name=""="name"` this should be `name="name"` and are you filling all the form fields? – Tony Jun 25 '21 at 12:00
  • Fixed that, yep i added the `required` argument in all the `` lines in html file, so all are being filled out. Still same error. – Roark Jun 25 '21 at 12:03
  • read [here](https://stackoverflow.com/questions/54611810/how-to-fix-cannot-read-property-value-of-null-with-an-input-in-javascript) – Tony Jun 25 '21 at 12:07
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/234196/discussion-between-roark-and-t0ny1234). – Roark Jun 25 '21 at 12:10
  • Shouldnt it be on head section ? – WNR Jun 25 '21 at 12:51
  • The error is because there is no input with id name add I'd to the input you want to validate – charchit Jun 25 '21 at 13:13

0 Answers0