这可能是一个有点奇怪的问题,但我有一些奇怪的情况。基本上,我有一个带有文本输入的html页面,旨在将某些输入与SQL数据库匹配,但必须首先检查输入是否为null。这是一种仅使用用户名的模拟登录系统。
我的问题来自这样一个事实,即我只需要将 PHP 用于数据库查询和传递值,例如文本字段中的内容。因此,我将表单设置为将 php 作为其操作,并制作了一个 javascript 文件,该文件首先运行其代码并检查 null。javascript 工作得很好,但是在它返回的警报关闭后,将加载 php 页面(它还没有包含任何代码,因为我需要先解决这个问题(。
有谁知道我如何解决它,以便用户被迫回到页面并重置 PHP 和 javascript?我尝试了一些试图重新加载页面的javascript命令,但PHP仍然激活。我读到AJAX可以处理这个问题,但是由于某种原因,我在让JQuery工作时遇到了很多麻烦。
很抱歉这个非常奇怪的问题,老实说,如果我能为此使用 PHP,那就太好了。无论如何,感谢您的任何和所有帮助,非常感谢!
JavaScript:
function loginEmpty() {
var login = document.getElementById("profileName").innerHTML;
if (login==null || login=="")
{
alert("You did not enter a username");
return false;
}
}
.HTML:
<script src="YSP.js" type="text/javascript"></script>
</head>
<body>
<div id="header">
<h1>You SHALL Pass!</h1>
</div>
<div id="navBar" class="mainScheme">
<!--Navbar content goes here-->
</div>
<div id="main" class ="mainScheme">
<h2>User Login</h2>
<!--Old user login - name match from DB-->
<form id="loginForm" action="You_Shall_Pass.php" method="post">
<fieldset>
<legend>Log in to a profile</legend>
<label for="profileName">User name: </label>
<input id="profileName" name="profileName" type="text" />
<input type="submit" id="loginButton" name="loginButton" value="Log in!" onclick="loginEmpty();" />
</fieldset>
</form>
<!--New user login - Creates a new user in DB-->
<form id="loginForm" action="You_Shall_Pass.php" method="post">
<fieldset>
<legend>New user? Create a user name here!</legend>
<label for="newProfileName">User name:</label>
<input id="newProfileName" name="newProfileName" type="text" />
<input type="submit" id="createButton" name="createButton" value="Create!" onclick="newUserEmpty();" />
</fieldset>
</form>
</div>
</body>
</html>
当单击表单内type="submit"
的按钮/输入元素时,封装该按钮的表单将调用表单的 action
属性。如果您不希望发生这种情况,则可以使用 <div>
元素并使用 CSS 设置其样式,使其看起来像一个按钮。当div 被点击时,表单将不会提交。
如果您希望 JQuery 工作,请在您的头标签中包含以下两行
<script src="/path/to/scripts/jquery.js"></script>
<script src="/path/to/scripts/yourjsfile.js"></script>
将您的提交按钮替换为 <div id="SubmitButton"></div>
。
然后在你的JavaScript文件中,你把
$(function(){ // $ means JQuery, so its a shortcut for JQuery(document).ready()
$("#SubmitButton").click(function(){
// function will run when SubmitButton is clicked
var DataIsValid = false;
if(/*Perform your checks here */)
DataIsValid = true;
//end function if checks failed
if(!DataIsValid)
return;
//perform an ajax call to send your data to the
//webserver where you can execute your PHP code
});
});