views:

568

answers:

4

I know this is probably a really amateur question, but I can't figure this out and I don't know much about PHP or MySQL.

So I have a really simple script that basically allows a user to submit a couple lines of text and their zipcode, then it write it to a database and returns the results on the site. It's a shoutbox essentially.

My client wants the users to be able to filter the results by zipcode. So I have it all setup, and I have a search input where people type in their zipcode, search, and then the PHP returns the submissions from that zipcode.

While I can get the results to show by themselves echoed from the PHP script, how do I get them to display within a specified div within the site? I want it essentially to store a variable, the zipcode, that a user search by, and then use that once the page refreshes to display an updated list that filters out the results that aren't from that zipcode.

Thanks so much for the help.

Chris

A: 

Make the HTML page a PHP page (usually by making the extension .php). Replace the target div's content with <?php stuff to get and print results ?>.

If you have PHP installed and configured on the web server, it will take certain files (usually those ending in .php) and run them through PHP's interpreter. The PHP interpreter is invoked anytime there is a <?php in the document and it stops parsing content anytime it meets a ?> within the <?php block.

For instance, to get a web page to print "Hello World!" into a div through PHP, I would do:

<div><?php print 'Hello World!'; ?></div>

cookiecaper
Thank you for the quick response! I'm actually running into a problem though because I'm using a CMS, TextPattern, so I can' call individual pages within there... Or is there a way and I'm just an idiot?
Chris Schmitz
A: 

OK, I figured it out. I just stored my answer in a SESSION variable so that I could call it in my other PHP files.

Now I have one file processing the request, creating the session and the variable, and then redirecting the page. Then I have a PHP included in the page that grabs the SESSION variable and plugs it into my mysql_query to return the proper result!

Not sure if this is the best way of doing it, but it's working.... If anyone knows of a more elegant solution I would love to know it.

Thanks, Chris

Chris Schmitz
You could just as easily include the PHP script you are redirecting to, without the need for the session variable.
Dustin Fineout
A: 

I suggest you do some tutorials on using PHP and database retrieval with mysqli.

<?php

/* Connect to a MySQL server */
$link = mysqli_connect(
            'localhost',  /* The host to connect to */
            'user',       /* The user to connect as */
            'password',   /* The password to use */
            'world');     /* The default database to query */

if (!$link) {
   printf("Can't connect to MySQL Server. Errorcode: %s\n", mysqli_connect_error());
   exit;
}

/* Send a query to the server */
if ($result = mysqli_query($link, 'SELECT Name, Population FROM City ORDER BY Population DESC LIMIT 5')) {

    print("Very large cities are:\n");

    /* Fetch the results of the query */
    while( $row = mysqli_fetch_assoc($result) ){
        printf("%s (%s)\n", $row['Name'], $row['Population']);
    }

    /* Destroy the result set and free the memory used for it */
    mysqli_free_result($result);
}

/* Close the connection */
mysqli_close($link);
?>
Yada
+1  A: 

Without getting into rewriting etc.:

  1. When you redirect your page, add a query at the end of it that is the zipcode making sure that you don't send any whitespace and that you have an input in a given format:

    search.php?zipcode=90210

  2. Then on your search results page, fetch the variable passed and work as usual:

    $zipcode = $_GET['zipcode'];

  3. make sure that the zipcode is properly escaped and filtered for nasties.

Radek
would you happen to know of any resources to help me learn how to "filter for nasties"?
Chris Schmitz
Sure, for ex. Kevin Waterson has an exhaustive article on that: http://www.phpro.org/tutorials/Validating-User-Input.html
Radek
Thanks for the help Radek. Much appreciated.
Chris Schmitz