Page 1 of 2 12 LastLast
Results 1 to 10 of 12

Thread: $_POST in PHP, need help with the code

  1. #1
    Senior Member
    Join Date
    Apr 2004
    Posts
    228

    $_POST in PHP, need help with the code

    Could you help me with the next code:

    <?php

    require ("Functions.php");

    #geting the destinations
    $link = Connect();

    $destinations_query = "SELECT * FROM destinations
    WHERE Held_In_Destination > 1";

    $destinations_result = mysqli_query($link, $destinations_query);

    #creating a form containing a destination list.
    #The form will pass on a destination ID to the next form
    #that shows hotels in the area.

    echo "<form action='MakeAnOrder.php' method='POST'>

    <strong>Select Your Destination</strong>
    <select name='Destination_Name'>\n";

    while ($destinations_row = mysqli_fetch_array($destinations_result))
    {

    extract ($destinations_row);
    echo"<option value = '$Destination_Name'>$Destination_Name\n";

    }


    echo"</select>\n";
    echo "<input type = 'submit' value = 'Select'>
    </form>\n";


    #Selecting a hotel

    $hotels = Hotels(isset($_POST['$Destination_ID']));

    echo "<form action='MakeAnOrder.php' method='POST' >

    <strong>Select a Hotel</strong>
    <select name='Hotel_Name'>\n";

    while ($hotels_row = mysqli_fetch_array($hotels))
    {

    extract ($hotels_row);
    echo"<option value = '$Hotel_Name'>$Hotel_Name\n";

    }

    echo"</select>\n";
    echo "<input type = 'submit' value = 'Select'>
    </form>\n";

    ?>



    I am trying to influence the output of the second form by the users choice in the first one. Both are based in the same page.
    Don\'t post if you\'ve got nothing constructive to say. Flooding is annoying

  2. #2
    Just Another Geek
    Join Date
    Jul 2002
    Location
    Rotterdam, Netherlands
    Posts
    3,401
    What do you mean by "influencing" the output? SQL-injection?
    Oliver's Law:
    Experience is something you don't get until just after you need it.

  3. #3
    Senior Member
    Join Date
    Apr 2004
    Posts
    228
    I want a user to chose a name of the destination. The program the should use an ID attached to this name to find the hotels which are based in that destination and output them in to a second dropdown list.
    Don\'t post if you\'ve got nothing constructive to say. Flooding is annoying

  4. #4
    Just Another Geek
    Join Date
    Jul 2002
    Location
    Rotterdam, Netherlands
    Posts
    3,401
    You probably need to change this part:
    Code:
    while ($destinations_row = mysqli_fetch_array($destinations_result))
    {
    
    extract ($destinations_row);
    echo"<option value = '$Destination_Name'>$Destination_Name\n";
    
    }
    Not sure what the extract() function does but I'm guessing you need to change the echo on the next line to:
    Code:
    echo "<option value='$Destination_ID'>$Destination_Name</option>\n";
    Both selects seem incomplete btw...

    It should be something like:
    Code:
    <select id=myselection>
      <option value=1>Option 1</option>
      <option value=2>Option 2</option>
    </select>
    Oliver's Law:
    Experience is something you don't get until just after you need it.

  5. #5
    Senior Member
    Join Date
    Apr 2004
    Posts
    228
    Didn't work
    Don\'t post if you\'ve got nothing constructive to say. Flooding is annoying

  6. #6
    Just Another Geek
    Join Date
    Jul 2002
    Location
    Rotterdam, Netherlands
    Posts
    3,401
    oh.... This doesn't look right either:

    Code:
    $hotels = Hotels(isset($_POST['$Destination_ID']));
    Im guessing this should be:
    Code:
    $hotels = Hotels(isset($_POST['Destination_Name']));
    This may help:
    http://www.w3schools.com/php/php_forms.asp
    Oliver's Law:
    Experience is something you don't get until just after you need it.

  7. #7
    Senior Member
    Join Date
    Apr 2004
    Posts
    228
    OK I made some changes to the code.

    &lt;?php

    require ("Functions.php");

    #geting the destinations
    $link = Connect();

    $destinations_query = "SELECT * FROM destinations
    WHERE Held_In_Destination &gt; 1";

    $destinations_result = mysqli_query($link, $destinations_query);

    #creating a form containing a destination list.
    #The form will pass on a destination ID to the next form
    #that shows hotels in the area.

    echo "&lt;form action='MakeAnOrder.php' method='POST'&gt;

    &lt;strong&gt;Select Your Destination&lt;/strong&gt;
    &lt;select name='Destination_Name'&gt;\n";

    while ($destinations_row = mysqli_fetch_array($destinations_result))
    {

    extract ($destinations_row);
    echo"&lt;option value = '$Destination_Name'&gt;$Destination_Name\n";

    }


    echo"&lt;/select&gt;\n";
    echo"&lt;input type = 'hidden' name = 'Destination_ID' value = '$Destination_ID'&gt;";
    echo "&lt;input type = 'submit' value = 'Select'&gt;
    &lt;/form&gt;\n";

    mysqli_close ($link);
    ?&gt;


    &lt;?php
    #Selecting a hotel
    $Destination_ID = SelectDestinationID(isset($_POST['Destination_Name']));

    echo"$Destination_ID";

    $hotels = Hotels('$Destination_ID');

    echo "&lt;form action='MakeAnOrder.php' method='POST' &gt;

    &lt;strong&gt;Select a Hotel&lt;/strong&gt;
    &lt;select name='Hotel_Name'&gt;\n";

    while ($hotels_row = mysqli_fetch_array($hotels))
    {

    extract ($hotels_row);
    echo"&lt;option value = '$Hotel_Name'&gt;$Hotel_Name\n";

    }

    echo"&lt;/select&gt;\n";
    echo "&lt;input type = 'submit' value = 'Select'&gt;
    &lt;/form&gt;\n";
    #When both forms are created, the Destination_ID doesn't transfere to the second form
    ?&gt;

    When I echo a Destination_ID in the second part of the code, it turns out to be fine. But the form still doesn't display the hotel names
    Don\'t post if you\'ve got nothing constructive to say. Flooding is annoying

  8. #8
    Senior Member
    Join Date
    Apr 2004
    Posts
    228
    Here's the code for the Hotels Funsction

    function Hotels($Destination_ID)
    {
    #Connect to the database
    $link = Connect();

    $query = "SELECT * FROM hotels
    WHERE Destination_ID = $Destination_ID";

    $result = mysqli_query ($link,$query);

    return(object) $result;
    }
    Don\'t post if you\'ve got nothing constructive to say. Flooding is annoying

  9. #9
    Senior Member
    Join Date
    Jul 2004
    Posts
    469

    Re: $_POST in PHP, need help with the code

    Originally posted here by nightcat

    $hotels = Hotels(isset($_POST['$Destination_ID']));
    Is the function Hotels() defined somewhere? I'm guessing its in your functions.php include, but without that we can't really tell what is going on. You should probably also parse the destination for the currently selected desitnation and then add a "selected" tag to that line.

    Also, why are you ?&gt;ing your php and then reopening immediately. I would suggest just leaving the tag open.

    Edit: hah, I guess I should have refreshed. I was editing this for about a half hour while someone was talking to me, and looks like you answered my question in the mean time. Lemme look at it and see whats going on.

  10. #10
    Senior Member
    Join Date
    Jul 2004
    Posts
    469
    My first suggestion is, if you're not going to functionize the first sql query, why do the second one? Put all of the code into the base .php file and get it working, then you can worry about functionizing it. Also, you only need 1 $link. When you do your functions, you can pass $link to it.

Posting Permissions

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