Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 6 of 6
  1. #1
    New to the CF scene
    Join Date
    Mar 2013
    Posts
    2
    Thanks
    1
    Thanked 0 Times in 0 Posts

    java select form database to text field.

    Hello coding forum .. I have a small problem .. or I do not know how to do this.

    so when I select something from my "select". I want it to write the name of the person from the database which has ID 1, in my input "text" field

    Code:
    <?php
    
    include("connect.php");
    $result= mysql_query("SELECT * FROM persons WHERE id = '".$q."'" );
    $row = mysql_fetch_assoc($result)
    
    ?>
    
    
    <form action="update.php"  method="post">
    
    <select name="sel1" id="sel1">
            	<option value="1">select 1</option>
                    <option value="2">select 2</option>
            	<option value="3">select 3</option>
            	<option value="4">select 4</option>
            	<option value="5">select 5</option>
    </select>
    
    <input type="text" id="text1" name="text1" value="<?php $row[name] ?>">
    <input type="submit">
    </form>

  • #2
    Senior Coder
    Join Date
    Dec 2010
    Posts
    2,398
    Thanks
    12
    Thanked 570 Times in 563 Posts
    I think the only thing that is missing from the update.php you are showing here: You use a variable $q without copying the value from the select to it.
    Code:
    <?php
    
    include("connect.php");
    $q = $_POST["sel1"];
    $result= mysql_query("SELECT * FROM persons WHERE id = '".$q."'" );
    $row = mysql_fetch_assoc($result)
    
    ?>
    
    
    <form action="update.php"  method="post">
    
    <select name="sel1" id="sel1">
            	<option value="1">select 1</option>
                    <option value="2">select 2</option>
            	<option value="3">select 3</option>
            	<option value="4">select 4</option>
            	<option value="5">select 5</option>
    </select>
    
    <input type="text" id="text1" name="text1" value="<?php $row[name] ?>">
    <input type="submit">
    </form>

  • #3
    Regular Coder
    Join Date
    Jan 2013
    Location
    Germany
    Posts
    578
    Thanks
    4
    Thanked 77 Times in 77 Posts

  • #4
    New to the CF scene
    Join Date
    Mar 2013
    Posts
    2
    Thanks
    1
    Thanked 0 Times in 0 Posts
    i would like it to work something like this.. http://www.w3schools.com/php/php_ajax_database.asp

    and the website will only be used for internal use.

  • #5
    Senior Coder
    Join Date
    Feb 2011
    Location
    Your Monitor
    Posts
    4,332
    Thanks
    60
    Thanked 526 Times in 513 Posts
    Blog Entries
    4
    One question.. What does this have to do with java?

    lol
    See my new CodingForums Blog: http://www.codingforums.com/blogs/tangoforce/

    Many useful explanations and tips including: Cannot modify headers - already sent, The IE if (isset($_POST['submit'])) bug explained, unexpected T_CONSTANT_ENCAPSED_STRING, debugging tips and much more!

  • #6
    Regular Coder Arcticwarrio's Avatar
    Join Date
    May 2012
    Location
    UK
    Posts
    721
    Thanks
    20
    Thanked 84 Times in 84 Posts
    i use that script a lot and it works fine,

    you need this in the head:

    PHP Code:
    <head>
    <
    script>
    function 
    showUser(str)
    {
    if (
    str=="")
      {
      
    document.getElementById("text1").value="";
      return;
      }
    if (
    window.XMLHttpRequest)
      {
    // code for IE7+, Firefox, Chrome, Opera, Safari
      
    xmlhttp=new XMLHttpRequest();
      }
    else
      {
    // code for IE6, IE5
      
    xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
      }
    xmlhttp.onreadystatechange=function()
      {
      if (
    xmlhttp.readyState==&& xmlhttp.status==200)
        {
    //alter this line to the field you want and change from innerHTML to value eg..
        
    document.getElementById("text1").value=xmlhttp.responseText;
        }
      }
    xmlhttp.open("GET","getuser.php?q="+str,true);
    xmlhttp.send();
    }
    </script>


    </head> 
    in this example you need a file called getuser.php with the following in it.

    PHP Code:
    <?php

    $q
    =$_GET["q"];

    include(
    "connect.php");
    $resultmysql_query("SELECT * FROM persons WHERE id = '".$q."'" );
    $row mysql_fetch_assoc($result)
    echo 
    $row[name];
    ?>
    and for your form you need an onchange event to call it

    PHP Code:


    <form action="update.php"  method="post">

    <
    select name="sel1" id="sel1" onchange="showUser(this.value);">
                <
    option value="1">select 1</option>
                    <
    option value="2">select 2</option>
                <
    option value="3">select 3</option>
                <
    option value="4">select 4</option>
                <
    option value="5">select 5</option>
    </
    select>

    <
    input type="text" id="text1" name="text1" value="">
    <
    input type="submit">
    </
    form
    Last edited by Arcticwarrio; 03-11-2013 at 09:51 AM.
    There are 10 types of people on CodingForums,
    Those who understand Binary and those who dont.
    Get Cloud Hosting now from only£59 / month

  • Users who have thanked Arcticwarrio for this post:

    fruth (03-13-2013)


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •