Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/376.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/356.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript request.form.get返回None而不是input_Javascript_Python_Html_Flask - Fatal编程技术网

Javascript request.form.get返回None而不是input

Javascript request.form.get返回None而不是input,javascript,python,html,flask,Javascript,Python,Html,Flask,我正在创建一个游戏,在这个游戏中,有人输入一个单词(生成搜索),你必须根据生成的结果猜出它是什么单词。 它的工作原理是获取玩家1输入的单词(输入1)并将其与您的猜测(输入2)进行比较 当用户提交input1时,我的python代码将获取并存储它。 但是,当第二个播放器输入input2并提交时,input1被覆盖,request.form.get=None(尽管它可以很好地获得input2) 发生了什么,我如何修复它?谢谢(PS:我知道这看起来像是很多代码,但大部分是用于上下文) HMTL 不要对

我正在创建一个游戏,在这个游戏中,有人输入一个单词(生成搜索),你必须根据生成的结果猜出它是什么单词。 它的工作原理是获取玩家1输入的单词(输入1)并将其与您的猜测(输入2)进行比较

当用户提交input1时,我的python代码将获取并存储它。 但是,当第二个播放器输入input2并提交时,input1被覆盖,request.form.get=None(尽管它可以很好地获得input2)

发生了什么,我如何修复它?谢谢(PS:我知道这看起来像是很多代码,但大部分是用于上下文)

HMTL


不要对
inp
guess
使用全局变量,而是使用会话cookie:

from flask import session

...

session['inp'] = request.form.get('input1')

...

session['guess'] = request.form.get('input2')

...

if session['inp'] == session['guess]:
   # do stuff

在全局变量中存储值不是您想要在这里做的事情;它的行为方式与您认为的不一样,也不太实际。

那么您可以在提交两个输入时同时获得它们吗?但问题是你在第二个输入(猜测输入)之后丢失了第一个输入,对吗?@JakeJackson是的,当我提交第一个输入时,没关系;但是当我提交第二个时,第一个变为无,第二个就可以了。在javascript上,值是存在的,是python无法获取,我想既然您将第一个输入存储到
inp
,为什么会出现问题?我不知道…当我提交第二个输入时,python再次发出
请求.form.get
,但由于某种原因,这次它返回
。我试图通过将input1的值存储在js中的常量值中来覆盖该
None
值,但该值没有传递给python。。。
<script>
  let is = document.getElementById('innerSpace');
  let input1 = document.getElementById('input1');
  let input2 = document.getElementById('input2');

  let playern = document.getElementById('playern');
  let cont1 = document.getElementById('cont1');
  let cont2 = document.getElementById('cont2');

  let spacer = document.getElementById('spacer');

  input1.addEventListener('change', searchFor);
  function searchFor() {
    //create constant variable with the value of input1
    const inp = input1;
    playern.innerHTML = 'Player 2';
    playern.style.marginTop = '0px';
    // hide first form and show the second one
    cont1.style.display = 'none';
    cont2.style.display = 'flex';
    cont2.style.flexDirection = 'column';
    cont2.style.justifyContent = 'center';
    cont2.style.alignItems = 'center';
    spacer.style.display ='none';
    is.style.display = 'flex';
    // search results
    let query = encodeURIComponent(input1.value);
    is.src = `https://search.givewater.com/serp?qc=images&q=weirdest+${query}`;
    // when user submits guess
    input2.addEventListener('change', function(){
        // trying to override None value by replacing with const value
        input1.value = inp.value;
        // tryin to re-submit input1
        document.getElementById("form1").submit();
    });
  }
@app.route('/multi', methods =['POST', 'GET'])
@login_required
def multiplayer():

    #If user got here from link
    if request.method == 'GET':
        return render_template('multiplayer.html')

    #If user inputed something
    if request.method == 'POST':

        global inp
        inp = request.form.get('input1')

        #this stops the whole page from loading before user submits a guess
        while not request.form.get('input2'):
            #wait
            print('.', end='')
            time.sleep(1)
            #if guess exists
            if request.form.get('input2'):
                break

        ui = inp

        #Get guess
        guess = request.form.get('input2')

        #Get User id
        userId = session['user_id']

        #Get current score from user (and id)
        rows = db.execute('SELECT * FROM scores WHERE user_id = ?', userId)

        #if this user hasn't played yet
        ##add them to the table with a score of 0
        if not rows:
            db.execute('INSERT INTO scores (user_id, score) VALUES (?, ?)', userId, 0)
            rows = db.execute('SELECT * FROM scores WHERE user_id = ?', userId)

        #Get score of current user
        points = rows[0]['score']


        #If correct guess, increase points by 1
        if guess == inp:
            points += 1
            #Update score
            db.execute('UPDATE scores SET score = ? WHERE user_id = ?', points, userId )
            #copy to another variable
            w = inp
            g = guess
            s = points
            return render_template('results.html', title = 'Correct!', t1 = 'The search was', word = w, t2 = 'Your guess was', guess = g, score = s)
        #If wrong guess
        else:
            w = inp
            g = guess
            s = points
            return render_template('results.html', title = 'Incorrect', t1 = 'The search was', word = w, t2 = 'Your guess was', guess = g, score = s)
from flask import session

...

session['inp'] = request.form.get('input1')

...

session['guess'] = request.form.get('input2')

...

if session['inp'] == session['guess]:
   # do stuff