SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Member
    Join Date
    Mar 2005
    Posts
    2
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Problem passing data from form

    Hi all, PHP newbie here!

    Designed a form to search a MySQL DB (code below) and my problem is that the name being searched for is always one step behind. i.e.

    Step 1: search for "Smith" and nothing is searched for (name attribute in URL is blank)
    Step 2: search for "Jones" and form returns results for Smith (name attribute in URL is Smith)
    Step 3: search for "Johnson" and form returns results for Jones
    etc etc

    Code is:

    <?php
    echo '<form method="post" name="find_name" action="';
    echo $PHP_SELF;
    echo '?name=';
    echo $name;
    echo '"><input type="text" name="name" id="name">&nbsp;<input type="submit" name="find_name" value="Find Name" />';
    ?>

    <?php
    if ($find_name) {

    $surname=$_GET['name'];

    $result = mysql_query("SELECT * FROM $table WHERE last = '$surname'",$db);

    }
    ?>

    <?php
    while ($row = mysql_fetch_array($result)) {

    echo '<tr><td><p><strong>';
    echo $row["title"];
    echo '&nbsp;';
    echo $row["first"];
    etc...

    }
    ?>

  2. #2
    SitePoint Evangelist djdykes's Avatar
    Join Date
    Feb 2005
    Location
    Chester, Cheshire
    Posts
    566
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    for a start, your posting the form and the search is querying the $_GET variables... it should be $_POST

    your form should look like this

    <form method="post" name="find_name" action="">
    <input type="text" name="name" /><br />
    <input type="submit" name="find_name" value="Find Name" />
    </form>

    then the code would be:

    $surname=$_POST['name'];

    $result = mysql_query("SELECT * FROM $table WHERE last = '" . $surname ."' ", $db);

    the rest looks alright to me tho

  3. #3
    SitePoint Member
    Join Date
    Mar 2005
    Posts
    2
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    D'oh!

    Thanks for that, worked a treat.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •