Form submit POST & php on one file problem.

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Dave Siegel
    New Member
    • Feb 2007
    • 42

    #1

    Form submit POST & php on one file problem.

    For some reason the submit POST and php on one form doesn't work for me.

    Here's my form code (it's dutch)

    [html] <form method="POST">
    <select name="locatie" size="1">
    <option value="">Kies de locaties</option>
    <option>Parij s</option>
    <option>Lyon</option>
    <option>Marseil e</option>
    <option>Lile </option>

    </select>
    <br>
    <br>
    <select name="prijs" size="1">
    <option value="">Kies de prijzen</option>
    <option value="1">0-100.000 </option>
    <option value="2">100.0 0-200.00 </option>
    <option value="3">200.0 00-300.000 </option>
    <option value="4">300.0 00-400.000 </option>
    <option value="5">400.0 00-500.000 </option>
    <option value="6">500.0 00-600.000 </option>
    <option value="7">600.0 00 en meer </option>

    </select>
    <br>
    <br>
    <input type="submit" name="button" id="button" value="Zoeken" />
    </form>[/html]


    And here's my php code :

    [PHP]<?
    ERROR_REPORTING (E_ALL);
    include("config .php");
    if(isset($_POST['button']))
    {
    $location = $_POST['locatie'];
    $price = $_POST['prijs'];

    //check what price it is
    if($locatie == ''){
    echo"Foute locatie gekozen, Kies aub een goede locatie.";
    }

    if($price == ''){
    echo"Foute prijs gekozen. Kies aub een goede prijs.";
    }

    elseif($price == '1'){


    $query = "SELECT * FROM houses WHERE location = '" . $location . "' AND price >= 0 AND price <= 100000";


    }

    elseif($price == '2'){
    $query = "SELECT * FROM houses WHERE location = '" . $location . "' AND price >= 100000 AND price <= 200000";
    }

    elseif($price == '3'){
    $query = "SELECT * FROM houses WHERE location = '" . $location . "' AND price >= 200000 AND price <= 300000";
    }

    elseif($price == '4'){
    $query = "SELECT * FROM houses WHERE location = '" . $location . "' AND price >= 300000 AND price <= 400000";
    }

    elseif($price == '5'){
    $query = "SELECT * FROM houses WHERE location = '" . $location . "' AND price >= 400000 AND price <= 500000";
    }

    elseif($price == '6'){
    $query = "SELECT * FROM houses WHERE location = '" . $location . "' AND price >= 500000 AND price <= 600000";
    }

    elseif($price == '7'){
    $query = "SELECT * FROM houses WHERE location = '" . $location . "' AND price <= 600000";
    }

    while($results = mysql_fetch_arr ay($query))
    {

    echo "<table width='434' height='72' border='0'>";

    echo "<tr><td width='133' height= '130'>";

    echo "<img width='132' height='129' src='" . $results['Foto1'] . "'></img>";

    echo "</td><td class='style5'> ";

    echo "Type: " . $results['type'] . "<br>Descriptie :" . $results['description'];

    echo "</td><td class='style5'> ";

    echo "prijs: " . $results['price'] . ".-";

    echo "</td></tr>";

    echo "</table>";

    }
    }
    ?>[/PHP]


    Any idea why it doesn't work?

    Help is much appreciated.
  • Atli
    Recognized Expert Expert
    • Nov 2006
    • 5062

    #2
    Hi Dave.

    First of all, I'd recommend changing the <? tag in your PHP code into <?php. The short-tags version you are currently using is disabled by default in PHP and can cause problems on other servers.

    As for your problem. The <form> tag is missing the 'action' property. Without that the form won't know where to send the data. So even if the data is meant to be posted to the current page, set the 'action' property.

    For example:
    [code=html]
    <form action="thisPag e.php" method="POST">
    [/code]

    Comment

    • Dave Siegel
      New Member
      • Feb 2007
      • 42

      #3
      Originally posted by Atli
      Hi Dave.

      First of all, I'd recommend changing the <? tag in your PHP code into <?php. The short-tags version you are currently using is disabled by default in PHP and can cause problems on other servers.

      As for your problem. The <form> tag is missing the 'action' property. Without that the form won't know where to send the data. So even if the data is meant to be posted to the current page, set the 'action' property.

      For example:
      [code=html]
      <form action="thisPag e.php" method="POST">
      [/code]
      Thanks, That worked.

      But now its nagging with this error for some reason:

      Warning: mysql_fetch_obj ect(): supplied argument is not a valid MySQL result resource in /home/content/C/o/l/Collussus/html/school/ict/search.php on line 213

      this is my php(i left the tags on <? ?> because i know my server allows it. :

      [PHP]<?
      ERROR_REPORTING (E_ALL);
      include("config .php");
      if(isset($_POST['button']))
      {
      $location = $_POST['locatie'];
      $price = $_POST['prijs'];

      //check what price it is
      if($locatie == ''){
      echo"Foute locatie gekozen, Kies aub een goede locatie.<br>";
      }

      if($price == ''){
      echo"Foute prijs gekozen. Kies aub een goede prijs.";
      }

      elseif($price == '1'){


      $query = "SELECT * FROM houses WHERE location = '" . $location . "' AND price >= 0 AND price <= 100000";


      }

      elseif($price == '2'){
      $query = "SELECT * FROM houses WHERE location = '" . $location . "' AND price >= 100000 AND price <= 200000";
      }

      elseif($price == '3'){
      $query = "SELECT * FROM houses WHERE location = '" . $location . "' AND price >= 200000 AND price <= 300000";
      }

      elseif($price == '4'){
      $query = "SELECT * FROM houses WHERE location = '" . $location . "' AND price >= 300000 AND price <= 400000";
      }

      elseif($price == '5'){
      $query = "SELECT * FROM houses WHERE location = '" . $location . "' AND price >= 400000 AND price <= 500000";
      }

      elseif($price == '6'){
      $query = "SELECT * FROM houses WHERE location = '" . $location . "' AND price >= 500000 AND price <= 600000";
      }

      elseif($price == '7'){
      $query = "SELECT * FROM houses WHERE location = '" . $location . "' AND price <= 600000";
      }

      $results = mysql_query($qu ery);
      while($results = mysql_fetch_obj ect($results))
      {

      echo "<table width='434' height='72' border='0'>";

      echo "<tr><td width='133' height= '130'>";

      echo "<img width='132' height='129' src='" . $results['Foto1'] . "'></img>";

      echo "</td><td class='style5'> ";

      echo "Type: " . $results['type'] . "<br>Descriptie :" . $results['description'];

      echo "</td><td class='style5'> ";

      echo "prijs: " . $results['price'] . ".-";

      echo "</td></tr>";

      echo "</table>";

      }
      }
      ?>[/PHP]

      Comment

      • Atli
        Recognized Expert Expert
        • Nov 2006
        • 5062

        #4
        These two lines are the source of your problem:
        [code=php]
        $results = mysql_query($qu ery);
        while($results = mysql_fetch_obj ect($results))
        [/code]
        You are overwriting the database result resource with the return value of the mysql_fetch_obj ect() function. So on the next loop, the $results is not a valid MySQL resource, causing the function to fail and a warning to be printed.

        You need to change the name of the variable you use to store the current row of data.

        Comment

        • Dave Siegel
          New Member
          • Feb 2007
          • 42

          #5
          Originally posted by Atli
          These two lines are the source of your problem:
          [code=php]
          $results = mysql_query($qu ery);
          while($results = mysql_fetch_obj ect($results))
          [/code]
          You are overwriting the database result resource with the return value of the mysql_fetch_obj ect() function. So on the next loop, the $results is not a valid MySQL resource, causing the function to fail and a warning to be printed.

          You need to change the name of the variable you use to store the current row of data.

          Fixed that, still same problem.

          Comment

          • Atli
            Recognized Expert Expert
            • Nov 2006
            • 5062

            #6
            Have you check out if there was an error in your SQL?
            Your code does not validate that the query executed successfully.

            Try adding something like this before you try to use the result from the database:
            [code=php]
            if(!$result) {
            echo "<pre><b>My SQL Query failed!</b>\n
            <b>Error:</b> ". mysql_error() ."\n
            <b>Query:</b> $query
            </pre>";
            }
            [/code]

            Comment

            • Dave Siegel
              New Member
              • Feb 2007
              • 42

              #7
              Originally posted by Atli
              Have you check out if there was an error in your SQL?
              Your code does not validate that the query executed successfully.

              Try adding something like this before you try to use the result from the database:
              [code=php]
              if(!$result) {
              echo "<pre><b>My SQL Query failed!</b>\n
              <b>Error:</b> ". mysql_error() ."\n
              <b>Query:</b> $query
              </pre>";
              }
              [/code]
              Fixed the first error.

              Now it just gives me this error:

              MySQL Query failed!

              Error:

              Query: SELECT * FROM houses WHERE region = 'Parijs' AND price >= 0 AND price <= 100000

              Comment

              • Atli
                Recognized Expert Expert
                • Nov 2006
                • 5062

                #8
                Note that the variable I used in my example was called $result, while the MySQL result in your code is called $results. (Force of habit for me. I always call mine $result :P)

                Comment

                • Dave Siegel
                  New Member
                  • Feb 2007
                  • 42

                  #9
                  That fixed it. Thanks.

                  Comment

                  Working...