SitePoint Sponsor

User Tag List

Results 1 to 11 of 11
  1. #1
    SitePoint Addict
    Join Date
    Nov 2007
    Posts
    219
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Angry Passing variables from page to page

    I made this form in the "index page"
    <form action="search_results.php" method="get" name="search">
    <label for="days">
    Choose days:
    <select name="days" iid="days">
    <option value="7">+/-7 days</option>
    <option value="14">+/-14 days</option>
    <option value="21">+/-21 days</option>
    </select>
    <input name="search" type="submit" value="search">
    </form>
    and when i did : echo $ days it in the "search_results.php" page ,it was ok

    Now i have made another form in the "search_results.php"
    echo "<form action='calculate.php' method='post'>";
    while($record=mysqli_fetch_assoc($result)){
    $days_db=$record['days'];
    echo "<input type='checkbox' name='choose[]' value='{$days_db}' />";
    echo "<input type='submit' name='submit' value='Continue'>";
    echo "</form>";
    and in the "continute.php"
    i calculated the sum of the checkboxes selection
    but my problem now is:
    i want to compare between the $days in the "index.php " and the $sum of the checkboxs selection
    but each time i try to echo $days in the "contniue.php" it deosnt seem to see it
    why???

  2. #2
    SitePoint Wizard silver trophybronze trophy Cups's Avatar
    Join Date
    Oct 2006
    Location
    France, deep rural.
    Posts
    6,869
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    Using HTML you can only submit one form on a page, its up to the user to submit the correct form.

    I cant see any reason for doing that, so why not put all the elements into one form and try it.

  3. #3
    SitePoint Addict
    Join Date
    Nov 2007
    Posts
    219
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    thnxxx it solved
    by doing this
    echo "<input type='hidden' name='days' value='$days' />";

  4. #4
    SitePoint Addict
    Join Date
    Nov 2007
    Posts
    219
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    but please i have another problem in mysql query
    $query="select tourname,description ,days from tours where
    days >='".$days."'";
    my select is
    <select name="days" iid="days">
    <option value="7">+/-7 days</option>
    <option value="14">+/-14 days</option>
    <option value="21">+/-21 days</option>
    </select>
    i have records conatins :5,10,15 days
    every time i choose +/-7 days which has the values 7
    it gives me 0
    and when i select +/14 which has the value 14
    its gives me the days 5,15
    and when i select +/-21
    its gives me the days 5
    why????????
    i think mysql query contains something wrong ???

  5. #5
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,702
    Mentioned
    101 Post(s)
    Tagged
    4 Thread(s)
    Have PHP echo the $query to help you work out what's going wrong

  6. #6
    SitePoint Addict
    Join Date
    Nov 2007
    Posts
    219
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    i did so but i found out that i was using varchar instead of int in the days colum in the database
    thnxxxx for trying to help me

  7. #7
    Theoretical Physics Student bronze trophy Jake Arkinstall's Avatar
    Join Date
    May 2006
    Location
    Lancaster University, UK
    Posts
    7,062
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Oh, and don't wrap integers in quotes when in a mysql_query (or rudy'll hunt you down! lol). Also, it's good practice to put sql commands in capitals.

    PHP Code:
    $query="SELECT tourname,description,days FROM tours WHERE 
    days >= "
    .$days
    Jake Arkinstall
    "Sometimes you don't need to reinvent the wheel;
    Sometimes its enough to make that wheel more rounded"-Molona

  8. #8
    SitePoint Zealot
    Join Date
    Dec 2007
    Location
    Mackay, QLD, Australia
    Posts
    158
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Another thing you might want to consider is to enclose all your table names and field names in identifier quote characters (or simply put, a backtick '`'). This helps MySQL differentiate between reserved words, and table names or field names, so that there aren't any conflicts. It also helps for compatibility, it's been my experience that some servers don't execute your queries if you don't quote your identifiers. So your query will look like this.

    PHP Code:
    $query "SELECT `tourname`,`description`,`days` FROM `tours` WHERE `days` >= {$days}"
    Also, see how I inserted the variable in the string there. With PHP, if you enclose your strings in a double quote, then PHP will go through your string and search for any variables that need to be inserted. PHP doesn't do this with single quote strings.

  9. #9
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,702
    Mentioned
    101 Post(s)
    Tagged
    4 Thread(s)
    Quote Originally Posted by Lamiunto View Post
    PHP Code:
    $query "SELECT `tourname`,`description`,`days` FROM `tours` WHERE `days` >= {$days}"
    Also, see how I inserted the variable in the string there. With PHP, if you enclose your strings in a double quote, then PHP will go through your string and search for any variables that need to be inserted. PHP doesn't do this with single quote strings.
    It might pay to notice that there is quite a performance hit when using variable insertion. This is of no real problem now, but it's one to keep in mind in the future.

    If you use an editor that uses syntax highlighting, it can also be more difficult to find those variables when they're embedded inside the string.

    And as a final tip, you don't need the curly braces when it's a plain variable being inserted. They're really only required when arrays or objects are being used. They won't cause any harm though, and some find that the curly braces makes the variables easier to find.

    So you can also use

    PHP Code:
    $query "SELECT `tourname`,`description`,`days` FROM `tours` WHERE `days` >= $days"
    or you can use

    PHP Code:
    $query 'SELECT `tourname`,`description`,`days` FROM `tours` WHERE `days` >= ' $days
    Which ever one floats your boat really.

  10. #10
    Theoretical Physics Student bronze trophy Jake Arkinstall's Avatar
    Join Date
    May 2006
    Location
    Lancaster University, UK
    Posts
    7,062
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Yup, I agree completely.

    General practice is to use concatenation (gluing) to insert variables, as it saves on processing time.

    And if you have alot of variables in a certain string, printf() and sprintf() are very handy functions to know.
    Jake Arkinstall
    "Sometimes you don't need to reinvent the wheel;
    Sometimes its enough to make that wheel more rounded"-Molona

  11. #11
    SitePoint Addict
    Join Date
    Nov 2007
    Posts
    219
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    tnxxxx guys u helped me alot
    i had so much troubles with mysql queries and how to concatenate the variables


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
  •