2013-06-28 5 views
2

새 사용자를 만들 페이지가 있습니다. 텍스트 필드에 입력 된 데이터에 대한 다양한 검사를 수행하는 PHP 스크립트가 있습니다. 부적합한 항목을 발견하면 프로세스가 종료 된 이유를 설명하기 위해 사용자에게 피드백을 제공해야합니다. 내 생각에는 실패한 조건에 해당하는 메시지로 채울 빈 div가 생겼다.PHP에서 if 문을 기반으로 div에 텍스트 추가/변경

그러나 생각보다 어려울 수도 있습니다. 내 PHP 스크립트 에서이 싶습니다. 나는 꽤 웹 개발에 익숙하지 않다. 이 작업을 수행하는 가장 쉽고/쉬운 방법에 대한 제안은 열려 있습니다. 감사.

<div id="page"> 
    <img id="mainpic" src="images/banner.png" height="390" width="982"> 

    <div id="stylized" class="leftsidebox"></div> 

    <div id="stylized" class="myform"> 
     <form id="form" name="form" method="post" action="scripts/create_admin.php"> 

      <label>Security Code 
       <span class="small">Enter the security code provided to you</span> 
      </label> 
      <input type="text" name="securitycode" id="name" /> 

      <button type="submit">Create Administrator</button> 
     </form> 
    </div> 

    <div id="stylized" class="rightsidebox"> 
     <div id="stylized" class="feedback"> 
      <h1>this is where I want to add dynamic text from php</h1> 
     </div> 

    </div> 
</div> 


<?php 

    include('connection.php'); 

    //retrieve input values from the form 
    $security_code = $_REQUEST['securitycode']; 

    if (strlen($security_code) == 0) { 
     //Add the text "Please enter a security code." to the div class="feedback" 
     die(); 
    } 
?> 

답변

0

이 같은 div 태그 안에 해당 스크립트를 넣을 수 있습니다 :

<div id="stylized" class="rightsidebox"> 
    <div id="stylized" class="feedback"> 
     <h1> 

<?php 

include('connection.php'); 

//retrieve input values from the form 
$security_code = $_REQUEST['securitycode']; 

if (strlen($security_code) == 0) { 
    echo "Please enter a security code."; 
} 
?> 
     </h1> 
    </div> 

</div> 
2

당신은 페이지를 다시로드해야합니다. 보안 코드에서 오류가 감지되면 오류 메시지와 함께 변수를 만들고 페이지를 다시 표시하십시오. 메시지가 여기에 있으면 페이지에 각 디스플레이가 표시됩니다. 그럴 경우 표시합니다. 그런 다음 페이지

if (strlen($security_code) == 0) { 
     $message = "Security code was wrong"; 
     require(page.php) //Call your view 
} 

:

<div id="stylized" class="leftsidebox"> 
    <?php if(isset($message)){ echo $message; } ?> 
</div> 

페이지를 다시로드 피하고 싶은 경우에, 나는 AJAX를 조사 권합니다. 대신 폼을 제출할 때 ajax 요청을하고 (오류가있는 경우 오류가 있음) javascript로 DOM을 조작하여 오류 메시지가 비동기 적으로 표시됩니다. 여기

3

당신은 HTML 전에 PHP 코드를 추가 이동

<?php 

    include('connection.php'); 

    //retrieve input values from the form 
    $security_code = $_REQUEST['securitycode']; 

    if (strlen($security_code) == 0) { 
    $error="Please enter a security code.";  
    //Add the text "Please enter a security code." to the div class="feedback" 
     die(); 
    } 
?> 

<div id="stylized" class="feedback"> 
      <h1><?php if(isset($error)){ echo $error; } ?></h1> 
     </div> 
관련 문제