Page 1 of 1

Passing variable from dropdown menu

Posted: Thu Dec 03, 2009 10:11 pm
by TARCON
See http://www.homejotter.com/new.index.php.

I have two drop down boxes that are dynamic. I want to retrieve the value (college) the user selects and then I want to get the id of that college. Then when I press Search >> I want the list to populate properties within that college. I am not too experienced with PHP. I did make everything myself but with a lot of help with previous coding. I know how to do the coding on the new.results.php to check if a college was passed, but I am not quite sure to go about the coding for the new.index.php.

If you click search now, it shows all of the properties in the database, which I don't want. Any help would be great. Send links, or just do some of the coding! Haha. Literally, any help would be great. Just send me in the right direction.

Thanks in advance.

Re: PASSING VARIABLE FROM DROP DOWN MENU

Posted: Thu Dec 03, 2009 10:52 pm
by Weiry
For starters, just looking at your source code i can see 2 problems

Code: Select all

<a href="new.results.php?college="""><input name="search2" type="submit" class="search" id="search2" value="Search »"/></a>
There shouldn't be an <a> tag around an input button. Especially if your passing through POST which you are. Not to mention the ( college=""" ) part of the <a> tag.

To check your actually getting data across pages, print out the contents of the $_POST data on your results page.

Code: Select all

print_r($_POST);
If you are getting data across, then it will appear here, if not, then your not passing it properly or are looking for the wrong variables on the results page.
Then again, i look at every link on your results page and they all assume your using a $_GET method for retrieving variables...

You really need to decide which form of variable passing your going to use.

Also, really if no variables are being passed, then your SQL statements probably shouldn't work either.

And without any code posted of how anything you have works, it makes it impossible to tell you where there are problems.



On another note, you have problems displaying prices. " $$1400.00 " for instance.
Also i see at least one place has "1 1/2" Baths?
Availability " 6-01-2010 0r 9=01-2010 "... 0r 9=01-2010 ?
Availability "1/1/10"
Deposit "$1 Month's Rent"

As you can probably see, there is a lot of data inconsistency too.

Re: PASSING VARIABLE FROM DROP DOWN MENU

Posted: Fri Dec 04, 2009 9:33 am
by TARCON
" " $$1400.00 " for instance.
Also i see at least one place has "1 1/2" Baths?
Availability " 6-01-2010 0r 9=01-2010 "... 0r 9=01-2010 ?
Availability "1/1/10"
Deposit "$1 Month's Rent" "

Hello. Yes I know there is an inconsistency here. I am definetely working on it. 1 1/2 baths is correct though. Meaning the apartment/house has a full bath with a bathtub/shower and the other bathroom is 1/2 because there is no bathtub/shower. It is somewhat common to say 1.5 baths or 2.5 baths, etc...

I am working on the prices for sure though. I am changing the values in the database to get rid of the dollar sign. Because I want to filter by price, as you could see. And, for some reason, the filtering is not working for the prices. And to retrieve a consistency, I am removing all of the dollar signs.

I can supply you any code you need because this is highly important for me to get done. This is a website on the side that I created and I just want it to work. My main priority is retrieving that pesky ID number from a college when it is selected and then pass it to the results page.

Thanks for taking time to help me.

Re: PASSING VARIABLE FROM DROP DOWN MENU

Posted: Fri Dec 04, 2009 9:00 pm
by Weiry
For starters, we would need to see the code relating to the retrieval of data from your form. This would be your $_POST or $_GET. And from the way in which you seem to be passing variables on your result page, you would be better off changing your form method from POST to GET, so that the information will be displayed on the URL address as is the case with all your links on the result page. This would be more consistent when retrieving data rather than having to respecify the college and state each time.
This would however mean that some changes to the links for rent/bedrooms will need to be modified to reflect the college and state.

Code: Select all

<a href="new.results.php?<?php print "state={$_GET['state']}&college={$_GET['college']}&beds=3"><input name="bed3" type="submit" class="filter" id="bed3" value="3 »"/></a>
Secondly, you would need to show the SQL statements / PHP code around your data retrieval or filtering.

Code: Select all

// As an example:
$sql = "SELECT * FROM `accommodation` WHERE `state` = '{$_GET['state']}' AND `college` = '{$_GET['college']}'";
if(!empty($_GET['rent'])){
    $sql .= " AND `rent` <= '{$_GET['rent']}'";
}
// etc etc.

Re: PASSING VARIABLE FROM DROP DOWN MENU

Posted: Fri Dec 04, 2009 10:08 pm
by Luke
Just popping in to remind you that you do not need to post your thread titles in all caps. In fact, I believe it's against our rules. I've edited your post title, but in the future, just try to remember not to do that.

