General Programming Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
User Name:
Password:
Remember me
 
Go Back   Dev Articles Community ForumsProgrammingGeneral Programming Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Display Modes
 
Unread Dev Articles Community Forums Sponsor:
  #1  
Old January 23rd, 2003, 06:41 AM
steve55 steve55 is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Nov 2002
Location: UK
Posts: 44 steve55 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 7
Send a message via AIM to steve55 Send a message via Yahoo to steve55
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.

Reply With Quote
  #2  
Old January 23rd, 2003, 08:51 AM
clintw@work clintw@work is offline
Junior Member
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jan 2003
Posts: 8 clintw@work User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
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:
<?php echo $_POST['myVariable']; ?>

or
PHP Code:
<?php echo $HTTP_POST_VARS['myVariable']; ?>

Reply With Quote
  #3  
Old January 23rd, 2003, 09:52 AM
steve55 steve55 is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Nov 2002
Location: UK
Posts: 44 steve55 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 7
Send a message via AIM to steve55 Send a message via Yahoo to steve55
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.

Reply With Quote
  #4  
Old January 23rd, 2003, 10:24 AM
ramz ramz is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jan 2003
Location: Juiz de Fora - MG- Brazil
Posts: 93 ramz User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 6
Send a message via ICQ to ramz Send a message via MSN to ramz
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.

Reply With Quote
  #5  
Old January 23rd, 2003, 10:33 AM
clintw@work clintw@work is offline
Junior Member
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jan 2003
Posts: 8 clintw@work User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Oh, ok. Here it is then:

PHP Code:
<?php
$query_GetFields 
"SELECT DISTINCT areas FROM houses";
$GetFields mysql_query($query_GetFields) or die(mysql_error());
$row_GetFields mysql_fetch_assoc($GetFields);
?>

<body>
<form name="form" method="post" action="pageToPostTo.php">
  <p>
   <select name="field_1">
    <?php
do {  
?>
    <option value="<?php echo $row_GetFields['areas']?>"><?php echo $row_GetFields['areas']?></option>
    <?php
} while ($row_GetFields mysql_fetch_assoc($GetFields));
?>
  </select>
  </p>
  <p>
    <input type="submit" name="Submit" value="Submit" />
  </p>
</form>
<?php
mysql_free_result
($GetFields);
?>
</body>


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.

Reply With Quote
  #6  
Old January 23rd, 2003, 11:43 AM
steve55 steve55 is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Nov 2002
Location: UK
Posts: 44 steve55 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 7
Send a message via AIM to steve55 Send a message via Yahoo to steve55
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";
}

?>

Reply With Quote
  #7  
Old January 23rd, 2003, 12:52 PM
clintw@work clintw@work is offline
Junior Member
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jan 2003
Posts: 8 clintw@work User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
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:
switch($_POST['field_1']){
   case: 
'all';
   
$query "select * from houses";
   break;
   default:
   
$srch="%".$field_1."%"
   
$query "select * from houses WHERE areas LIKE '$srch' ";
   break;



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.

Reply With Quote
  #8  
Old January 23rd, 2003, 04:35 PM
steve55 steve55 is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Nov 2002
Location: UK
Posts: 44 steve55 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 7
Send a message via AIM to steve55 Send a message via Yahoo to steve55
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...

Reply With Quote
  #9  
Old January 23rd, 2003, 04:50 PM
clintw@work clintw@work is offline
Junior Member
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jan 2003
Posts: 8 clintw@work User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
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?

Reply With Quote
  #10  
Old January 24th, 2003, 03:54 AM
steve55 steve55 is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Nov 2002
Location: UK
Posts: 44 steve55 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 7
Send a message via AIM to steve55 Send a message via Yahoo to steve55
yes it does thank you - once again thanks for your help.

Reply With Quote
Reply

Viewing: Dev Articles Community ForumsProgrammingGeneral Programming Help > Passing a value to a results page...


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 6 hosted by Hostway
Stay green...Green IT