-1

Here I am going to update chapter details when new user is added, this added user id is obtained in php by using mysql_insert_id(), but I want this user id to be accessed in ajax. As I am new to web I don't know how to fetch php last inserted user id in ajax and send it in a data string. Please can u help me in getting the solutions?

insert_search.php

<?php
    include('header/page_header.php');
    $user="root";
    $server="localhost";
    $password="";
    $db="coedsproddb1";
    $dbconn= mysql_connect($server,$user,$password);
    mysql_select_db($db,$dbconn);
?>
<html>
    <head>
        <title>Insert</title>
        <link rel="stylesheet" href="css/bookstyles.css">
        <link rel="stylesheet" href="css/jquery-ui.css">
        <script src="js/jquery-1.12.4.js"></script>
        <script src="js/jquery-ui.js"></script>
    </head>
    <body>
        <div class="container">
            <style>
                #display {
                    color:red;
                    font-size:12px;
                    text-align:center;
                }

                .logo {
                    padding:5px;
                    float:right;
                }

                header {
                    background-color:#074e7c;
                    height:60px;
                    width:100%;
                    text-align:center;
                    color:white;
                    font-size:40px;                 
                }

                #wrap {
                    text-align:center;
                }

                table, tr, td {
                    border-collapse:collapse;
                }
            </style>
            <?php
                $end_date1 = date('Y-m-d', strtotime("+1 months"));
                $end_date2 = date('Y-m-d', strtotime("+3 months"));
                $end_date3 = date('Y-m-d', strtotime("+6 months"));
                $end_date4 = date('Y-m-d', strtotime("+9 months"));
                $end_date5 = date('Y-m-d', strtotime("+12 months"));
            ?>

            <form name="useradd" id="useradd" action="insert_dummy.php" method="post">
                <input type="hidden" name="end_date" id="end_date" >
                <input type="hidden" name="userid" id="userid" value="<?php $userid;?>" >
                <table align='center' border='1'>
                    <tr>
                        <td><label for="userName">UserName</label></td>
                        <td><input id="userName" name="userName" type="text"/></td>
                    </tr>
                    <tr>
                        <td><label for="userEmail">Email</label></td>
                        <td><input id="userEmail" name="userEmail" type="text" oninvalid="this.setCustomValidity('You must enter a valid email adress')" oninput="setCustomValidity('')"/></td>
                    </tr>
                    <tr>
                        <td><label for="userPassword">password</label></td>
                        <td><input id="userPassword" name="userPassword" type="password" /></td>
                    </tr>
                    <tr>
                        <td><label for="expiry_date">Application_expiry_date</label></td>
                        <td><input id="expiry_date" name="expiry_date" type="text" /></td>
                    </tr>
                    <br/>
                    <br/>
                    </tr>
                </table>
                <br>
                <?php
                    include('db.php');
                    $end_date1 = date('Y-m-d', strtotime("+1 months"));
                    $end_date2 = date('Y-m-d', strtotime("+3 months"));
                    $end_date3 = date('Y-m-d', strtotime("+6 months"));
                    $end_date4 = date('Y-m-d', strtotime("+9 months"));
                    $end_date5 = date('Y-m-d', strtotime("+12 months"));
                    $str="select chapter_no from chapter_details ";

                    $query7=mysql_query($str);

                    $count=mysql_num_rows($query7);
                    for($i=0;$i<$count;$i++)
                    {
                        $chapter_no=mysql_result($query7,$i,'chapter_no');
                        $start_date=date('Y-m-d');
                        $end_date=date('Y-m-d',strtotime("+3 months"));

                        $end_date=date("Y-m-d" ,strtotime("+3 months"));

                        $strings="select * from chapter_subscriptions GROUP BY(chapter_no) ";

                        $query9=mysql_query($strings);
                        $display="<table border='1' align='center'>";
                        $display.="<tr><th>ChapterNumber</th><th>StartDate</th><th>EndDate</th><th colspan='2'>Action</th></tr>";

                        while($result=mysql_fetch_array($query9))
                        {
                            $result['start_date']=date("Y-m-d");
                            $result['end_date']=date("Y-m-d",strtotime("+3 months"));
                            $display.="<tr>";
                            $display.="<td>".$result['chapter_no']."</td>";
                            $display.="<td>".$result['start_date']."</td>";

                            $display.="<td>".$result['end_date']."</td>";
                            //$display.="<tr><select>";

                            //$display.="</td></select></tr>";
                            $display.="<td><a id='colour' class='tooltip' title='Edit' href='chapter_subscription_update.php?user_Id=".$result['userId']."'><img id='image' src='./images/small.gif'/></a></td>";
                            $display.="<td><a id='colour' class='tooltip' data-toggle='tooltip' title='Delete' href='chapter_subscription_delete.php?user_Id=".$result['userId']." '><img id='image' src='./images/trash.png'/></a></td>";
                        }
                        $display.="</tr>";
                        $display.="</table>";
                    }
                    $display.= "<td><select id='drop' data-id='chapter_no'>".$result['end_date'];
                    $display.="<option id='final1' value= $end_date1> $end_date1</option>";
                    $display.=" <option id='final2' value=  $end_date2> $end_date2</option>";
                    $display.="<option id='final3' value=  $end_date3> $end_date3</option>";
                    $display.="<option  id='final4' value=$end_date4> $end_date4</option>";
                    $display.="<option  id='final5' value=  $end_date5>  $end_date5</option>";
                    $display.= " </select></td>";
                    echo $display;
                ?>

                <div id="wrap">
                <input type="submit" name="add" value="add" id="add">
            </form>
            <div id="display">
            </div>
        </div>
        <script type="text/javascript">
            $(document).ready(function(){
                $( "#expiry_date" ).datepicker();
                $("#add").click(function(e){
                    var userName = $("#userName").val();
                    var userEmail = $("#userEmail").val();
                    var userPassword = $("#userPassword").val();

                    var expiry_date = $("#expiry_date").val();
                    var d=$.datepicker.formatDate("yy-mm-dd", new Date(expiry_date));

                    var dataString='userName='+userName+'&userEmail='+userEmail+'&userPassword='+userPassword+'&expiry_date='+d;
                    alert(dataString);
                    if(userName==""||userEmail==""||userPassword=="")
                    {
                        document.getElementById("display").innerHTML="Please Enter The Fields";
                    }
                    else if(!validate1($.trim(userName)))
                    {
                        document.getElementById("display").innerHTML="Please Enter The Valid UserName";
                        document.getElementById("display").focus();
                    }
                    else if(!ValidateEmail($.trim(userEmail)))
                    {
                        document.getElementById("display").innerHTML="Please Enter The Valid Emailid";
                        document.getElementById("display").focus();
                    }
                    else
                    {
                        $.ajax({
                            type: "POST",
                            url: "insert_dummy.php",
                            data: dataString,
                            cache: false,
                            success: function(result){
                                //alert("submitted"+result);
                                $('#display').html(result);
                                //window.location.href="chapter_subscription_search.php";
                            },
                            error: function (xhr, ajaxOptions, thrownError) {
                                alert(xhr.status);
                                alert(thrownError);
                            }
                        });

                    }
                    e.preventDefault(); 
                });

                function validate1(userName)
                {
                    var u=userName;
                    var filter=/^[a-zA-Z0-9]+$/;
                    if(filter.test(u))
                    {
                        return true;
                    }
                    else
                    {
                        return false;
                    }
                }

                function ValidateEmail(userEmail) 
                {  
                    var e=userEmail;
                    var filter=/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/;
                    if(filter.test(e)) {
                        return true;
                    }
                    else {
                        return false;
                    }
                }
            });
        </script>
        <script type="text/javascript">
            $(document).ready(function() {
                $( "#expiry_date" ).datepicker();
                $("#menuwrapper").hide();
                $("#unicode").click(function() {
                    $("#menuwrapper").slideToggle(500);
                });
            });
        </script>
        <script type="text/javascript">
            $(document).ready(function() {
                $("#drop").on("change", function(){
                    alert("hi");
                    // var end_date=$("#final1").val();
                    // alert(end_date);
                    // var end_date=$("#final2").val();
                    //alert(end_date);
                    //var end_date=$("#final3").val();
                    //alert(end_date);
                    // var end_date=$("#final4").val();
                    //alert(end_date);
                    var end_date=$("#final5").val();
                    var userid=
                    // alert(end_date);
                    dataString='end_date='+end_date;
                    alert(dataString);
                    $.ajax({
                        type: "POST",
                        url: "update_dropdown.php",
                        data: dataString,
                        cache: false,
                        success: function(result){
                            //alert(result);
                            $('#display').html(result);
                            //window.location.href="books.php";
                            //location.reload();
                        },
                        error: function (xhr, ajaxOptions, thrownError) {
                            alert(xhr.status);
                            alert(thrownError);
                        }
                    });
                });
            });
        </script>
    </body>
