Loading options in a select box after a previous selection

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Rovas
Forum Contributor
Posts: 272
Joined: Mon Aug 21, 2006 7:09 am
Location: Romania

Loading options in a select box after a previous selection

Post by Rovas »

EDIT: I soved it, kinda (but a back link) Thanks for all the help.
I want to make a page where the user compares two products. I thought of putting data about the category, firm and the product (there going to be many products in the site on different categories) in select boxes. The user selects the category desired then two select boxes appear with the firms, selects one firm then select boxes for product appear and after selecting this the specification for the products appear.
I' m having problems at creating the session (on my local computer) in which the data about the last product viewed is stored. Session are set to start automaticly, create a cookie on the client machine with the path "/".
The following error appears:
Warning: Unknown(): open(/tmp\sess_5a501419b6f32067d1fa90269262b713, O_RDWR) failed: No such file or directory (2) in Unknown on line 0

Warning: Unknown(): Failed to write session data (files). Please verify that the current setting of session.save_path is correct (/tmp) in Unknown on line 0
I created tmp folder and granted RW for my user but still the warnings appear appear.
Second problem is that after selecting a value from the select boxes nothing happens even if put

Code: Select all

<form method='POST' action=<?php $_SERVER[PHP_SELF] ?>>
or 
<form method='GET' action=<?php $_SERVER[PHP_SELF] ?> >
Here is a part of the code (it can repeats itself for the rest, the functions called fill the option tag with the name and id of the category, firm and product ).

Code: Select all

