Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

forms and php and my head hurting

Status
Not open for further replies.
Apr 4, 2001
34
0
0
GB
Hi
OK I want to get three list menu/droplist menus to work with each other and report all the information out to a text file or to something.
Here is the setup
An easy example of my database:- I have the droplist which is car manufacturers (ford, VW, etc...) The user clicks on one of these say ford. This fills a second droplist with all the ford cars, the user chooses escort. This opens a third droplist with steering wheel, gearstick etc.. They click on Steering wheel and the information is sent to a text file. I mean all the information so it would be FORD, ESCORT, STEERING WHEEL, not just STEERING WHEEL.

At the moment I have got the first droplist working it picks the data from the database and displays it.
1)--What do I do so that when a user clicks on ford it selects all the ford cars from the database and displays them in the next droplist.

2)-- how once the user has selected the correct data do I send it to a textfile or to a checkout basket which includes all the information not just STEERING WHEEL


Please help I'm stuck I have managed to work out how to get the data to the first droplist but I really need help with it.
 
PHP runs on the server. Therefore the page has to be submitted to fetch any additional data. That way you can build the drop downs easily.
If you want to use cleint side scripting you'd have to have all manufacutrers, all models, all parts already in arrays in the page - bad idea.

SO, with PHP:
When the manufacturer changes submit the page. Build the model list.
 
Hi DRJ478

Maybe I have not explained myself correctly I am after the code or a push in the right direction towards the correct code to use to make this website work.

Kind regards
Russ
 
If you want all this to happen in PHP, then when the make of car is selected, that form must be submitted to a PHP script. The PHP script to which the form is submitted can then accept the make of car and reproduce the form, only this time with the make of car pre-selected and the models of car populated.

How you make the form submission happen is a matter of your choice. You can put a submit button on the form or you can use client-side scripting to detect the change in the make of car and automatically submit the form.


Want the best answers? Ask the best questions!

TANSTAAFL!!
 
OK, here's a more verbose explanation:

1. When the manufacturer is selected use a JavaScript onChange event to submit the page.

2. Check in the PHP if the manufacturer was posted
Code:
if(isset($_POST['manufacturer'])){
  # build SQL query and list on page
  $mSQL = "SELECT * FROM models WHERE manufacturer='".$_POST['manufacturer'].'"';
  result = mysql_query(..etc...


Hope this helps, - if not, feel free to ask further questions.
 
You could trap the onclick event for the drop down and in the javascript procedure move the selected value into a hidden field on a form and generate a submit. (if you search this forum i've posted code to do this, can't find it on my laptop at the mo !). That shoud get you around one problem. I think you need to start a session and put the selected details in there you could even use the session in the onclick procedure. (or maybe in hiden field in the form). When the final "click" happens read session and update the file database etc.
Sorry it's a bit sketchy but it should give you some thoughts.
Regards
 
If the <select> element holding the first list is within a form the assigning of the value to a hidden field is superfluos. Your suggestion of initializing a SESSION variable however is excellent.
 
Yesterdays-

If it's not too much trouble, post your code. If you're a complete newbie, this would be the best way for us to help you reach your goal.



*cLFlaVA
----------------------------
A polar bear walks into a bar and says, "Can I have a ... beer?"
The bartender asks, "What's with the big pause?
 
Hey -

I had the same issue (see post directly above yours :) ) and this is how I solved it...

1. Name your form in the form tag

2. Put the following in each of the drop downs that will refine your search:

onchange="document.forms['FORMNAME'].submit()"

In your example, make and model of cars would have the above added to their select drop downs. Replace FORMNAME with your form name obviously.

3. GO back to your form and in the action point it to a handler.

4. In your handler, create an if statement for make and model, for example...
if(carmodel == ''){
create a session variable of the car make and go back to file
} else {
create session for model and go back to file
}

5. Use the session variable in yrou other drop downs, what I did is:

if(!isset($_SESSION[carmodel])){
<option>SELECT CAR MAKE</option>
} else {
use Session variable and create a drop down query
}


Hope that helps to give you some direction.

 
Hi Guys,
Right here is my code don't laugh if it's wrong. I have put the first droplist in but not the other two as I don't know what to write.

the tables in my database are 'manu' for the first droplist which contains the manufacturers of the cars. depending on what the user clicks on in the first box goes to a different table in the second droplist eg. i they choose ford in the first droplist in the second a list of all the ford cars(ford table) will appear. The user clicks on ford escort and this brings up in the third droplist a list of generic parts (generic table)of any car (so this is the same table for every car) eg a steering wheel, or a gear stick or a handbrake. I hope this makes sense.
from this the data from all three table must be sent to a text file on the local machine. it's on good just the data from the third box as I need to know which car manufacturer and model it is for.
If you can tell he what I am to read on the net to give me this information on how to do it that would be great.
ok finally here is the code
<table width="393" height="64">
<tr>
<td width="99">
<?php
$h="localhost";
$u="root";
$p="";
$db1="database";
$connect=mysql_connect($h,$u,$p) or die ("please try again");
$db=mysql_select_db($db1,$connect);
$query="select manu from manufact";
$result=mysql_query($query) or die ("that query doesn't work");
echo "<form action='' method='post'>
<select name='manu'>\n";
while ($row = mysql_fetch_array($result)) /*what does that mean? */
{
extract($row); //what does this mean and do
echo "<option value='$manu'>$manu\n"; /*what does this mean and do */
}
echo "</select>\n";
echo "<input type='submit' value='go to next droplist'>";
echo "</form>";
?>
</td>
<td width="100">&nbsp;</td>
<td width="178">&nbsp;</td>
</tr>
</table>

thanks for the help
Russ
 
Well, there's a lot we can do.

I'll go through an example for just two select boxes. You'll be able to do the same for the other two.

First of all, you should have the following at the top of your file:
Code:
$manu = $_POST['manu'];
$model = $_POST['model'];
This will store the manufacturer and model in variables, if they were set. If they haven't yet been sent (and the form, therefore, hasn't been submitted), they will be empty.

