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 2 of 2
  1. #1
    New to the CF scene
    Join Date
    Aug 2010
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    get database query result to text field when button clicked

    I have a problem in getting database query result to text field when button was clicked. So far I have two php files, one used to connect database and save the database query value to a variable. The second php file used to create the buttons and text field.
    code for dbconnection.php
    [code]
    $con_str = "host=jojo1234 port=3214 dbname=postgres user=postgres password=postgres";
    $con = pg_connect("$con_str") or die('Could not connect: ' . pg_last_error());
    $queryresult1 = pg_query($query1);
    $result11=array_sum(pg_fetch_array($queryresult1));
    [code]
    code for interface.php
    [code]
    <html>
    <head> jsadfj</head>
    <body>
    <form name = "weightform" action = "dbconnect.php" method = "post" target="hidden">

    <input type="submit" name="submit" value = ""/>
    <input id='wb1' type="text" name="wb1" value=""/>
    </form>
    </body>
    </html>
    [code]
    my problem is how to get ī$result11 vlaue to text field wb1 when button submit was clicked. By using form, these two php files can hear each other. but I donīt know how to get first php file variable value. I have been thinking about the solution. one solution is to create funtion called getTxtField(), here is psudo code
    [code]
    getTxtField(){
    connect to database
    wb1.value=$result11
    }
    <input type="submit" name="submit" value = onclick"getTxtField()" />
    [code]
    but I donīt know if this [ICODE]wb1.value=$result11[ICODE] can be done. what could be the right syntax.

    This is my first time program with javascript. some javascript syntax still new for me. Does anyone can help me to get sample code which could help me with this problem.I posted this question to javascript forum, but one persion replied me and said this is ajax problem. Can i use ajax to solve this problem? As far as I know ajax can help me to read xml or asp files. But I donīt really have those files. I also donīt have webserver installed in my computer. I just want to simple query to the database and display the query result to the webpage text field.
    Thanks a lot

  • #2
    New Coder
    Join Date
    Sep 2010
    Posts
    27
    Thanks
    2
    Thanked 0 Times in 0 Posts
    I think this would be better for you , and i also giving you an advice please also learn ajax or jquery without these there is nothing..

    Your problem will be done with ajax very easily to see how it is take a look upon these codes


    this would be your interface file named interface.php
    Code:
    <html>
    <head><title> Mypage </title>
    
    <script type="text/javascript">
    
     function getvalue()
     {
    
         if(window.XMLHttpRequest)
         {
    
           xmlhttp = new XMLHttpRequest();
         }
         else
         {
    
           xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
         }
     
         xmlhttp.onreadystatechange=function()
         {
    
           if(xmlhttp.readyState==4 && xmlhttp.status==200)
           {
    
              document.form1.textbox1.value = xmlhttp.responseText;
           }
    
         }
    
         xmlhttp.open("GET","dbconn.php",true);
         xmlhttp.send();
    
     }
    
    
    </script>
    
    
    </head>
    
    
    <body>
    
    
    <form name="form1">
    
     <input type="text" name="textbox1" value=" " size="40" ><br>
     <input type="button" name="submit" value="Get Value" onclick="getvalue()">
    
    </form>
    
    
    </body>
    </html>
    and this would be your database file i have taken your last result variable that contain your value named result11 , simply display it with echo and it will be in your text box

    this file name is dbconn.php

    Code:
    <?php
    
      $result11 = "what ever your value";
    
      echo $result11;
    
    ?>
    Thank you..


  •  

    Posting Permissions

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