Re: Passing variable from dropdown menu

Posted: Sun Dec 06, 2009 5:26 pm
by TARCON
I don't have any code to retrieve data from the form in http://www.HOMEJOTTER.com/new.index.php. That is mainly what my question is about. How do you retrieve that data? Say, if someone clicks on the state then the college, how do you get the college from the dropdown menu. You are right that I want it $_GET to show up in the URL; this will be the best way.

I know how to do the coding for the results page once a variable is passed and I know you would have to put "&college= " in the URL. But I am just wondering how to get the retrieval of the college name from that drop down menu.

And here is the code in its entirety for my index.php page:

Code: Select all

<?php
 
    INCLUDE "lib.php";
    
    $dbConn = connectToDb();
    
    login();
 
    
    print <<<HERE
    
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>HOME JOTTER</title>
 
<link href="new.stylesheet.css" media="all" rel="stylesheet" type="text/css" />
 
<script type="text/javascript">
var varieties=[
               
HERE;
 
$stateIndex = 0;
$college1Index = 0;
$college2Index = 0;
$college3Index = 0;
 
$count = 0;
 
$state1 = getStateCollege($colleges[0]);
 
foreach($states as $st)
{
    if ($st == $state1)
    {
        $stateIndex = $count;
        
    }
    
    $output = "[";
                 
    $collegeList = getColleges($st);
    
    $count1 = 0;
    
    foreach ($collegeList as $college)
    {
        if ($college == $college1)
        {
            $college1Index = $count1 - 1;
            
        }
        
        $output .= "'" . $college . "', ";
        
        $count1++;
        
    }
    
    $output = rtrim($output, ", ");
            
    $output .= "],\n";
    
    print $output;  
    
    $count++;
    
}
 
print <<<HERE
 
];
 
function Colleges(idx) 
{
    var f=document.form1;
    f.college1.options.length=null;
    
    f.college1.options.length=varieties[idx].length - 1;
 
    for(i=1; i<varieties[idx].length; i++) 
    {
        f.college1.options[i-1]=new Option(varieties[idx][i], i);
        
    }
    
    f.college1.options[$college1Index].selected = "0";
    
}
 
onload=function() {Colleges($stateIndex);};
 
</script>
 
</head>
 
<body class="oneColElsCtrHdr">
  <div id="header">
    <table width="100%" border="0" cellspacing="0" cellpadding="0">
      <tr>
        <td width="50%"><div align="left">
          <a href="new.index.php"><span class="normalUnderline">Home</span></a>&nbsp;&nbsp;&nbsp;<a href="news.php">News</a>&nbsp;&nbsp;&nbsp;<a href="new.about.html">About</a>&nbsp;&nbsp;&nbsp;<a href="students.php">Students</a>&nbsp;&nbsp;&nbsp;<a href="new.signin.php">Property Owners</a>
        </div></td>
        <td width="50%"><div align="right"><a href="new.signin.php">Sign In</a>&nbsp;&nbsp;&nbsp;<a href="new.contact.html">Contact</a>&nbsp;&nbsp;&nbsp;<a href="new.help.html">Help</a></div></td>
      </tr>
    </table>
  <!-- end #header --></div>
  <div id="container">
  
HERE;
 
      print <<<HERE
 
      
    <div id="mainContent"> 
      <div align="center"><br />
        <br />
        <br>
        <img src="images/logo_big.png" width="372" height="59" /><br />
        <br />
        <br />
        <br />
      </div>
      <form id="form1" name="form1" method="get" action="" enctype="multipart/form-data">
      <table width="400" border="0" align="center" cellspacing="15" class="tableSearch">
 
      <tr>
        <td width="162"><div align="left"><select name="state1" id="state1" tabindex="1" class="textfield" onchange="Colleges(this.selectedIndex)">
              
HERE;
 
    foreach($states as $st)
    {
        print "<option";
        
        if ($st == $state1)
        {
            print " selected='selected'";
            
        }
        
        print ">$st</option>/n";
        
    }
 
    print <<<HERE
    
            </select></div>
            
            </td>
            
        <td width="402"><div align="left"><select name="college" class="textfield" id="college" value="" tabindex="2"></select></div></td>
        <td width="520"><div align="right"><input name="search2" type="submit" class="search" id="search2" value="Search »"/></div></td>
      </tr>
    </table>
    </form>
      <br />
      <br /><br />     
      <div id="numberOfListings"><span class="red">65,810</span> listings</div>
      <div id="numberOfRealtors"><span class="red">3,291</span> property owners</div>
      <div id="numberOfColleges"><span class="red">1,941</span> colleges
        <!-- end #mainContent --></div>
 
