Using Forms to Requery DB

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
citytours
Forum Newbie
Posts: 5
Joined: Mon Apr 02, 2007 4:33 pm

Using Forms to Requery DB

Post 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]
User avatar
neophyte
DevNet Resident
Posts: 1537
Joined: Tue Jan 20, 2004 4:58 pm
Location: Minnesota

Post 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.
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post 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.
citytours
Forum Newbie
Posts: 5
Joined: Mon Apr 02, 2007 4:33 pm

Post 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
Begby
Forum Regular
Posts: 575
Joined: Wed Dec 13, 2006 10:28 am

Post 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.
citytours
Forum Newbie
Posts: 5
Joined: Mon Apr 02, 2007 4:33 pm

Post 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?
Begby
Forum Regular
Posts: 575
Joined: Wed Dec 13, 2006 10:28 am

Post 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.
citytours
Forum Newbie
Posts: 5
Joined: Mon Apr 02, 2007 4:33 pm

Post by citytours »

Thanks Begby, I will do that.
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post 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.
citytours
Forum Newbie
Posts: 5
Joined: Mon Apr 02, 2007 4:33 pm

Post 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!
Post Reply