Click here to Skip to main content
15,892,059 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
employess.php
PHP
<?php
// First of all, don't make use of mysql_* functions, those are old
$pdo = new PDO("mysql:host=localhost;dbname=erp1314;charset=utf8", "root", "");
?>
<html>
<head>
<link type="text/css" rel="stylesheet" href="style.css"/>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script> <!-- You will need jQuery (or anyother javascript framework) to accomplish your goal cause you need ajax -->
        <title>Training</title>
    </head>
    <body>
        <div id="content">
           <h1 align="center">Add Training</h1>
<form action="employess.php" method="post">
                <div>
                    <p>
                        Training ID:
                        <input type="text" name="Training_ID">
                    </p>
                    <p>
                        Employee ID:
                        <select id="JOB_NO" >
                            <option value="">Select one</option>
                            <?php
                            $st = $pdo->prepare("SELECT JOB_NO FROM mm_client_loc_job");
                            $st->execute();
                            $rows = $st->fetchAll(PDO::FETCH_ASSOC);
                            foreach ($rows as $row) {
                                ?><option value="<?php echo $row ['JOB_NO']; ?>"><?php echo $row ['JOB_NO']; ?></option><?php
                            }
                        ?>
                        </select>
                    <p>
                        First name:
                        <input type="text" name="CLIENT_NAME" id="CLIENT_NAME">
                    </p>
                    <p>
                        Last name:
                        <input type="text" name="LOC_NAME" id="LOC_NAME">
                    </p>
                    <p>
                        Training required?
                        <select name="Training">
                            <option value="">Select...</option>
                            <option value="Customer Service">Customer Service</option>
                            <option value="Bailer">Bailer</option>
                            <option value="Reception">Reception</option>
                            <option value="Fish & meat counters">Fish & meat counters</option>
                            <option value="Cheese counters">Cheese counters</option>
                        </select>
                    </p>
                    <input type="submit">
            </form>
        </div>
    <script type="text/javascript">
        $(function() { // This code will be executed when DOM is ready
            $('#JOB_NO').change(function() { // When the value for the Employee_ID element change, this will be triggered
                var $self = $(this);
             // We create an jQuery object with the select inside

                $.post("getEmployeeData.php", { JOB_NO : $self.val()}, function(json)
                   {
                     alert($self.val());
                    if (json && json.status) {
                        $('#CLIENT_NAME').val(json.name);
                        $('#LOC_NAME').val(json.lastname);


                    }
                })
            });
        })
    </script>

    </body>
</html>

getEmployeeData.php
PHP
<?php
$pdo = new PDO("mysql:host=localhost;dbname=erp1314;charset=utf8", "root", "");

//
header("Content-Type:application/json; Charset=utf-8");

// As you can see, here you will have where Employee_ID = :employee_id, this will be
// automatically replaced by the PDO object with the data sent in execute(array('employee_id' => 
$_POST['Employee_ID']))
// This is a good practice to avoid Sql Injection attacks

$st = $pdo->prepare("SELECT CLIENT_NAME, LOC_NAME FROM mm_client_loc_job WHERE JOB_NO = :job_No LIMIT 1");
$st->execute(array (':job_No' => $_POST['Job_No']));
echo (job_no);
$data = $st->fetch(PDO::FETCH_ASSOC);
echo json_encode(array ('status' => true, 'name' => $data ['CLIENT_NAME'], 'lastname' => $data ['LOC_NAME']));
?>
Posted
Updated 27-Dec-13 10:11am
v4
Comments
Er. Tushar Srivastava 27-Dec-13 16:13pm    
Have you commented out the line echo (job_no) It is an error as per I can see.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900