0

I am using the Jquery/Ajax API to update a relevant database with the information the users enter when signing up for their account. Once the data has been successfully sent I need to open another page where the user can upload their user photo. The form handling PHP page that my data is sent to uses the PDO function to grab the last inserted id into my table $users_ID_count = $dbh->lastInsertId();. It then echos back $users_ID_count to my success function within AJAX where I was under the intention i could open my pic upload page with the ID as a parameter. I cant seem to be able to use the ID passed in the URL and am widely open to a better solution.

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);
                    }
                });

below is the blog-signup-srvr-4.php sorry that it drags quite long

    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;
2
  • 1
    have you console.log your text value? it might be an object then it might be something like text.XXX Commented Dec 18, 2014 at 17:56
  • Can you add the blog-signup-srvr-4.php code so we can see what's being returned? Commented Dec 18, 2014 at 18:29

1 Answer 1

2

Perhaps in your ajax file you could create a session with the user ID like:

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

And the "blog-signup-propic.php" file could start by retrieving the userID from that session:

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

This way you can also avoid parameters on the url. Also, once your script is done, you can unset that session:

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

And finally, modify your javascript to this:

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

Of course the variables shown here might be different than the ones that apply for your project. But I think you can fix that :)

Hope it helps...

Sign up to request clarification or add additional context in comments.

1 Comment

I guess I could be considered a nube in practice, I am just going the unorthodox way of teaching myself where a university professor more then likely would have covered $_SESSIONs from the get go but I read a tutorial on it and $_SESSION is exactly what is going to solve my problem! much appreciated!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.