47 users online (1 members and 46 guests)  


  Results 1 to 12 of 12

Related

  1. Fill a Text Field from a Drop-Down Box    Forum: Javascript Forum
    Replies: 0
  2. Populate fields by javascript using MYSQL    Forum: Javascript Forum
    Replies: 0
  3. Replies: 0
  4. Replies: 6
  5. Replies: 4
  1. #1
    xChAMaRx's Avatar
    New User

    Status
    Offline
    Join Date
    Feb 2009
    Posts
    4

    [Javascript] Populate a Text field via MYSQL Drop Down

    Hi All,

    I'm quite new to PHP/MySQL and I'm trying to create a Job Card for our company. I have created a page with a MySQL/PHP dropdown which works great, but I need to populate the text box with the dropdown selection.

    I have found a few examples using Javascript, but none of them seems to work. Am I doing something wrong or missing something. Below is my code:


    Code:
    <html>
    <head>
    <title>Job Card</title>
    <script language=Javascript>
    function Choice()
    {
    form1.users2.value = form1.users.value;
    }
    </script>
    
    <?PHP
    // Make Connection
    include 'dbconfig.php';
    
    // Retreive data from database
    mysql_connect($dbhost2, $dbuser2, $dbpass2) or die(mysql_error());
    mysql_select_db($dbname2) or die(mysql_error());
    
    $user_query = "SELECT concat(first_name, ' ', last_name) as fullname FROM users ORDER BY first_name";
    $user_result = mysql_query($user_query);
    $client_query = "SELECT name FROM accounts ORDER BY name";
    $client_result = mysql_query($client_query);
    
    ?>
    
    </head>
    <body>
    <Form name ="form1" Method ="POST" Action ="submitform.php">
    <p>Please Select a User:<br>
    <Select Name = "users" onChange='Choice();'>User Name:</Option>
    
    <?php
    while ($row = mysql_fetch_array($user_result))
    {
    echo '<Option value="'.$row['id'].'">'.$row['fullname'].'</Option>';
    }
    ?>
    
    </Select>
    <p><Input Type ="text" id="users2" name="users2">
    <p><Input Type ="Submit" Value ="Submit" Name ="submit" >
    
    </Form><p>
    </body>
    </html>

  2. #2
    jthayne's Avatar

    Status
    Offline
    Join Date
    Aug 2008
    Location
    Texas
    Posts
    508

    Re: [Javascript] Populate a Text field via MYSQL Drop Down

    Use the following Javascript function:
    Code:
    <script language=Javascript>
    function Choice()
    {
    x = document.getElementById("users2");
    y = document.getElementById("users");
    
    x.value = y.options[y.selectedIndex].text;
    }
    </script>
    Also, for your PHP (and HTML), try and use lowercase unless you must capitalize. This creates uniformity, and also insures you are using the correct case since PHP is case sensitive. This is just a recommendation, and not a requirement, so take it with a grain of salt.

  3. #3
    rangana's Avatar
    Moderator/Allstar

    Status
    Offline
    Join Date
    Feb 2008
    Location
    Cebu City Philippines
    Posts
    317

    Re: [Javascript] Populate a Text field via MYSQL Drop Down

    One thing from jthayne's code...please replace highlighted:
    Code:
    <script language=Javascript>
    ...with type="text/javascript"
    Checkout my porfolio.
    Please click the button when a member helped you.
    Take time to use Forum's Search function.

  4. #4
    jthayne's Avatar

    Status
    Offline
    Join Date
    Aug 2008
    Location
    Texas
    Posts
    508

    Re: [Javascript] Populate a Text field via MYSQL Drop Down

    Doh!

  5. #5
    xChAMaRx's Avatar
    New User

    Status
    Offline
    Join Date
    Feb 2009
    Posts
    4

    Re: [Javascript] Populate a Text field via MYSQL Drop Down

    Thanx guys, this has been a great help.

  6. #6
    xChAMaRx's Avatar
    New User

    Status
    Offline
    Join Date
    Feb 2009
    Posts
    4

    Re: [Javascript] Populate a Text field via MYSQL Drop Down

    Now I have a second question. I need to populate multiple text fields from the drop down selection.

    Scenario:

    PHP creates a dynamic drop down with results from a MySQL query. (working script)

    I select the client from the dropdown, which then populates two text fields with the telephone number and address.

    Unfortunately I have not idea on how to go about this, so any help would be appreciated.

  7. #7
    jthayne's Avatar

    Status
    Offline
    Join Date
    Aug 2008
    Location
    Texas
    Posts
    508

    Re: [Javascript] Populate a Text field via MYSQL Drop Down

    You have two options on this since I am assuming that the data needed is stored in the database.

    Option 1:
    Store the data in a javascript array, and populate the text boxes based on those values.

    Option 2:
    Use Ajax to retrieve the information from the database and output it to the screen similar to the example here

    Either way is fairly simple, just let me know which way you prefer to go, and we will get it working.

  8. #8
    xChAMaRx's Avatar
    New User

    Status
    Offline
    Join Date
    Feb 2009
    Posts
    4

    Re: [Javascript] Populate a Text field via MYSQL Drop Down

    Thanx jthayne. As I'm very familiar with Ajax, I would like to try the first option.

    Would be able to put together a sample so that I can try and work it into my code.

  9. #9
    jthayne's Avatar

    Status
    Offline
    Join Date
    Aug 2008
    Location
    Texas
    Posts
    508

    Re: [Javascript] Populate a Text field via MYSQL Drop Down

    Try this. If it does not work, make sure to describe/post any errors you get.

    Code:
    <?php
    #I moved the php to the beginning of the document for better organization
    
    // Make Connection
    include 'dbconfig.php';
    
    // Retreive data from database
    mysql_connect($dbhost2, $dbuser2, $dbpass2) or die(mysql_error());
    mysql_select_db($dbname2) or die(mysql_error());
    
    #Changed the query to pull the phone number and address as well.  This will need to be edited to reflect the correct column names.
    $user_query = "SELECT concat(first_name, ' ', last_name) as fullname, phone, address FROM users ORDER BY first_name";
    $user_result = mysql_query($user_query);
    $client_query = "SELECT name FROM accounts ORDER BY name";
    $client_result = mysql_query($client_query);
    
    #Put the values from the query into an array
    $uArr = array();
    while ($u = mysql_fetch_assoc($user_result)) {
    	$uArr[] = $u;
    }
    ?>
    
    <html>
    	<head>
    		<title>Job Card</title>
    		<script type="text/javascript">
    			var add = new Array();
    			var nam = new Array();
    			var pho = new Array();
    			<?php
    			foreach($uArr as $key=>$value) {
    				echo "add[" . $key . "] = '" . $value['address'] . "';\n";
    				echo "nam[" . $key . "] = '" . $value['address'] . "';\n";
    				echo "pho[" . $key . "] = '" . $value['address'] . "';\n";
    			}
    			?>
    
    			function Choice() {
    				x = document.getElementById("users2");
    				y = document.getElementById("users");
    				x.value = y.options[y.selectedIndex].text;
    				document.getElementById("add") = add[y.selectedIndex];
    				document.getElementById("pho") = pho[y.selectedIndex];
    			}
    		</script>
    	</head>
    	<body>
    		<form name="form1" method="post" action="submitform.php">
    			<p>Please Select a User:<br>
    			<select name="users" onChange='Choice();'>User Name:</Option>
    				<?php
    				foreach ($uArr as $key=>$value) {
    				while ($row = mysql_fetch_array($user_result)) {
    					echo '<option value="'.$key.'">'.$value['fullname'].'</option>';
    				}
    				?>
    			</select>
    			<p><input type="text" id="users2" name="users2"></p>
    			<p><input type="text" id="add" name="address"></p>
    			<p><input type="text" id="pho" name="phone"></p>
    			<p><input type="Submit" value="Submit" name="submit"></p>
    		</form>
    	</body>
    </html>

  10. #10
    chilldude's Avatar
    New User

    Status
    Offline
    Join Date
    Apr 2009
    Location
    Johannesburg, South Africa
    Posts
    1

    Re: [Javascript] Populate a Text field via MYSQL Drop Down

    Hi Guys,

    I need some help here, not to good with JS, and what i need, is full of it. I have been searching everywhere for something, but cant find it.

    i am writing a site, and need about 5 fields, 1 must have a auto suggest from a list in a DB, and then as i click on a selection, thise must then autofill field 2 and three with a desctiption and price. I then want to be able to enter a qty and have a piece at the bottom that add up all the lines entered.

    Can anyone help? eveb prepared to pay for something like this to be written.

    Thanks guys, cool site.

  11. #11
    rangana's Avatar
    Moderator/Allstar

    Status
    Offline
    Join Date
    Feb 2008
    Location
    Cebu City Philippines
    Posts
    317

    Re: [Javascript] Populate a Text field via MYSQL Drop Down

    When do you want the auto-suggest be triggered? At how many character length? I suppose on the fourth character.

    Drop me a PM, with your current markup/link and the column names from your table.

    I might work something out.
    Checkout my porfolio.
    Please click the button when a member helped you.
    Take time to use Forum's Search function.

  12. #12
    Kid's Avatar
    New User

    Status
    Offline
    Join Date
    Oct 2009
    Posts
    4

    Unhappy Re: [Javascript] Populate a Text field via MYSQL Drop Down

    I have the same problem as above and i tried to modified my codes according to what solution others have given but it won't work. I've tried for a few days already. I'm using dreamweaver cs3 to build my site and I'm new to php, js and others. I'm not sure if my codes failed because I'm using dreamweaver app to retrieve data from mysql.

    I need to populate my 3 text field when I select an item from the drop down menu. I managed to populate the drop down menu from table in mysql, I can't even get one text field right..

    This is the condition :
    SelectDropDown = Course Code(Kod_Kursus)
    TextField1 = CourseName(Nama_Kursus)
    TextField2 = TimeCreditTheory(Jam_Kredit_Teori)
    TextField3 = TimeCreditPractical(Jam_Kredit_Amali)

    #retrieve from mysql by dreamweaver
    <?php require_once('Connections/mysql.php'); ?>
    <?php
    if (!function_exists("GetSQLValueString")) {
    function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "")
    {
    $theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;

    $theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue);

    switch ($theType) {
    case "text":
    $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
    break;
    case "long":
    case "int":
    $theValue = ($theValue != "") ? intval($theValue) : "NULL";
    break;
    case "double":
    $theValue = ($theValue != "") ? "'" . doubleval($theValue) . "'" : "NULL";
    break;
    case "date":
    $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
    break;
    case "defined":
    $theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
    break;
    }
    return $theValue;
    }
    }

    mysql_select_db($database_mysql, $mysql);
    $query_ListCourse = "SELECT * FROM kursus";
    $ListCourse = mysql_query($query_ListCourse, $mysql) or die(mysql_error());
    $row_ListCourse = mysql_fetch_assoc($ListCourse);
    $totalRows_ListCourse = mysql_num_rows($ListCourse);
    ?><html>
    <head><title>Course Listing</title>

    #follow the instructions in forums
    <script type="text/javascript">
    function Choice() {
    x = document.getElementById("Nama_Kursus");
    y = document.getElementById("Kod_Kursus");

    x.value = y.options[y.selectedIndex].text;
    }

    </script>
    </head><body>
    <form name ="form1" Method ="POST" Action ="">
    Code :
    <select name="Kod_Kursus" id="Kod_Kursus" onChange='Choice();'>
    <option value="">-pilih kod kursus-</option>
    <?php
    do {
    ?>
    <option value="<?php echo $row_ListCourse['Kod_Kursus']?>"><?php echo $row_ListCourse['Kod_Kursus']?></option>
    <?php
    } while ($row_ListCourse = mysql_fetch_assoc($ListCourse));
    $rows = mysql_num_rows($ListCourse);
    if($rows > 0) {
    mysql_data_seek($ListCourse, 0);
    $row_ListCourse = mysql_fetch_assoc($ListCourse);
    }
    ?>

    </select> <br>

    Courses name :

    <input type="text" name="Nama_Kursus" id="Nama_Kursus"> <br>

    # can't even get the 1st text field right..so as this two below
    Time Credit (theory) :

    <input name="Jam_kredit_Teori" type="text" id="Jam_Kredit_Teori" size="3">

    Time Credit (Practical) :

    <input name="Jam_Kredit_Amali" type="text" id="Jam_Kredit_Amali" size="3">


    </form>
    </body>
    </html>
    <?php
    mysql_free_result($ListCourse);
    ?>

    help me plss....



Tags for this Thread