Using Forms to Requery DB
Posted: Mon Apr 02, 2007 4:39 pm
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[/syntax]
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>