在将数据发送到PHP脚本之前,Javascript重新加载页面

在将数据发送到PHP脚本之前,Javascript重新加载页面

问题描述:

Background: I want to create a login form, the user input the "user ID" and "password". My JavaScript file checks certain parameters. If the data does not comply with them the my page is re-load. If the data is approved then after checking in JavaScript my PHP script kicks in and does other stuff.

Question: the problem I am having is that even though the data is not complaint with the JavaScript check up my page is not re-loading. It keeps sending the data to my PHP script.

Which is want I do not want.

I want to re-loed the same page and not send to PHP. I want to send the data to PHP when the data has passed the JavaScript check up.

CODE:

HTML SECTION

enter image description here

JavaScript SECTION

enter image description here

Thank you

If you guys can suggest a way to approach this situation it would be great.

Thank you.

背景: strong> 我想创建一个登录表单,用户输入“ 用户ID“和”密码“。 我的 JavaScript em>文件检查某些参数。 如果数据不符合它们,我的页面将重新加载。 如果数据被批准,那么在检入JavaScript后,我的 PHP em>脚本将启动并执行其他操作。 问题: strong>我遇到的问题是即使数据没有投诉JavaScript检查我的页面也没有重新加载。 它不断将数据发送到我的PHP脚本。 p>

我想要的是什么。 p>

我想重新登录同一页面而不是发送给PHP。 我希望在数据通过JavaScript检查时将数据发送到PHP。 p>

代码: strong> p>

HTML SECTION strong> p>

p>

JavaScript SECTION strong > p>

p>

谢谢 strong> p>

如果你们可以建议一种方法来解决这种情况,那就太棒了。 p>

谢谢。 p> div>

Your approach is incorrect , you don't need to reload the page in case of none valid entries , I would suggest doing something similar to the following :

function checkForm(){
  if(document.getElementById("whatever").value != 'xyz') {
    console.log("False !")
    return false;
  }
  return true
}
<form action="something.php" onsubmit="return checkForm()">
<input type="text" id="whatever">
<input type="submit">
</form>

</div>