Forums

Simple database search - where to put php?

zerynthia 12 Mar, 2008
Hello,

I'm tyring to do a very simple database seach form with just one field and a search button. The search form works in a separate php-file (on localhost), but not with the form I created using chronoforms. Is the placement of my php maybe wrong?

In "Form HTML" I have the following code:

<input type="text" name="hakuehto" size="20" maxlength="20" value="" /> 
<input type="submit" value="HAE">


And this php code I tried putting both in "Form HTML" and "On Submit code - before sending email:" but neither seems to work. All I get after hitting Search is a basic joomla page with no content. And I am of course supplying a searchword that exists in the database.


<?php
if (isset($_POST['hakuehto'])) 
	{
		$yhteys = mysql_connect("localhost", "**", "**"«») or die(mysql_error()); 
		mysql_select_db('yrityshaku', $yhteys) or die(mysql_error());	
		
		$_hakuehto = $_POST['hakuehto']; 
		
		$result=mysql_query("SELECT * FROM perustiedot where toiminimi LIKE '%$_hakuehto%'"«»);
			if (!$result) 
			{
    			die('Invalid query: ' . mysql_error());
			}
		
		$num=mysql_numrows($result);
		
			echo "<b><center>Database Output</center></b><br><br>";

			$i=0;
			while ($i < $num) 
                          {
				$ytunnus=mysql_result($result,$i,"ytunnus"«»);
				$toiminimi=mysql_result($result,$i,"toiminimi"«»);
				$osoite=mysql_result($result,$i,"osoite"«»);
				$puhelin=mysql_result($result,$i,"puhelin"«»);
				$www=mysql_result($result,$i,"www"«»);
				$toimiala=mysql_result($result,$i,"toimiala"«»);

			echo "<b>$ytunnus $toiminimi</b><br>Osoite: $osoite<br>Puhelin: $puhelin<br>Www: $www<br>Toimiala: $toimiala<br><hr><br>";
			$i++;
			}
		}
?>


Thanks for your help!<br><br>Post edited by: zerynthia, at: 2008/03/11 22:15
GreyHead 12 Mar, 2008
Hi zerynthia,

From a quick look I don't see anything 'wrong' with your code.

Do you know where it stops running? I'd put in echo "XXX"; in various places to track down the line where the error is and see what is happening there.

You might also echo the $sql, then you can copy and paste that into PHPMyAdmin and see if it gives you the result you want. You need to change the code slightly to do that:
$sql = "SELECT * FROM perustiedot where toiminimi LIKE '%$_hakuehto%'";
echo $sql;
$result=mysql_query($sql);
Bob
zerynthia 13 Mar, 2008
Thank you for your answer. I belive that the code is working, becouse it works when it is "on it's own" in a stand alone php -file, with the <form></form> -element in the beginning of the same file.

I'm actually wondering where or in what format I can place the form-(html)code and the php-code?
Max_admin 13 Mar, 2008
You should have some switcher, some if statement to check for some posted variable, if its true then do the query, if its false then show the HTML!

Cheers

Max
Max, ChronoForms developer
ChronoMyAdmin: Database administration within Joomla, no phpMyAdmin needed.
ChronoMails simplifies Joomla email: newsletters, logging, and custom templates.
zerynthia 14 Mar, 2008
Thank you, but I'm not sure I understand. I'd like the searchbox to be visible in both cases, you know, like a search engine 1) before doing the search and 2) after doing the search.. but actually the form disappears when doing the search.

And I belive that I already have an if statement that checks "if the global variable $_POST is set" then run the php inside the brackets.

I switched the Debug from chronoengine on, and hitting the search with the searchword "oy", gives me the following line: "_POST: Array ( [hakuehto] => oy )"

So it would seem to me that the php-code is not able to affect the post-method of the form.. or somethgin like that?
GreyHead 14 Mar, 2008
Hi zerynthia,

The HTML form code should go into the Form HTML box. The code to process the result should go into one of the On Submit boxes.

If the result isn't displaying then there is most likely something wrong with your code. Please go back and check my earlier answer about adding some diagnostics. You could at least put something to print if the if test fails.

Bob

Bob
glens1234 14 Mar, 2008
Hi. i didnt really read through the replies above as im in a slight hurry.
However, i had a similar issue.

I firsly created a new folder in the 'componants' folder.
I think the folder has to start with com_
i.e com_mycom

Then i saved my form processing script i.e myscript.php in the com_mycom folder.

My script looks like this...

<?php

// no direct access
defined( '_JEXEC' ) or die( 'Restricted access' );
// require the html view class
jimport( 'joomla.application.helper' );
$database = & JFactory::getDBO();


$lang_spoken = JRequest::getVar( 'languages_spoken' );
$lang_imploded = implode(",", $lang_spoken);

$query = "SELECT name, languages_spoken, ipaddress FROM jos_chronoforms_1 WHERE languages_spoken LIKE '$lang_imploded%'";

$database->setQuery($query);
$rows = $database->loadObjectList();

echo "<h2>Translator Search</h2><br />";

print "<table width=\"400\" border=\"0\">\n";
print "<tr>\n";
print "<td><h3>Name</h3></td><td><h3>Languages Spoken</h3></td><td><h3>Who's Online</h3></td>\n";
print "</tr>\n";
foreach ( $rows as $row ) {
print "<tr>\n";
print "<td>$row->name:</td><td>$row->languages_spoken</td><td>$row->ipaddress:</td>\n";
print "</tr>\n";

}
print "</table><br /><br /><br /><br />";
?>



so bascially...
As far as i am aware you have to use the joomla database connection classes i.e

jimport( 'joomla.application.helper' );
$database = & JFactory::getDBO();


$lang_spoken = JRequest::getVar( 'languages_spoken' );


JRequest::getVar() replaces $_POST as it is apparently more secure.

Then you have methods such as...

$database->setQuery($query);
$rows = $database->loadObjectList();


..in order to get the query string.


Hope that helps a bit.
glens1234 14 Mar, 2008
oh yeh and in the form url section of chronoforms
add something like..
index.php?option=com_mycom
GreyHead 14 Mar, 2008
Hi Glens1234 & zerynthia,

This will work OK I think. I'd do the same thing but put the code into a new ChronoForms form rather than create a new component folder.

Bob
glens1234 14 Mar, 2008
Hi Bob!

Creating a new componant folder was the only way i could get the script to connect to the database for some reason.

Whenever i tried to execute the same script elsewhere nothing happened.

It works anyway. B)

cheers!
zerynthia 15 Mar, 2008
Thanks for your answers! Altough, I think I'll try to do this with some other tool than chronoforms. It's getting too difficult for me😟 . InlcudePHP is working ok half the way, but there too I have problems with including it with joomla... dunno if this joomla-thingy is that good after all :unsure:
glens1234 15 Mar, 2008
if you want to show me the php code you are using for processing the form ill change it in the same way i changed mine to get it working with joomla. Im not a php/joomla guru by any means but i glady have a go.
glens1234 15 Mar, 2008
also it may help to post the autogenerate code produced by chrono forms. This is located under the autogenerated code tab in chronoforms.
GreyHead 15 Mar, 2008
Hi glens1234,

That's odd, seems to work OK in the Autogenerated tab and I think the others are executed in the same way.

Bob
zerynthia 16 Mar, 2008
Thank you glens1234, but it's ok now, I got it working with inlcudephp (it was just a matter of using the $PHP_SELF -attribute as the form action) B).
This topic is locked and no more replies can be posted.