简体   繁体   中英

PHP POST data to another page using AJAX

I currently have a form in PHP that gets validated using JQuery and the data is submitted to the DB. During the INSERT I'm getting the ID of the user that was inserted.
On success, it redirects the user to the next page which I then want to display the $user_id . I know I should be using get or some other method but unsure how to go about it with my current set up. Any help would be appreciated.

JS

                var submitRequest = $.ajax({
                     type: "POST",
                     url: "submit.php",
                     data: user_data
                });

                submitRequest.done(function(msg)
                {
                    //success
                   // / console.log('success');
                   window.location.href = "nextpage.php";
                });

PHP

$query_user ="INSERT INTO mytable(name,email) VALUES ('".$name."', '".$email."' )";
$sql_user =mysql_query($query_user) or die (mysql_error());
$user_id = mysql_insert_id();

nextpage.php

//How can I get it to display $user_id?

Try below :-

PHP

$query_user ="INSERT INTO mytable(name,email) VALUES ('".$name."', '".$email."' )";
$sql_user =mysql_query($query_user) or die (mysql_error());
$user_id = mysql_insert_id();
echo $user_id;

JS

submitRequest.done(function(msg)
                {
                    //success
                   // / console.log('success');
                   window.location.href = "nextpage.php?id="+msg; // msg will consist userid
                });

nextpage.php

$userid = $_GET["id"];

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM