|
|
|||||||||
|
|||||||||
|
|||||||||
| |
|||
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Display Modes |
|
#1
|
|||
|
|||
|
Passing a value to a results page...
I have the following code that populates a drop down menu with data from a mysql database.
I would like to include it in a form and pass the selected value to a results page but am unsure how. Here is my code:- <?php $host = 'localhost'; $user = 'x'; $pass = 'x'; $db = 'x'; $table = 'houses'; $link = mysql_connect($host,$user,$pass); if(!$link) die(mysql_error()); $select = mysql_select_db($db); if(!$select) die(mysql_error()); $query = "SELECT DISTINCT areas FROM houses ORDER BY areas"; $result = mysql_query($query); if(!$result) die(mysql_error()); echo "<select name=\"areas\">\n"; echo "<option value=\"\">Choose</option>\n"; while($row = mysql_fetch_array($result)) { // added this if ($choose == $row['areas']) $selected = ' selected'; else $selected = ''; echo "<option value='".$row['areas']."'$selected>".$row['areas']."</option>\n"; } echo "</select>\n"; ?> Thanks. |
|
#2
|
|||
|
|||
|
Why do you have to provide a subject to answer a subject?
Where is your form stuff? Man, your learning php before you know html. I was afraid this would happen to todays kids. They have use of wysiwyg editors to do the stuff we old guys had to learn. Now they can drive a car but, they don't know where the gas goes.
Sorry, I'm just venting. Nothing personal, and forgive me if you left it out on purpose. Anyways..Use: Code:
<FORM action="postToPage.php" method="POST"> <INPUT type="HIDDEN" name="myVariable" value="myValue"> </FORM> to inclose form elements like selects. And don't forget the submit button too: Code:
<INPUT type="SUBMIT" value="Continue"> And then on the PHP page the action points to you catch the variables passed to it in two ways: PHP Code:
or PHP Code:
|
|
#3
|
|||
|
|||
|
Thank you , but I am quite proficient using html....
I am however new to php/mysql and how html 'fits into such scripts' as the one I have posted. Having played with said script I have been unable to integrate a form command into the php code and get it to pass a value to a results page, which would display all possible results based on the users selection from the drop down menu. (ideally I would like to display three drop downs, with different fields from the database in each). - Hence I posted this thread, for which I am still awaiting a suitable reply. |
|
#4
|
|||
|
|||
|
Use something like this
I think that you can use a function that I developed...
function dropDown($table, $id, $text, $selected = "") { if(($table) && ($id) && ($text)) { $db = new db; // I'm using a class for connecting to MySQL... $db->sql = "SELECT $id AS idField, $text AS textField FROM $table ORDER BY $text"; $rs = $db->query($db->sql); if($rs) { for($i = 0; $i < sizeof($rs); $i++) { $idField = $rs[$i]['idField']; $textField = $rs[$i]['textField']; if($idField == $selected) { print "<option value='$idField' selected>$textField</option>"; } else { print "<option value='$idField'>$textField</option>"; } } } else { print "<option value=''>[ -- Empty -- ]</option>"; } } else { print "<option value=''>[ -- Empty -- ]</option>"; } unset($db,$rs,$idField,$textField,$i); } // End: dropDown To use this function is very easy. In your form put this: <select name="var_to_make_selected"> <?php dropDown("my_table","table_id_field","field_to_show_as_label",$var_to_make_selected); ?> </select> Then if the user submit the form and need to correct something, the value will be selected. If you want the database class that I'm using, just request and I'll send it to you... With this db class that I created, you can split the results into many pages very easily (to say the truth with 6 lines of code...) I think that this will help you... post a new message with more instructions...
__________________
Regards, Ramiro Varandas Jr. |
|
#5
|
|||
|
|||
|
Oh, ok. Here it is then:
PHP Code:
And so now the next page would be passed the variable 'field_1' which you get it's value with $_POST['field_1']; Of course this isn't a copy paste solution. Configure it for your needs. Last edited by clintw@work : January 23rd, 2003 at 10:37 AM. |
|
#6
|
|||
|
|||
|
Thanks it works! Much appreciated.
One last thought If I wanted to give the user the option of selecting all areas, I guess I could add a 'choose one' select option at the start of the drop down like so, <option>choose one</option> <option>area 1</option> <option>area 2</option> <option>area 3</option> Could I then pass a value through to my results page to show 'all areas' on the results page? And finally, can I repeat the code/send multiple variables to my results page so I can have 2 or 3 drop downs on my page (one for areas, one for towns and one for zips) ? For anyone that may liket o know my current results page is like so:- <? include "config.php"; if ($field_1) // perform search only if a string was entered. { $db = mysql_connect ($Host, $User, $Password); mysql_select_db ($DBName) or die ("Cannot connect to database"); $srch="%".$field_1."%"; $query = "select * from houses WHERE areas LIKE '$field_1' "; $result = mysql_db_query("databasename in here", $query); if ($result) { echo "Here are the results:<br><br>"; echo "<TABLE border = '1' cellspacing = '0' cellpadding ='0' width = '90%' align='center'><tr> <td align=center bgcolor=#cccc66><font size=\"2\" face=\"Verdana, Arial, Helvetica, sans-serif\">URL</font></td> <td align=center bgcolor=#cccc66><font size=\"2\" face=\"Verdana, Arial, Helvetica, sans-serif\">Area</font></td> <td align=center bgcolor=#cccc66><font size=\"2\" face=\"Verdana, Arial, Helvetica, sans-serif\">Town</font></td> <td align=center bgcolor=#cccc66><font size=\"2\" face=\"Verdana, Arial, Helvetica, sans-serif\">Post Code</font></td> </tr>"; while ($r = mysql_fetch_array($result)) { // Begin while $areas = $r["areas"]; $towns = $r["towns"]; $zip = $r["zip"]; $id = $r["id"]; echo "<tr> <td><font size=\"2\" face=\"Verdana, Arial, Helvetica, sans-serif\"><A HREF=\"view2c.php?id=$id\">View Property</A></font></td> <td><font size=\"2\" face=\"Verdana, Arial, Helvetica, sans-serif\">$areas</font></td> <td><font size=\"2\" face=\"Verdana, Arial, Helvetica, sans-serif\">$towns</font></td> <td><font size=\"2\" face=\"Verdana, Arial, Helvetica, sans-serif\">$zip</font></td></tr> "; } // end while echo "</table>"; } else { echo "problems...."; } } else { echo "Search string is empty. <br> Go back and type a string to search"; } ?> |
|
#7
|
|||
|
|||
|
The extended questions are little unclear to me but here it goes:
give the user the option of selecting all areas - You could make an option like <option>all areas</option> at the top or bottom with the value equal to 'all' or something and your result code could condition between two different selects. PHP Code:
And are you talking about sending multiple values to the page so the visitor can search houses in areas through either area keywords, the town, or zip code? Your losing me on this one. But if its what I think, you would again use switch statments. |
|
#8
|
|||
|
|||
|
Almost - basically I was thinking of having a dropdown menu for areas, then another for towns and another for zip, so therefore I would be passing three selected values to a results page instead of one, and wondered how I would do this...
|
|
#9
|
|||
|
|||
|
So you would do more <select>'s with each having a different name="". Afterall, the value you give in name="value" is the variable in the next page, i.e. field_1, field_2, field_3. And you can pass as many variable=value pairs as you want from a form, at least all the name/value pairs within that <form></form> that is submitted.
Does this answer the question? |
|
#10
|
|||
|
|||
|
yes it does thank you - once again thanks for your help.
|
![]() |
| Viewing: Dev Articles Community Forums > Programming > General Programming Help > Passing a value to a results page... |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|