我有两个单独的javascipt文件和一个php文件,在第一个create.js上我使用post将变量发送到get.php文件,然后我想将这些变量从get.php发送到register.js ,我怎样才能在register.js中获取这些变量并将它们用于下一步?
这是来自第一个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);
?>
答案 0 :(得分:0)
你不能这样做(向浏览器发送请求。从技术上来说,你可以作为推送服务,但这不是你需要的东西恕我直言,PHP在处理它时很棘手。)
在ajax POST调用完成后,为什么不直接在JavaScript中进行通信?我的意思是,你想发送什么寄给register.js? (不要忘记,即使它们是单独的文件,它们也可以相互通信,因为它们都包括在内)
var post_data = { f:fir, l:las, n:nam, p:pas};
var request = $.ajax({
type: "POST",
url:"get.php",
data:post_data
});
request.done(function(data){
alert(data); //will give you objects the -response form the server
// now do whatver you need to do, cal an fucntion to create.js, create trigger, etc.
});