Page 1 of 1

Using Forms to Requery DB

Posted: Mon Apr 02, 2007 4:39 pm
by citytours
Sorry if this is a stupid question, I am new to PHP.

I am trying to use a form to query a database using a function. When I load the PHP page it seems to call my function (even though I do not want it to). Then when I press the form button it seems to call the function but the results from the query are those of when I initially loaded the page. Can anyone tell me why this is happening or perhaps give me an alternative? Here is my code:

*** PLEASE USE THE

Code: Select all

OR [SYNTAX] TAG WHEN POSTING CODE ***[/color]

[syntax="php"]<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
    <meta http-equiv="content-type" content="text/html; charset=UTF-8"/>
  </head>

  <body>

    <form onsubmit="doQuery(); return false;">
      <p>
        <input type="submit" value="Search" />
      </p>
    </form>

    </body>
</html>
    <script type="text/javascript">
   
    function doQuery() {
<?php
	if (!$link = odbc_connect("MyDB","dba","sql")) {
	   echo "Could not connect to $database!\n"; 
	   exit;
	}		

	$result = odbc_exec($link, "select col1 from test");
	if (!$result)
	{
		echo "no results ";
	} 

	while(odbc_fetch_row($result))
	{
		echo "alert('" . odbc_result($result,'col1') . "')";
	}

	odbc_free_result($result);

?>

    }

    </script>
[/syntax]

Posted: Mon Apr 02, 2007 6:33 pm
by neophyte
I could give you the answer but I'd rather teach you how to fish.

http://www.hudzilla.org/php/7_0_0.php

Read that chapter.

Everything will be much clearer if you do.

Posted: Mon Apr 02, 2007 6:39 pm
by RobertGonzalez
EDIT | Sorry, didn't come out right.

Every page load will call the doQuery JS function because you are telling it to do that with the onLoad call in the body tag.

You data will also not change at all because this query:
"select col1 from test"

Is what is filling the data in that function.

Posted: Tue Apr 03, 2007 8:11 am
by citytours
Thank-you for the responses.

Neophyte, I read that chapter but I am afraid it did not help (perhaps I missed something?).

Everah, I am confused. I am not calling doQuery in the body onload. If I would have been, would it not look like this?

Code: Select all

<body onload="doQuery()">
Thanks,
Gary

Posted: Tue Apr 03, 2007 8:37 am
by Begby
This is because PHP runs on the server before the page gets to the browser, so all of your code will get executed irregardless of any javascript stuff.

Think of PHP as a program than runs and then spits out HTML, this HTML is then sent to your browser. Load that page up that you created and look at the source. Notice that its just HTML and all of your PHP is gone.

Posted: Tue Apr 03, 2007 8:51 am
by citytours
Thanks Begby, so is there another way to do this to dynamically get query results based on when someone presses a button? I would really prefer not to have to reload the page but rather dynamically update just the query results (say in a grid). Is DHTML a good way to do this?

Posted: Tue Apr 03, 2007 9:05 am
by Begby
citytours wrote:Thanks Begby, so is there another way to do this to dynamically get query results based on when someone presses a button? I would really prefer not to have to reload the page but rather dynamically update just the query results (say in a grid). Is DHTML a good way to do this?
You are going to need to use ajax to do this. I suggest using a javascript library such as prototype to do it. Your script will have two parts, the HTML page with the javascript, and a PHP page that does the query and returns the results. The ajax part will be a function that remotely calls the PHP page when you click a button, gets the results, then displays them on your page in a div tag or something.

Before you delve into this, I strongly suggest becoming a little more versed in PHP/HTML/Scripting so that you understand how client/server scripting works and also understand a bit more about php syntax.

Posted: Tue Apr 03, 2007 9:13 am
by citytours
Thanks Begby, I will do that.

Posted: Tue Apr 03, 2007 9:50 am
by RobertGonzalez
citytours wrote:Everah, I am confused. I am not calling doQuery in the body onload. If I would have been, would it not look like this?
Sorry, I read your post wrong. You were not doing what I thought you were. My fault.

Why do you not want to make the trip back to the server? Just thought I'd ask to get a feel for what you are attempting to do.

Posted: Tue Apr 03, 2007 11:04 am
by citytours
For anyone reading this and is looking for a similar solution, I found the following simple example which seems to do everything I need.

http://www.w3schools.com/php/php_ajax_database.asp

Thanks everyone!