LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 07-18-2013, 06:01 AM   #1
kedarp
Member
 
Registered: Jul 2012
Distribution: Ubuntu
Posts: 198
Blog Entries: 3

Rep: Reputation: 23
Want to prefetch values from the database


Hello,

I am creating a web page where I need to get values from the database and fill the textboxes before submitting the form.
When I enter the employee ID, I should get the name of the employee when I click on a button "Get details".
What would help me to do this?
 
Old 07-18-2013, 06:37 AM   #2
Snark1994
Senior Member
 
Registered: Sep 2010
Distribution: Debian
Posts: 1,632
Blog Entries: 3

Rep: Reputation: 346Reputation: 346Reputation: 346Reputation: 346
Well, you could have PHP on the page that checks the POST data for an "employeeID" value: if it's not present (when you first load up the page) or invalid, then it just displays a blank page, but if it matches a record in your employee database (when someone's filled in the field and clicked 'Get details'), then it loads up their details and puts them into the fields as defaults. Just make sure your code doesn't fall foul of SQL injection if you use this approach.

If you need more specific help than this, please show us the code you've written so far.

Hope this helps,
 
Old 07-18-2013, 08:25 AM   #3
kedarp
Member
 
Registered: Jul 2012
Distribution: Ubuntu
Posts: 198

Original Poster
Blog Entries: 3

Rep: Reputation: 23
Thanks for the reply. Here is a sample code for page.
I want to get the employee name on the same page as this.

Code:
<form action="action.php" method="post">

    Employee ID: <input type="text" name="id">
    <input type="button" value="Get details">

    Employee Name: <input type="text" name="name">
    <br>
    <input type="submit" value="Submit">

</form>
The problem is if I click "Submit", it will navigate me to another page.
Do I need to look into AJAX or something?
 
Old 07-18-2013, 09:38 AM   #4
dugan
LQ Guru
 
Registered: Nov 2003
Location: Canada
Distribution: distro hopper
Posts: 11,235

Rep: Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320
Quote:
Originally Posted by kedarp View Post
Do I need to look into AJAX or something?
Yes.

With jQuery, you'd use something like:

Code:
$('input[name="id"]).click(function {} {
    $.get('http://localhost/employees/' + this.val(), function (data) {
        $('input[name="name"].val(data['name']));
    })
});
And on the server side, you'd have an endpoint at http://localhost/employees/<id> (say, http://localhost/employees/2), which returns JSON in the following format:

Code:
{'id': 2, 'name': 'bob'}
 
1 members found this post helpful.
Old 08-03-2013, 12:45 AM   #5
kedarp
Member
 
Registered: Jul 2012
Distribution: Ubuntu
Posts: 198

Original Poster
Blog Entries: 3

Rep: Reputation: 23
Uptil now, I have got success in getting values from the database on the same page by calling a php script.

Code:
<script type="text/javascript">
$(document).ready(function(){
    $("span").hide();
    $("#getname").click(function(){
        $.get("data.php?id=" + id.value,function(data){
	    if(data.length>1)
	    {
	    $("span").hide();
	    $("#name").val(data);
            }
	    else
            {
	    $("span").show();
	    }		    
        });
    });
});
</script>
Here is the html code.
Code:
    Employee ID: <input type="text" name="id" id="id">
    <button id="getname">Get name</button>

    Employee Name: <input type="text" name="name" id="name">
    <span>Name does not exist for this id</span>
    <br>
Now, the problem is that when I add the <form> tag to the html code, this code fails. The form gets submitted and
takes me to another page(action of form tag). I am working on it.

Regards.

Last edited by kedarp; 08-03-2013 at 12:47 AM.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Shared Memory, Semaphore Values for oracle database thiyagusham Linux - Newbie 8 11-25-2012 03:03 AM
how kernal values are calculated to ORACLE DATABASE thiyagusham Linux - Server 1 11-20-2012 10:04 AM
want to display database values in select box using php sunlinux Programming 3 08-08-2012 03:26 AM
Reading a CSV text file and storing the values in Oracle Database table shafi2all Linux - Newbie 3 04-17-2008 12:19 PM
How To Put Values In Select Box From Database In Jsp nimishabhatia Programming 1 07-20-2005 01:16 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 04:59 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration