Go Back   CodingForums.com > :: Client side development > JavaScript programming > Ajax and Design

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 09-01-2010, 10:46 AM   PM User | #1
jojo1234
New to the CF scene

 
Join Date: Aug 2010
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
jojo1234 is an unknown quantity at this point
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
jojo1234 is offline   Reply With Quote
Old 09-18-2010, 06:18 PM   PM User | #2
ashishchaudhary
New Coder

 
Join Date: Sep 2010
Posts: 27
Thanks: 2
Thanked 0 Times in 0 Posts
ashishchaudhary is an unknown quantity at this point
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..
ashishchaudhary is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

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 On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 01:07 AM.


Advertisement
Log in to turn off these ads.