Jump to content

Help returning search results as hyperlinks that go to previous php form


Recommended Posts

Hi all,

 

I am new to PHP and am trying to implement a window that opens when a user is trying to validate an organisation on our site. I have an initial page where users can enter data. One of the fields is organisations. This has a button to its right that lets the user search existing organisations in the mysql db.

 

I have the search piece thanks to some code I found and altered.

 

However, instead of a static non-linked list 1,2,3 coming back from the search, I need the search results to be links / or at least "select"-able. When the user chooses the organisation they were referring to that exists in the db I would like the selection to go back to the original php page and populate the organisation field. Ideally without having other data in the original form being lost.

 

Any tips, hints or links would be greatly appreciated.

 

Here is my code:

 

<?php

 

$dbHost = 'localhost';

$dbUser = 'root';

$dbPass = 'mypass';

$dbDatabase = 'mydb';

$con = mysql_connect($dbHost, $dbUser, $dbPass) or trigger_error("Failed to connect to MySQL Server. Error: " . mysql_error());

 

mysql_select_db($dbDatabase) or trigger_error("Failed to connect to database {$dbDatabase}. Error: " . mysql_error());

 

// Set up our error check and result check array

$error = array();

$results = array();

 

// First check if a form was submitted.

// Since this is a search we will use $_GET

if (isset($_GET['search'])) {

  $searchTerms = trim($_GET['search']);

  $searchTerms = strip_tags($searchTerms); // remove any html/javascript.

 

  if (strlen($searchTerms) < 3) {

      $error[] = "Search terms must be longer than 3 characters.";

  }else {

      $searchTermDB = mysql_real_escape_string($searchTerms); // prevent sql injection.

  }

 

  // If there are no errors, lets get the search going.

  if (count($error) < 1) {

      $searchSQL = "SELECT Organisation_Name FROM vcaccount_contact_organisation WHERE ";

     

      // grab the search types.

      $types = array();

      $types[] = isset($_GET['Organisation_Name'])?"`Organisation_Name` LIKE '%{$searchTermDB}%'":'';

 

     

      $types = array_filter($types, "removeEmpty"); // removes any item that was empty (not checked)

     

      if (count($types) < 1)

        $types[] = "`Organisation_Name` LIKE '%{$searchTermDB}%'"; // use the body as a default search if none are checked

     

          $andOr = isset($_GET['matchall'])?'AND':'OR';

      $searchSQL .= implode(" {$andOr} ", $types) . " ORDER BY `Organisation_Name`"; // order by organisation.

 

      $searchResult = mysql_query($searchSQL) or trigger_error("There was an error.<br/>" . mysql_error() . "<br />SQL Was: {$searchSQL}");

     

      if (mysql_num_rows($searchResult) < 1) {

        $error[] = "The search term provided {$searchTerms} yielded no results.";

      }else {

        $results = array(); // the result array

        $i = 1;

        while ($row = mysql_fetch_assoc($searchResult)) {

            $results[] = "{$i}: {$row['Organisation_Name']}<br /><br />";

            $i++;

        }

      }

  }

}

 

function removeEmpty($var) {

  return (!empty($var));

}

?>

<html>

  <title>My Simple Search Form</title>

  <style type="text/css">

      #error {

        color: red;

      }

  </style>

  <body>

      <?php echo (count($error) > 0)?"The following had errors:<br /><span id=\"error\">" . implode("<br />", $error) . "</span><br /><br />":""; ?>

      <form method="GET" action="<?php echo $_SERVER['PHP_SELF'];?>" name="searchForm">

        Organisation: <input type="text" name="search" value="<?php echo isset($searchTerms)?htmlspecialchars($searchTerms):''; ?>" /><br />

                <input type="submit" name="submit" value="Search!" />

      </form>

      <?php echo (count($results) > 0)?"Your search term: {$searchTerms} returned:<br /><br />" . implode("", $results):""; ?>

  </body>

</html>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.