2012-08-29 25 views
0

我只是一個PHP初學者,現在我想學習JQUERY,關於我的學習過程我在驗證輸入方面的做法通常是使用純PHP代碼驗證我的輸入,並且每次驗證輸入時都會重新加載頁面,現在我想要改進的地方我發現了一些文章,如http://api.jquery.com/jQuery.ajax/,http://api.jquery.com/jQuery.post/(無法發佈其他鏈接),但我更困惑,因爲他們有不同的方法,我想使用JQUERY教程的方法,但我還沒有找到任何良好的教學課程並且對jQuery的網站沒有教程,正在使用的數據庫,通常我這樣的代碼:如何在沒有頁面重新加載的情況下直接從數據庫驗證輸入?

<form method="post"> 
<label for="Username">Username:</label> 
<input id="Username" type="text" name="username"> 
<?php 
session_start(); 
if(isset($_SESSION['msg'])){ 
$msg=$_SESSION['msg']; 
echo '<label for="Username">'.$msg.'</label>'; 
?> 
<input type="submit" name="reg"> 
</form> 
<?php 
if(isset($_POST['reg'])){ 
$result=//check username from database here 
if($result){ 
$_SESSION['msg']='username not available.'; 
} 
else { 
$_SESSION['msg']='username available.'; 
} 
} 
?> 

現在我想了解我怎麼可以直接輸入驗證從數據庫中無需重新加載頁面?我不知道我應該從哪裏開始,在我的代碼中添加什麼。任何幫助,建議或建議對我來說都將是一個很大的幫助:)

+0

直接從數據庫意味着後端? 我認爲在這種情況下,你可以通過ajax來做到這一點。 – insomiac

+1

編寫您的驗證腳本,就像您期待刷新頁面一樣。不要輸出錯誤消息,而是將它們放入JSON數組中並打印JSON數據。然後從AJAX函數調用腳本。這真的很簡單。 – Matt

+0

是的,先生,我讀了jQuery中的教程,但沒有數據庫。 –

回答

0

首先,在表單中添加的onsubmit功能

<form name='myform' type='POST' action='http://www.action.fr' onSubmit="return check_form()"> 

你可以在一個JAX像

function check_form() 
{ 
    var user = $('#Username').val(); // Username is the id of your input 
    var password = $('#password').val(); // password is the id of your input 
    $.ajax(
    { 
    type:"POST", // or get as you want 
    url:"myfile.php", // it is the php file which can do the job 
    data: "user="+user+"&password="+password, // the param to send to your file, 
    success:function(msg) 
    { 
      ;// msg is the result of your 'myfile.php', everything you write is in the msg var 

    } 
    }); 
} 
在你的PHP文件

,你可以得到你的數據是這樣的:

$user = $_POST['user']; 
$password = $_POST['password']; 
// if your type is get then use $_GET instead of $_POST 

告訴我,如果你有我的代碼任何問題。

+0

我忘了說,在你的函數check_form()中你必須返回false;最後。如果您不確定何時會驗證您的表單,該頁面將重新加載。 – dharth

+0

});在這部分之後,我把這個回報假? –

+0

如果我把回聲放到php文件中,我應該如何放置成功:function(msg){? –

0

編寫您的驗證腳本,就像您期待刷新頁面一樣。不要輸出錯誤消息,而是將它們放入JSON數組中並打印JSON數據。然後從AJAX函數調用腳本。這真的很簡單。

<?php 
// validate.php 

$sampleInput_number = isset($_POST['sampleInput_number']) ? $_POST['sampleInput_number'] : ""; 

$errors = array(); 
if (trim($sampleInput_number) == "" || !is_numeric(trim($sampleInput_number)) { 
    $errors[] = "SampleInput_number must be a number!"; 
} 

// sample input must also match a value from the database 
if (!matchesDBValue($sampleInput_number)) { 
    $errors[] = "SampleInput_number must match a value from the database!"; 
} 

function matchesDBValue($value) { 
    $retval = false; 

    // compare to db values here... 

    return $retval; 
} 

echo json_encode($errors); 

你的形式將是這個樣子:

<form action="" method="post" id="theForm"> 
    <input type="text" name="sampleInput_number" id="sampleInput_number" /> 
    <input type="button" id="formSubmit" value="Submit" /> 
</form> 

而JavaScript是這樣的:

<script language="javascript" type="text/javascript"> 
    $(#formSubmit).on("click", function() { 
     $.post("validate.php", 
      { 
       sampleInput_number: $("#sampleInput_number").val() 
      }, function(data) { 
       // check returned json data 
       // perform action based on results 

       if (no_errors) { 
        $("#theForm").submit(); 
       } 
      }, "json" 
     ); 
    }); 
</script> 
+0

對不起hehe :) - –

相關問題