我有兩個單獨的javascipt文件和一個php文件,在第一個create.js我發送變量到get.php文件使用後,然後我想發送這些變量從get.php到register.js,我怎樣才能在register.js中獲得這些變量並將它們用於下一步?發送多個帖子變量到另一個JavaScript文件
這是從第一JS文件的代碼:
var fs = $('#fname').val();
var ls = $('#lname').val();
var name = $('#adduser').val();
var pass = $('#addpass').val();
var cpass = $('#conpass').val();
if (document.forms['form2'].fname.value == "" || document.forms['form2'].lname.value == "" || document.forms['form2'].adduser.value == "" || document.forms['form2'].addpass.value == ""){
alert("Input the required Filled!");
}
else{
var vals;
var request = $.ajax({
url:"ifexist.php", type:"POST",
data:{
n:name
}
});
request.done(function(data){
vals = data; //alert(data);
if (vals == "1"){
alert("Username Already Exist!");
}
else
if(pass==cpass){
var request1 = $.ajax({
type: "POST",
url:"get.php",
data:{ fi:fs, la:ls, na:name, pa:pass}
});
request1.done(function(data){
alert("Not yet!");
location.href = 'captcha.html';
// setTimeout(function() {location.href = 'captcha.html';},1500);
});
}
else
alert("Password did not match!");
});
}
,這是第二個js文件:
$.getJSON('get.php', function(data) {
// Inside your success callback:
var fir = $("#fi").html(data.uf);
var las = $("#la").html(data.ul);
var nam = ("#na").html(data.un);
var pas = $("#pa").html(data.up);
alert("Success!");
var request = $.ajax({
type: "POST",
url:"adduser.php",
data:{ f:fir, l:las, n:nam, p:pas}
});
request.done(function(data){
alert("Success!");
//setTimeout(function() {alert("Success!");},1500);
});
});
,這是PHP文件:
<?php
$uf = $_POST['fi'];
$ul = $_POST['la'];
$un = $_POST['na'];
$up = $_POST['pa'];
//add to associative array
$result['fi'] = $uf;
$result['la'] = $ul;
$result['na'] = $un;
$result['pa'] = $up;
// encode as json and echo
echo json_encode($result);
?>
你爲什麼要這麼做?你還是指定'success'回調你'$ .ajax'函數中:'$阿賈克斯({成功:功能(數據){...}})' –