将参数传递到另一个网页

时间:2014-12-18 17:49:40

标签: php jquery mysql ajax pdo

我正在使用Jquery / Ajax API来更新相关数据库,其中包含用户在注册其帐户时输入的信息。数据成功发送后,我需要打开另一个用户可以上传用户照片的页面。处理我的数据发送到的PHP页面的表单使用PDO函数将最后插入的id抓取到我的表$users_ID_count = $dbh->lastInsertId();中。然后它回复$ users_ID_count到我在AJAX中的成功函数,我打算在那里打开我的图片上传页面,其中ID作为参数。我似乎无法使用URL中传递的ID,并且可以更好地解决这个问题。

var dataString = 'name1='+ first + '&last1='+ last + '&email1='+ email + '&password1='+ password + '&user1=' + usrname + '&dev=' + dev + '&des=' + des + '&bth=' + bth;

            // AJAX Code To Submit Form. jquery version 
                $.ajax({
                    type: "POST",
                    url: "blog-signup-srvr-4.php",
                    data: dataString,
                    cache: false,
                    success: function(text){
                        window.location.assign('http://localhost/knoxPrograms/knoxville_programmers/blog-signup-propic.php?disatl ='+ text);
                    }
                });

下面是blog-signup-srvr-4.php抱歉它拖得很长

    if(isset($_REQUEST['name1']) && isset($_REQUEST['last1']) && isset($_REQUEST['email1']) && isset($_REQUEST['password1']) && isset($_REQUEST['user1']) && isset($_REQUEST['dev']) && isset($_REQUEST['des']) && isset($_REQUEST['bth']))
    {
    //career type field values
    $dev = $_REQUEST['dev'];
    $des =  $_REQUEST['des'];
    $bth = $_REQUEST['bth'];


    //su form field values
    $firstName = $_REQUEST['name1'];
    $lastName =  $_REQUEST['last1'];
    $emailAddr = $_REQUEST['email1'];
    $usrPass =   $_REQUEST['password1'];
    $userId =    $_REQUEST['user1'];        


    setUpUser($dev, $des, $bth, $dbh, $firstName, $lastName, $emailAddr, $usrPass, $userId, $dbh);

}

    //initial set up function. Prepare sql statements, bind parameters to users input and send      
    function setUpUser($dev, $des, $bth, $dbh, $first, $last, $email, $pass, $id, $dbh)
    {

    $stmt = $dbh->prepare('INSERT INTO users(userName, userPass) VALUES(? , ?)');
    $stmt2= $dbh->prepare('INSERT INTO userinfo(firstName, lastName, email, ID, careerType) VALUES(?,?,?,?,?)');    

    $dbh->beginTransaction();    //the start of paramater binding and execution

    $stmt->bindParam(1, $id);
    $stmt->bindParam(2,$pass);
    $stmt2->bindParam(1,$first);
    $stmt2->bindParam(2,$last);
    $stmt2->bindParam(3,$email);


    //one of the following sends the string 'true' over, the value
    //of the element that sends true over is what is updated in our database

    if($dev == "true"){
        $dev = "dev";
        $stmt2->bindParam(5, $dev);
    }
    elseif($des == "true"){
        $des = "des";
        $stmt2->bindParam(5,$des);
    }
    elseif($bth == "true"){
        $bth = "bth";
        $stmt2->bindParam(5,$bth);
    }

    $stmt->execute();
    $users_ID_count = $dbh->lastInsertId();

    $stmt2->bindParam(4,$users_ID_count); //bind parameter after the first statement has been created and its last id insert is stored

    $stmt2->execute(); //execute after last id inserted is binded to parameter so we may enter it into our database

    $dbh->commit();             
    echo $users_ID_count;

1 个答案:

答案 0 :(得分:2)

也许在您的ajax文件中,您可以使用以下用户ID创建会话:

<?php session_start(); $_SESSION["userID"] = $users_ID_count; ?>

&#34; blog-signup-propic.php&#34;文件可以从该会话中检索userID开始:

<?php
if(isset($_SESSION["userID"])) { $disatl = $_SESSION["userID"]); } else { $disatl = ""; }
?>

这样你也可以避免网址上的参数。此外,一旦您的脚本完成,您可以取消设置该会话:

<?php
unset($_SESSION["userID"]);
?>

最后,将您的javascript修改为:

...
success: function(text){
    window.location.assign('http://localhost/knoxPrograms/knoxville_programmers/blog-signup-propic.php');
}
...

当然,此处显示的变量可能与适用于您的项目的变量不同。但我认为你可以解决这个问题:)

希望它有所帮助...