다른 비슷한 글을 보려고했지만 분명히 PHP, HTML 및 자바 스크립트 기술이 너무 오래되어 눈치 채지 못했습니다. 마지막으로 나는 작년에 이러한 것들을 만졌고, 그래서 많은 결함 탐지 기술들이 녹슬 었습니다. 말했다되고 그건자바 스크립트가 PHP에서 실행되지 않습니다.
, 내가 할 노력하고있어 코드의 조각이
지금까지 내가 할 수있는 한, PHP 구문이 올바른 것 같다<?PHP //If?> JS //Script to check if input is empty and prevents submission HTML <!--Input Forms--> <?PHP //DB actions ?>
(사용되는 PHP와 같이 구성되어있다 formatter를 사용하여 구문이 잘못되었는지 확인하십시오.) 여기 문제는 자바 스크립트 부분도 실행되지 않습니다. 그리고 그것이 실행되면 아무 이유없이 (입력이 제공 될 때) 나머지 UI (로그인 폼)가 사라집니다.
아래 코드는 제가 사용하고있는 코드입니다.
<?php
if(empty($_POST["username"]) && empty($_POST["password"])){
?>
<script type="text/javacsript">
function checkField()
{
if((login-form.username.value == "") || (login-form.password.value == ""))
{
alert("Please fill in both Username and Password fields");
return false;
}
else
return true;
}
</script>
<div class="panel-body">
<form role="form" id="login-form" action="login.php" method="post" onsubmit="return checkField();">
<fieldset>
<div class="form-group">
<input class="form-control" placeholder="Username" id="username" type="text" autofocus>
</div>
<div class="form-group">
<input class="form-control" placeholder="Password" id="password" type="password" value="">
</div>
<!-- Change this to a button or input when using this as a form -->
<!--<a href="index.html" class="btn btn-lg btn-success btn-block">Login</a>-->
<div class="form-group">
<button class="btn btn-lg btn-success btn-block" type="submit">Login</button>
</div>
</fieldset>
</form>
</div>
<?php
}else {
$message = "wrong answer";
echo "1<script type='text/javascript'>alert('$message');</script>";
}
?>
onsubmit 용 JavaScript는 주요 관심사입니다. 가정 된 참 조건이 발생할 때 (입력 중 하나가 비어 있거나 모두 비어있는 경우) 실행되지 않습니다. 그리고 아래의 보조 스크립트는 데이터베이스 부분을 제공하기 전에 실행되는 초기 테스트입니다. 모두 로그인 및 패스워드 필드가없는 경우에서 getElementById
를 사용 checkField위한편집 1
대체 코드()
<script type="text/javacsript">
function checkField()
{
if((document.getElementById("username").value == "") || (document.getElementById("password").value == ""))
{
alert("Please fill in both Username and Password fields");
return false;
}
else
return true;
}
</script>
편집 2
오타를 해결 한 후, PHP 부분이 트리거되는 empty는 전혀 실행되지 않습니다. 액션 속성이 손을 가질 수도 있음
<form role="form" id="login_form" action="" method="post" onsubmit="return checkField();">
위의 코드는 조치 속성이 범인인지 테스트하는 코드입니다. 여전히 결과는 같습니다. 빈 필드가있는 페이지가 다시로드됩니다.
최신 코드
<?php
if(empty($_POST["username"]) && empty($_POST["password"])){
?>
<script type="text/javascript">
function checkField()
{
if((document.getElementById("username").value == "") || (document.getElementById("password").value == ""))
{
alert("Please fill in both Username and Password fields");
return false;
}
else
return true;
}
</script>
<div class="panel-body">
<form role="form" id="login_form" action="" method="post" onsubmit="return checkField();">
<fieldset>
<div class="form-group">
<input class="form-control" placeholder="Username" id="username" type="text" autofocus>
</div>
<div class="form-group">
<input class="form-control" placeholder="Password" id="password" type="password" value="">
</div>
<!-- Change this to a button or input when using this as a form -->
<!--<a href="index.html" class="btn btn-lg btn-success btn-block">Login</a>-->
<div class="form-group">
<button class="btn btn-lg btn-success btn-block" type="submit">Login</button>
</div>
</fieldset>
</form>
</div>
<?php
}else {
$conn = mysqli_connect("localhost","root","rootacc","test");
if($conn)
{
echo "success";
header("LOCATION:tables.html");
}
}
?>
편집 해결 3
모든 문제. 초보자 질문에 대해 유감스럽게 생각합니다. 기여한 모든 사람에게 감사드립니다. 다음은 수정 된 코드는 문제 1
문제 2 내 자신의 오타로 인해뿐만 아니라에서 getElementById에 대한 지식의 부족 (대신 변수 참조의 다른 스타일의 대시를 사용) 내 자신의 프로그래밍 sloppiness 때문이다
<?php
if(empty($_POST["username"]) && empty($_POST["password"])){
?>
<script type="text/javascript">
function checkField()
{
if((document.getElementById("username").value == "") || (document.getElementById("password").value == ""))
{
alert("Please fill in both Username and Password fields");
return false;
}
else
return true;
}
</script>
<div class="panel-body">
<form role="form" id="login_form" action="" method="post" onsubmit="return checkField();">
<fieldset>
<div class="form-group">
<input class="form-control" placeholder="Username" id="username" name="username" type="text" autofocus>
</div>
<div class="form-group">
<input class="form-control" placeholder="Password" id="password" name="password" type="password" value="">
</div>
<!-- Change this to a button or input when using this as a form -->
<!--<a href="index.html" class="btn btn-lg btn-success btn-block">Login</a>-->
<div class="form-group">
<button class="btn btn-lg btn-success btn-block" type="submit">Login</button>
</div>
</fieldset>
</form>
</div>
<?php
}else {
$conn = mysqli_connect("localhost","root","rootacc","test");
if($conn)
{
echo "success";
header("LOCATION:tables.html");
}
}
?>
그것은 발견되지 않았다.
문제 3은 ID와 이름에 대한 이해가 부족하기 때문입니다 (동일하게 보임)
문제를 해결하기위한 모든 권장 사항이 구현되었습니다.
콘솔에 오류가 있습니까? –
"자바 스크립트 부분도 실행되지 않습니다. 실행되면 ..."그래서 어떤가? 실행 여부? –
'login-form.username.value' -이 명령문은'login'의'form.username.value'를 배제합니다. 이것은 당신이 원하는 것이 아닙니다. 어쨌든 브라우저에서 id를 가진 요소를 참조하는 전역 변수를 자동 생성하는 것은 정말 나쁜 습관이기 때문에 입력 필드에 대한 참조를 얻기 위해'document.getElementById'를 대신 사용하고 싶습니다. – CBroe