Auto filling the Text boxes

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Dinzy
Forum Newbie
Posts: 1
Joined: Wed Apr 06, 2011 12:39 am

Auto filling the Text boxes

Post by Dinzy »

Hello all,

I have three text fields ( hosteladmissionnumber,branch & Semester) and three text boxes in a form. When i provide the hostel admission number , the corresponding branch semester must be acquired from the registrationtable in DB and fill it in text boxes. How can i do it? any help would be appreciated. Thanks. I have pasted the code below.

Code: Select all

<?php
session_start();
  $hostad=$_POST['hosteladmissionno'];
   $sem=$_POST['student_name'];
   $sem=$_POST['semester'];
$con=mysql_connect('localhost','root','');

if(!$con)
{
die('Unable to connect'.mysql_error());
}
mysql_select_db('hostel',$con);
//i have to insert the hosteladmission in payment too..but no need to insert semester and branch
$r1="INSERT INTO payment(hosteladmissionno) values ('$hostad')";
mysql_close($con);
?>
/HTML PART

<form action='payment.php' method='POST' name='form1'>
    <center>
 <table>
<tr><td><b>Admission No:</b></td><td><input type='text'  name='hosteladmissionno'></td></tr>
<tr><td><b>Student Name:</b></td><td><input type='text'  name='student_name'></td></tr>
<tr><td><b>Semester:</b></td><td><input type='text'  name='semester'></td></tr>

Apocalypz
Forum Newbie
Posts: 6
Joined: Fri Nov 02, 2007 2:54 pm

Re: Auto filling the Text boxes

Post by Apocalypz »

In order to fill in a text box based on what a user enters in a different text field you will need to use a little Javascript. I would recommend familiarizing yourself with jQuery and the AJAX functionality to make this work the best.

For example...

Code: Select all

<input type='text' name='hosteladmissionno' onblur='getSemester(this.value);' />
<input type='text' name='semester' id='semester' />

<script>
function getSemester(han) {
    $.ajax({
        type:'POST',
        url:'http://yoursite.com/getSemester.php',
        data:'hosteladno='+han,
        success: function(sem) {
            $('#semester').val(sem);
        }
}
</script>
This assumes that your getSemester.php file uses the "hosteladno" as a POSTed variable to find the semester value in the database, then simply echo's the semester value.
Post Reply