2012-05-22 3 views
0

나는 유효성 검사를하고 싶은 양식이 있습니다. 그러나 쿼리를 작성하는 것을 검증하고자하는 필드가 있습니다. 포스트 백 이후에 양식에 채워진 모든 값이 손실되기 때문에 양식을 다시 게시하지 않아야합니다. 포스트 백없이 쿼리를 작성할 수있는 방법이 있습니까? 아니면 값을 유지하는 방법을 다시 게시해야합니까? 도와주세요PHP : 양식 다시 게시없이 쿼리를 실행하는 방법?

+0

"다시 게시"란 무엇을 의미합니까? – Andrew

답변

2

AJAX (jQuery)를 사용하는 경우 필요한 경우 브라우저를 새로 고치지 않고도 XML 요청을 게시 할 수 있습니다. 이 들어, 그냥 텍스트 필드와 양식을 작성하고, 제출 버튼을 모두에게 ID를 부여하고 버튼에 대한 클릭 리스너를 추가

$('#submit-button').click(function() { 
    var name = $('#username').val(); 
    $.ajax({ 
     type: 'POST', 
     url: 'php_file_to_execute.php', 
     data: {username: name}, 
     success: function(data) { 
      if(data == "1") { 
       document.write("Success"); 
      } else { 
       document.write("Something went wrong"); 
      } 
     } 
    }); 
}); 

만약 "제출 버튼과 버튼을 사용자가 클릭 "-ID,이 함수가 호출됩니다. 그런 다음 POST를 사용하여 텍스트 필드의 값을 php_file_to_execute.php로 보냅니다. 이 .php 파일 안에 사용자 이름을 확인하고 그 결과를 출력 할 수 있습니다 :

if($_POST['username'] != "Neha Raje") { 
    echo "0"; 
} else { 
    echo "1"; 
} 

나는 당신을 도울 수 있기를 희망합니다! :)

0

당신이 썼던 것을 다시 말해야 할 수도 있습니다. 조금 헷갈 렸습니다. 참고하시기 바랍니다. 내가 데이터를 처리하고 때

<form method="post"> 
Text 1: <input type="text" name="form[text1]" value="<?=$form["text1"]?>" size="5" /><br /> 
Text 2: <input type="text" name="form[text2]" value="<?=$form["text2"]?>" size="5" /><br /> 
<input type="submit" name="submit" value="Post Data" /> 
</form> 

는 그리고,이 같다; 이 방법으로

<?php 
if ($_POST["submit"]) { 
$i = $_POST["form"]; 
if ($i["text1"] or .....) { $error = "Something is wrong."; } 
if ($i["text2"] and .....) { $error = "Maybe right."; } 

if (!$error) { 
    /* 
    * We should do something here, but if you don't want to return to the same 
    * form, you should definitely post a header() or something like that here. 
    */ 
    header ("Location: /"); exit; 
} 
// 
} 

if (!$_POST["form"] and !$_GET["id"]) { 
} else { 
$form = $_POST["form"]; 
} 
?> 

, 값은 당신이 길을 잃기하도록 설정하지 않는 을 손실되지 않습니다. 비동기이 등 양식에서 사용자 이름을 얻을 validate.php에서

$('#my_submit_button').click(function(event){ 
    event.preventDefault(); 
    var username = $('#username').val(); 
    $.post('validate.php', {username: username, my_submit_button: 1}, function(response){ 
    console.log(response); //response contain either "true" or "false" bool value 
    }); 
}); 

:로

0

사용 jQuery의 $.post() 방법

if(isset($_POST['my_submit_button']) && $_POST['my_submit_button'] == 1 && isset($_POST['username']) && $_POST['username'] != "") { 

    // now here you can check your validations with $_POST['username'] 
    // after checking validations, return or echo appropriate boolean value like: 
    // if(some-condition) echo true; 
    // else echo false; 

} 

: 보안 관련 취약점을 알고 고려하시기 바랍니다 데이터베이스 변경 스크립트를 실행하기 전에 AJAX를 사용하기 전에 다른 문제를 해결해야합니다.

관련 문제