//the 
   if(!isset($_GET['prod_1']) && $_SESSION[prod]='')
   {   
?>

  <form name='f' method='GET' action='<?php $_SERVER['PHP_SELF']?>'>
    <table>
      <tr>
				<td colspan='2'>Pick the category of the products you wish to compare:&nbsp;</td>
                                 <!-- filling the select box with the names of the categories -->
				<td><select name='categ'><?php sel_c()?></select></td> 
			</tr>
<?php
    }          //displaying after a category is selected the companies that have products in it 
		if(isset($_GET['categ']))
		{ 
?>			
			<tr>
      	<td>Pick the company which makes the desired product: &nbsp;
                                <!-- displays the 
				<td><select name='firma_1'><?php sel_f($categ) ?></select></td>
				<td><select name='firma_2'><?php sel_f($categ) ?></select></td>
			</tr>
<?php
                //similar for products
		if(isset($_GET['firma_1']) || isset($_GET[firma_2]))
		{ ...}
                if(isset ($_GET[Compare]))
{ 
?>
  <tr> <td>displayProduct($product1)</td>
         <td>displayProduct($product2)</td>
  </tr>
<?php
  }
?>
Last edited by Rovas on Wed Oct 04, 2006 4:25 am, edited 1 time in total.
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

Warning: Unknown(): Failed to write session data (files). Please verify that the current setting of session.save_path is correct (/tmp) in Unknown on line 0
Did you check your php.ini?
If you have to change the parameter to a valid directory path (maybe because you're using php on win32) remember to restart the webserver (apache?).
Rovas
Forum Contributor
Posts: 272
Joined: Mon Aug 21, 2006 7:09 am
Location: Romania

Post by Rovas »

I figured that after I posted here and still it won' t work. The new warning
Warning: Unknown(): open(/\sess_e60b3e3b06ae3299c0c222a857d5b958, O_RDWR) failed: Invalid argument (22) in Unknown on line 0

Warning: Unknown(): Failed to write session data (files). Please verify that the current setting of session.save_path is correct (/) in Unknown on line 0
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

New warning? It's still the same.

Call a script with phpinfo()
It tells you which php.ini is used.
Open that php.ini
Set the value of session.save_path to a valid path.
Save the file and restart the webserver.
Call again the phpinfo() script and check the new settings. Does session.save_path show up in the output as expected?
Rovas
Forum Contributor
Posts: 272
Joined: Mon Aug 21, 2006 7:09 am
Location: Romania

Post by Rovas »

Now why didn' t I think about that? :oops:
It works thanks volka!
I worked on the other problem I mentioned and still no solution. :cry:
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

if(isset($_GET['categ']))
{
[...]sel_f($categ)
There's no code to set $categ. If register_globals is off (as it should be) $categ is undefined.
<?php $_SERVER['PHP_SELF']?>
that doesn't do anything. You probably want <?php echo $_SERVER['PHP_SELF']; ?>.
Regarding PHP_SELF please take a look at viewtopic.php?p=309910#309910
There are some other variables related problems (e.g. $_GET[firma_2], why is firma_2 not quoted?). You should carefully (re-)inspect your script for consistency.
Rovas
Forum Contributor
Posts: 272
Joined: Mon Aug 21, 2006 7:09 am
Location: Romania

Post by Rovas »

Sorry about the long time between posts but I live in Europe.
I don' t know how to set

Code: Select all

$_GET['categ']
except using a submit button which I don' t want to use that.
I' m thinking of using JS on the select using escape strings and get the data returned to use in PHP.
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

Rovas wrote:I don' t know how to set

Code: Select all

$_GET['categ']
$_GET is not the issue. I was talking about $categ. Where does it come from?
Rovas
Forum Contributor
Posts: 272
Joined: Mon Aug 21, 2006 7:09 am
Location: Romania

Post by Rovas »

Code: Select all

$_categ
is the selected value of the categories dropdown menu (I call them select boxes). Is initialized (set) by the user when he clicks a value from the categories menu (it' s "captured" by $_GET).

Code: Select all

<select name='categ' onchange='SendForm()'>
                    <option value='1'> Convertible</option>
                    <option value='4'>Coupe</option>
                    <option value='5'>Sedan</option>
          </select>
Hope you understand. Now I' m struggling to make a JS script to submit the changes made to the form.
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

Rovas wrote:

Code: Select all

$_categ
is the selected value of the categories dropdown menu (I call them select boxes). Is initialized (set) by the user when he clicks a value from the categories menu (it' s "captured" by $_GET).
esp. the underlined part I do not understand.
I don't see where $categ is assigned the value of $_GET['categ']. If register_globals is off, php will not automagically assign the value of the GET parameter to $categ. see http://de2.php.net/security.globals
Rovas
Forum Contributor
Posts: 272
Joined: Mon Aug 21, 2006 7:09 am
Location: Romania

Post by Rovas »

sorry typo I meant
$_GET['categ']
not
$_categ
.

Code: Select all

$_GET ['categ']
gets the selected item from the dropdown menu with the name categ when the user clicks (selects) the desired value.
Then I show the firms which have products in that category using the function

Code: Select all

sel_f($_GET['categ'])

which is a pretty simple function that outputs another dropdown menu from a query that selects the firms which have products in that category.
The functions work fine because I tested them. What doesn' t work is the setting of the variable which we discuss.
I put an JS script that posts an entire form using the method GET on the an "different" page (different because it just adds the selected values (mysite/comparare.php?categ=2&firm_1=2).
Should I use POST instead?
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Re: Loading options in a select box after a previous selecti

Post by volka »

Rovas wrote:<td>Pick the company which makes the desired product: &nbsp;
<!-- displays the
<td><select name='firma_1'><?php sel_f($categ) ?></select></td>
<td><select name='firma_2'><?php sel_f($categ) ?></select></td>
</tr>
here you're using $categ, not $_GET['categ']. Already fixed?
Is sel_f() echo'ing or returning the data?
Rovas wrote:Should I use POST instead?
<form method="get"> => $_GET, <form method="post"> => $_POST
Should work either way, so don't bother right now.
Rovas
Forum Contributor
Posts: 272
Joined: Mon Aug 21, 2006 7:09 am
Location: Romania

Post by Rovas »

No because the JS script (who is submiting the selected data) is not working yet.
Edit: Forgot sel_f() it' s echoing the data.
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

Do you want to post your javascript here then? The code copied from the browser's source view is the most useful for javascript debugging - not the php source ;)
viewtopic.php?t=55331 might also be of interest.
Rovas
Forum Contributor
Posts: 272
Joined: Mon Aug 21, 2006 7:09 am
Location: Romania

Post by Rovas »

here is the code (it' s pretty simple ) no errors are given by Venkman (JS debugger) on the code but in IE it display' s the Object expected error.
I put some of the code in a test page not on the compare.php (i like to test something a couple of times before implementing it).
Here is the code:

Code: Select all

//JS code
   		<script language="javascript">
   			function send()
   			{
   						f=document.getElementByID("form");
                                                mylist=document.getElementByID('test');
	 					if(typeof(f) == 'undefined')
							{
									document.write "An error has occured, where sorry for the incovience!";
									return;
							}
							document.getElementById("camp").value=.options[mylist.selectedIndex].text;
							f.submit();
				}
   		</script>
		</head>  		
		<body>
			<form method='POST' name='form' action='#'>
	  		<select name='test' onchange='trimitere()'>
	  			<option value='Test 1' >Test 3</option>
	 				<option value='Test 2'>Test 4</option>
	  		</select><br />
				Result: <input type='text' name='camp' value='' />
			</form>
Post Reply