</html>

insert_dummy.php

<html>
    <head><title>Insertion</title>
        <link rel="stylesheet" href="css/bookstyles.css">
        <link rel="stylesheet" href="css/jquery-ui.css">
        <script src="js/jquery-1.12.4.js"></script>
        <script src="js/jquery-ui.js"></script>
    </head>
    <body>
        <style>
            #colour {
                text-decoration:none;
            }

            .option {
                width:20px;
            }

            table tr {
                width:200px;
            }
        </style>
        <div id="display">
            <input type="hidden" name="chapter_no" id="chapter_no" >
            <input type="hidden" name="userid" id="userid" >
            <input type="hidden" name="level" id="level" >

            <?php

            include('db.php');

            $userName=mysql_real_escape_string($_POST['userName']);

            $userEmail=mysql_real_escape_string($_POST['userEmail']);
            $userPassword=mysql_real_escape_string($_POST['userPassword']);
            $expiry_date1=mysql_real_escape_string($_POST['expiry_date']);
            $expiry_date=date("Y-m-d" ,strtotime($expiry_date1));
            /*$end_date1=mysql_real_escape_string($_POST['end_date']);
            $end_date=date("Y-m-d",strtotime($end_date1));*/

            $regDate = date("Y-m-d");

            function generateCode($characters) 
            {
                $possible = '23456789abcdefghjkmnpqrstuvwxyz!@#$%^&*';
                $code = '';
                $i = 0;
                while ($i < $characters) { 
                    $code .= substr($possible, mt_rand(0, strlen($possible)-1), 1);
                    $i++;
                }
                return $code;
            }

            $registration_key=generateCode(10);

            $str="insert into coeds_user(userName,userEmail,userPassword,regDate,expiry_date,registration_key) values('$userName','$userEmail','$userPassword','$regDate','$expiry_date','$registration_key')";

            $query=mysql_query($str);
            $userid=mysql_insert_id();
            if($query)
            {
            $display="Success";
            }

            else
            {
            $display= "Failed";
            }

            /*$string="select * from chapter_subscriptions where userId=$userid";
            $query2=mysql_query($string);
            $display.="<table border='1' align='center'>";
            $display.="<tr><th>ChapterNumber</th><th>StartDate</th><th>EndDate</th><th colspan='3'>Action</th></tr>";
            while($res=mysql_fetch_array($query2))
            {
                $display.="<tr>";

                $display.="<td>".$res['chapter_no']."</td>";
                $display.="<td>".$res['start_date']."</td>";
                $display.="<td>".$res['userPassword']."</td>";
                $display.="<td>".$res['regDate']."</td>";
                $display.="<td>".$res['expiry_date']."</td>";
                $display.="<td>".$res['registration_key']."</td>";
                $display.="<td><a id='colour' class='tooltip' title='Edit' href='user_update.php?user_Id=".$res['userId']."'><img id='image' src='./images/small.gif'/></a></td>";
                $display.="<td><a id='colour' class='tooltip' data-toggle='tooltip' title='Delete' href='user_delete.php?user_Id=".$res['userId']." '><img id='image' src='./images/trash.png'/></a></td>";

                $display.="</tr>";

                $display.="</table>";
            }

             $page="";
             echo $display;

             ?>*/

            include('db.php');
            $str="select chapter_no from chapter_details ";

            $query7=mysql_query($str);

            $count=mysql_num_rows($query7);
            for($i=0;$i<$count;$i++)
            {
                $chap_lic=generateCode(50);
                $chapter_no=mysql_result($query7,$i,'chapter_no');
                $start_date=date('Y-m-d');
                $end_date=date('Y-m-d',strtotime("+3 months"));

                $s="insert into chapter_subscriptions (userId,chapter_no,start_date,end_date) values($userid,$chapter_no,'$start_date','$end_date')";
                $end_date=date("Y-m-d" ,strtotime("+3 months"));

                $query8=mysql_query($s);

                $strings="select * from chapter_subscriptions where userId=$userid";

                $query9=mysql_query($strings);
                $display="<table border='1' align='center'>";
                $display.="<tr><th>ChapterNumber</th><th>StartDate</th><th>EndDate</th><th colspan='2'>Action</th></tr>";
                while($result=mysql_fetch_array($query9))
                {
                    $display.="<tr>";
                    $display.="<td>".$result['chapter_no']."</td>";
                    $display.="<td>".$result['start_date']."</td>";

                    $display.="<td>".$result['end_date']."</td>";
                    //$display.="<tr><select>";
                    //$display.="</td></select></tr>";
                    $display.="<td><a id='colour' class='tooltip' title='Edit' href='chapter_subscription_update.php?user_Id=".$result['userId']."'><img id='image' src='./images/small.gif'/></a></td>";
                    $display.="<td><a id='colour' class='tooltip' data-toggle='tooltip' title='Delete' href='chapter_subscription_delete.php?user_Id=".$result['userId']." '><img id='image' src='./images/trash.png'/></a></td>";
                }
                $display.="</tr>";
                $display.="</table>";
            }
            echo $display;
            ?>
        </div>
        <script type="text/javascript">
            $(document).ready(function() {
                $( "#expiry_date" ).datepicker();
                $("#menuwrapper").hide();
                $("#unicode").click(function() {
                    $("#menuwrapper").slideToggle(500);
                });
            });
        </script>
    </body>
</html>
Manfred Radlwimmer
  • 13,257
  • 13
  • 53
  • 62

2 Answers2

0

If you want to get answer then try to post as little code as possible, becauce noone wants to read bunch of badly formatted lines. It's not really about ajax, it's more about PHP that gets the data assuming you are using MySQL you can run query in your ajax calledPHP file:

SELECT user_id FROM table_name
ORDER BY user_id DESC
LIMIT 1;
NoOorZ24
  • 2,914
  • 1
  • 14
  • 33
0
SELECT LAST_INSERT_ID() as last_id
Junius L
  • 15,881
  • 6
  • 52
  • 96