HERE;
 
 
?>
 
    <div id="footer">
      <div align="center">© 2009 - <a href="new.terms.html">Terms</a>
      </div>
    <!-- end #footer --></div>
<!-- end #container --></div>
</body>
</html>
 
Anyways, here is my code for the results.php page:

Code: Select all

if(isset($_GET["beds"]))
        $beds = $_GET["beds"];
    else
        $beds = "";
        
    if(isset($_GET["rent"]))
        $rent = $_GET["rent"];
    else
        $rent = "";
    
    if($beds != null && $beds != 5)
    {
        $result = mysql_query("SELECT * FROM listing WHERE beds =\"" . $beds . "\";");
    }
    else if($beds != null && $beds = 5)
    {
        $result = mysql_query("SELECT * FROM listing WHERE beds >= 5");
    }
    else if($rent == 1)
    {
        $result = mysql_query("SELECT * FROM listing WHERE rent <= 300 ORDER BY  `listing`.`rent` ASC ");
    }
    else if($rent == 300)
    {
        $result = mysql_query("SELECT * FROM listing WHERE rent >= 300 AND rent <= 500 ORDER BY  `listing`.`rent` ASC ");
    }
    else if($rent == 500)
    {
        $result = mysql_query("SELECT * FROM listing WHERE rent >= 500 AND rent <= 700 ORDER BY  `listing`.`rent` ASC ");
    }
    else if($rent == 700)
    {
        $result = mysql_query("SELECT * FROM listing WHERE rent >= 700 AND rent <= 900 ORDER BY  `listing`.`rent` ASC ");
    }
    else if($rent == 900)
    {
        $result = mysql_query("SELECT * FROM listing WHERE rent >= 900 ORDER BY  `listing`.`rent` ASC ");
    }
    else
        $result = mysql_query("SELECT * FROM listing");

Re: Passing variable from dropdown menu

Posted: Mon Dec 07, 2009 11:38 am
by TARCON
UPDATE:

I changed the form to get and I did a few other things. I got the state value in the URL but I keep on getting the college value to be the number of the college in the list (drop down box). So Alabama A&M University's value would be 1 and the next one would be 2 and so on....
How would I get the actually name of the college as the value?

If I get the name as the value I think I can pretty much do it from there...

Thanks,

Andrew

Re: Passing variable from dropdown menu

Posted: Fri Dec 11, 2009 1:22 pm
by TARCON
?

Re: Passing variable from dropdown menu

Posted: Fri Dec 11, 2009 11:27 pm
by McInfo
Solution A: Include an identification number from the database for each college in the JavaScript. Use that number as the option value. When processing the submitted form, query the database for the college with that number.

Solution B: Use the college name as the option value. It should be URL-encoded.

Solution C: Work with the incremented option value that you have now. When processing the submitted form, query the database to get a list of colleges for the selected state. Append a LIMIT clause to the query so the submitted option value acts as an offset into the list. The college names must be sorted alphabetically, both in the form and in the query.

Solutions A and B are better because a user can return to the results page via a bookmark and see the same college even if you have added or removed colleges for the associated state. Solution A is best because you can also rename colleges without invalidating bookmarks.
TARCON wrote:How would I get the actually name of the college as the value?
Find the part of the JavaScript that changes the list of options and learn what it is doing. (Hint: Option)

Edit: This post was recovered from search engine cache.

Re: Passing variable from dropdown menu

Posted: Sat Dec 26, 2009 12:37 pm
by TARCON
I am still not getting it and I really don't know anything about Javascript so it is hard to figure out what is making the value...the value.

What I want the value to be in the collegeID, now.

I know how to get the college ID from the college, which is: $collegeID = getCollege($college, $st);

And this code deals with the value (I think):
for(i=1; i<varieties[idx].length; i++)
{
f.college.options[i-1]=new Option(varieties[idx], i);
}

f.college.options[$collegeIndex].selected = "0";


Now I just want to make the value of the selected college, the college ID. Please help!

UPDATE: I include: f.college.options[i-1].value = $collegeID; within the for loop. That changes the ID but not the correct ID. I know that my code for the college ID variable is correct, but then it is giving me the incorrect ID.


Thanks,

Andrew

Re: Passing variable from dropdown menu

Posted: Tue Dec 29, 2009 7:25 pm
by McInfo
I'm feeling charitable. The attached code is not a complete solution -- more like a starter kit. I hope it explains some things.

Edit: This post was recovered from search engine cache.

Re: Passing variable from dropdown menu

Posted: Sat Jan 09, 2010 12:31 pm
by TARCON
I got it using:

function disp_text()
{
var f = document.form1.college;
var w = f.selectedIndex;
var st = f.options[w].text;
f.value = st;
}

And I just put the onchange function as the disp_text.

Thanks for everyone's help!