2013-09-02 3 views
0

OK 오류 메시지 자바 스크립트를 사용합니다. 나는 두 필드가 채워 것을 확인하기 mail_check.php을 사용하고 있습니다. (더 많은 것을 검증하지만 포함되지되고있다는 문제 아니므로)PHP 양식 유효성 검사,

주요 문제는에서이다 mail_check.php은 내가 사업부 아이디 = "mailrespond"에 배치의 메시지와 함께 다시 index.php를에 전송하고 싶다. 이를 달성하기 위해 PHP와 Javascript를 모두 선택했습니다.

Index.php는

<div id="mailrespond"></div> 
    <form method="post" action="mail_check.php"> 
    <span>Name: <input type="text" name="name" maxlength="30" /></span><br /> 
    <span>Email: <input type="text" name="email" maxlength="30" /></span><br /> 
    <input type="submit" value="Sign Up" name="registration"> 
    </form> 
</div> 

mail_check.php는

if(isset($_POST['registration'])) 

$name = $_POST['name']; 
$email = $_POST['email']; 


if(empty($email) || empty($name)){ 
    // if email and name are empty 
    //header ("location: index.php"); 
    ?> 
    <script language="javascript" type="text/javascript"> 
     window.location.replace("index.php"); 

     function msg() { 
      // creating elements are a safer method then innerHTML 
      dv = document.createElement('div'); // creates a Div element 
      dv.setAttribute('class', 'error_msg'); // adds error styling 
      txt = document.createTextNode('please enter your name and email '); // create the message 
      dv.appendChild(txt); // place the text node on the element 

      document.getElementById("mailrespond").appendChild(dv); 
      } 
      window.onload = msg; 

    </script> 
    <?php } 

코드는 계속하지만, 내 문제는 내가 메시지를 피드를 다시 받고 있지 않다 때문이다. 나는이 모든 것을 조금 새로운 것입니다. :)

+0

실행하기 전에 리디렉션되고 있습니다. uting 기능 MSG()는 – Shadow

+0

당신은 다른 코드 전에 location.replace를 배치 할 수 없습니다 당신은 온로드 당신이 어떤 자바 스크립트 **하지 않고 시스템 작업 **을 받으셨어요 – mplungjan

+0

당신이 지정 함수 내 온로드하는 기능을 할당 할 수 없습니다? –

답변

0

< ---- @downvoter 이유를 남겨주세요!

감사이 시도

Index.php는

<!DOCTYPE html> 
<html> 
<head> 
<title>Mail respond</title> 
<script> 
window.onload=function() { 
    document.getElementById("mailform").onsubmit=function(){ 
    if (this.fullname.value=="" || this.email.value=="") { 
     alert("Please fill in name and email"); 
     return false; 
    } 
    return true; // allow form submission 
    } 
} 
</script> 
</head> 
<body> 
<div id="mailrespond"></div> 
    <form method="post" action="mail_check.php" id="mailform"> 
    <span>Name: <input type="text" name="fullname" maxlength="30" /></span><br /> 
    <span>Email: <input type="text" name="email" maxlength="30" /></span><br /> 
    <input type="submit" value="Sign Up" name="registration"> 
    </form> 
</div> 
</body> 
</html> 

mail_check (및 양식 필드 이름를 호출하지 마십시오 형태는 이름도있을 수 있습니다하십시오 때문에). PHP

<?PHP 
    if(isset($_POST['registration'])) 
    $name = $_POST['fullname']; 
    $email = $_POST['email']; 


    if(empty($email) || empty($name)){ 
    // if email and name are empty - only seen if JavaScript is turned off 
?> 

    <h3>You did not fill in name and email</h3> 
    <p>please wait to be redirected or click <a href='index.php'>here</a></p>; 
    <meta http-equiv="refresh" content="3; url=http://example.com/index.php" /> 
    <script type="text/javascript"> 
    window.onload=function() { 
     setTimeout(function() { 
      window.location.replace("index.php"); 
     },3000); 
    } 
</script> 
    <?php } ?> 
+0

그리고 여기에 우리는 다시 간다. 이유를 말하지 않고 downvoting!? – mplungjan

+0

도움을 주셔서 감사합니다 mplungjan, 나는 내 작품을 당신과 비슷한 것으로 편집했으며 잘 작동합니다. 추신. 나는 투표하지 않았다 : S –

+0

당신은 천만 다. 나는 당신이하지 않았을 것이라고 확신합니다 - 내가 믿는 몇 가지 코드를 작성할 때 동의하지 않는 사람이 새로운 것을 배우거나 내 제안을 방어하거나 오타를 수정하거나 이유를 제시 할 수있는 기회를주지 못하는 것은 정말로 나를 괴롭힌다. 투표. – mplungjan

0

당신은 MSG 기능에되는 index.php 리디렉션하고, 그래서 아무 것도하지 않습니다.

당신은 순수 PHP에 의해 그것을 할 $ _SESSION의 구성 요소를 설정의 index.php와 구성 요소가 존재하는 경우 index.php를 체크에서 PHP로 리디렉션합니다.

+0

은 당신이 저에게 당신이 두 파일을 변경 할 방법에 대한 빠른 개요를 제공 할 수 있습니다, 그래서 세션이 순간에 나를 넘어 조금있다 당신에게 SkarXa을, 감사? 사전에 감사드립니다 :) –