2015-11-07 61 views
-4
$(function(){ 

$("form").submit(function() { 
    event.preventDefault(); 

    var password = $('#inputPassword').val(); 
    var doublep = $('#checkPassword').val(); 

    $.post("updatepassword.php", 
    { 
     password: password, 
     doublep: doublep 
    }, 

    function(data) 
    { 
     if(data.result == 'changed') { 
      $("#resultDiv").hide().html('<div class="alert alert-success" style="margin-top:25px;"> <strong>Success!</strong> Your password was successfully changed.</div>').fadeIn(1000); 
     } else if (data.result == 'error'){ 
      $("#resultDiv").hide().html('<div class="alert alert-danger" style="margin-top:25px;"> <strong>Error!</strong> Could not update your password. Please contact an admin.</div>').fadeIn(1000); 
     } else if (data.result == 'dontmatch'){ 
      $("#resultDiv").hide().html('<div class="alert alert-danger" style="margin-top:25px;"> <strong>Error!</strong> The passwords you entered didnt match! Please try again.</div>').fadeIn(1000); 
     } 
    }, "json"); 
}); 

});如何使用PHP配置JS

下面是一個Java Script代碼,需要2個輸入值,並進入到PHP,在updatepassword.php我怎麼才能把這些2個值到我的PHP代碼,並給予data.result?

p.s:我沒有得到Java腳本代碼的積分。

回答

0

如果要發佈到該頁面,這樣

$val1 = $_POST['val1']; 
$val2 = $_POST['val2']; 

訪問,如果你使用get方法,

$val1 = $_GET['val1']; 
$val2 = $_GET['val2']; 

,並使用$val1$val2,做你的東西,在最後,只是做echo,你想發回

//let's say $stuff is your final data. 
$stuff = array("Return this stuff back"); 
echo json_encode($stuff); 

閱讀更多關於$_POST$_GETVariablesjson_encode()

+0

所以,當我做陣列和回聲json_encode,該Java腳本會得到回報,並會做的過程嗎? –

+0

當你'echo'時,你會在你的'javascript'代碼中得到東西 – Mubin