Next, we come to your code that builds the first select box. We add some php that will make the default value equal to that which has been selected. This is useful once the user selects Ford and submits the page. This way, when the page is submitted, Ford will be selected.
Code:
echo "<form action='' method='post'><select name='manu'>\n";
while ($row = mysql_fetch_array($result)) {
    echo '<option value="' . $row['manu'] . '"';
    if ($manu == $row['manu'])
        echo ' selected';
    echo '>' . $row['manu']\n";
}
echo "</select>\n";

Now, test for a value in $manu. If one exists, build the query for the models.
Code:
if (!empty($manu)) {
    $sql = "SELECT * FROM cars WHERE manu = '$manu'";
    $result = mysql_query($sql);
    if ($result) {
        //build the CAR drop-down with the same method here.
    } else {
        //error in query
    }
}

Gotta get back to work.
Let us know if this helps.

*cLFlaVA
----------------------------
A polar bear walks into a bar and says, "Can I have a ... beer?"
The bartender asks, "What's with the big pause?
 
Hi
I have tryed the code but it comes up with an error
Warning: Unexpected character in input: '\' (ASCII=92) state=1 in c:\testingfolder\car.php on line 132

Parse error: parse error, unexpected T_STRING, expecting ',' or ';' in c:\testingfolder\car.php on line 132
its something to do this this bit i think

echo '<option value="' . $row['manu'] . '"';
if ($manu == $row['manu'])
echo ' selected';
echo '>' . $row['manu']\n";
}
and ideas
thanks for the help by the way
Russ
 
It's exactly what the error is reporting:

echo '>' . $row['manu']\n";

You have a bare "\n". The line should probably read:

echo '>' . $row['manu'] . "\n";


Want the best answers? Ask the best questions!

TANSTAAFL!!
 
Yours:

echo '<option value="' . $row['manu'] . '"';
if ($manu == $row['manu'])
echo ' selected';
echo '>' . $row['manu']\n";
}

What it should look like, dont forget to add slashes!

echo '<option value=\"' . $row['manu'] . '\"';
if ($manu == $row['manu'])
echo ' selected';
echo '>' . $row['manu'] . "\n";
}
 
Shanksta
Oh no!
Don't forget that double quotes only need to be escaped within double quotes. sleipnir214 concatenates the strings and encloses the double quotes inside single quotes, hence, no escaping is necessary.

 
WHOOPS!
DRJ478 - Thanks, I am getting too used to using double quotes whenever I echo out html to preserve caps.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top