Change select option values on return to a html page from a php page

  • (2 Pages)
  • +
  • 1
  • 2

17 Replies - 9994 Views - Last Post: 21 June 2009 - 05:10 AM Rate Topic: -----

#1 livium   User is offline

  • D.I.C Addict

Reputation: 0
  • View blog
  • Posts: 554
  • Joined: 21-December 08

Change select option values on return to a html page from a php page

Posted 21 June 2009 - 01:18 AM

How can i change select option values on return to a html page from a php page ? I though of two ways but they are very complicated. One involves writing a php section in my html for every option. Here's the code (from the html page):

<?php
	  if (!isset($_SESSION['language'])) $_SESSION['language']="English";
			   
	?>
	  
	  
		<select name="lang" id="lang">
		  <option value="Romanian">Romanian</option>
		  <option value="English" selected="selected">English 
		  
		 </option>
		  <option value="Deutsch">Deutsch</option>
		  <option value="French">French</option>
		</select>




with $_SESSION['language'] i retain the language variable when i press Submit.
for exemple, if i select deutsch and then press submit, i want the value deutsch to be the one selected.

One way of doing this i though, was to echo the hole select and add a line like this: <option value=$_SESSION['language']>$_SESSION['language']</option>

but the problem is that it can put me two identical option values.
Is there a way to make select unique, i mean if i have two identical options, just one to be taken into account?

Is This A Good Question/Topic? 0
  • +

Replies To: Change select option values on return to a html page from a php page

#2 livium   User is offline

  • D.I.C Addict

Reputation: 0
  • View blog
  • Posts: 554
  • Joined: 21-December 08

Re: Change select option values on return to a html page from a php page

Posted 21 June 2009 - 01:29 AM

ok!

this is how i thougt

		<select name="lang" id="lang">
		  <?php
	  if (!isset($_SESSION['language'])) $_SESSION['language']="English";
	  
	  echo '<script type="text/javascript">';
		 echo 'document.getElementById("'.$_SESSION['language'].'").selected="selected"';
		 echo '</script>';   
		 
		 
	?>
		  <option value="Romanian">Romanian</option>
		  <option value="English">English </option>
		  <option value="Deutsch">Deutsch</option>
		  <option value="French">French</option>
		</select>



but when it is english selected and i press submit it shows me Romanian, so i guess it doesnt work
Was This Post Helpful? 0
  • +
  • -

#3 noorahmad   User is offline

  • Untitled
  • member icon

Reputation: 209
  • View blog
  • Posts: 2,290
  • Joined: 12-March 09

Re: Change select option values on return to a html page from a php page

Posted 21 June 2009 - 01:33 AM

try this:
      if (!isset($_SESSION['language'])) $_SESSION['language']="Deutsch";
?>    
     
        <select name="lang" id="lang">
          <option value="Romanian"<?php if($_SESSION['language']=="Romanian"){ echo "selected='selected'";}?>>Romanian</option>
          <option value="English"<?php if($_SESSION['language']=="English"){ echo "selected='selected'";}?>>English
         
         </option>
          <option value="Deutsch"<?php if($_SESSION['language']=="Deutsch"){ echo "selected='selected'";}?>>Deutsch</option>
          <option value="French"<?php if($_SESSION['language']=="French"){ echo "selected='selected'";}?>>French</option>
        </select>



Hope it Helps You :)
Was This Post Helpful? 0
  • +
  • -

#4 no2pencil   User is offline

  • Professor Snuggly Pants
  • member icon

Reputation: 6879
  • View blog
  • Posts: 31,672
  • Joined: 10-May 07

Re: Change select option values on return to a html page from a php page

Posted 21 June 2009 - 01:34 AM

<?php
	  if (!isset($_SESSION['language'])) $_SESSION['language']="English";
?>
		<select name="lang" id="lang">
		  <option value="Romanian"<?php if($_SESSION['language']=="Romanian") echo " selected";?>>Romanian</option>
		  <option value="English"<?php if($_SESSION['language']=="English") echo " selected";?>>English </option>
		  <option value="Deutsch"<?php if($_SESSION['language']=="Deutsch") echo " selected";?>>Deutsch</option>
		  <option value="French"<?php if($_SESSION['language']=="French") echo " selected";?>>French</option>
		</select>



** Edit **

Ha-ha, nice noorahmad.... great minds think a-like, eh?
Was This Post Helpful? 0
  • +
  • -

#5 livium   User is offline

  • D.I.C Addict

Reputation: 0
  • View blog
  • Posts: 554
  • Joined: 21-December 08

Re: Change select option values on return to a html page from a php page

Posted 21 June 2009 - 01:45 AM

i understand what your code is doing. the noorahmand's doesn't work. I haven't tested the last one but i guess it works.

But... once again. If I have 150 values? Isn't it boring to write this code?

Isnt' it easier by javascript?
Here's what i tought:

	<select name="lang" id="lang">
		  
		  <option value="Romanian" id="Romanian">Romanian</option>
		  <option value="English"  id="English">English </option>
		  <option value="Deutsch" id="Deutsch">Deutsch</option>
		  <option value="French"   id="French">French</option>
		
	<?php
	 if (!isset($_SESSION['language'])) $_SESSION['language']="English";
	  
	  echo '<script type="text/javascript">';
		 echo 'document.getElementById("'.$_SESSION['language'].'").selected="selected"';
		 echo '</script>';   
	 ?> 
	
	   
		</select>



if i select english it shows romanian.

i thik the problem is the selectd line. How can i acces the selected property of a option?

This post has been edited by livium: 21 June 2009 - 01:48 AM

Was This Post Helpful? 0
  • +
  • -

#6 noorahmad   User is offline

  • Untitled
  • member icon

Reputation: 209
  • View blog
  • Posts: 2,290
  • Joined: 12-March 09

Re: Change select option values on return to a html page from a php page

Posted 21 June 2009 - 01:51 AM

Quote

Ha-ha, nice noorahmad.... great minds think a-like, eh?

Thanks :)
Was This Post Helpful? 0
  • +
  • -

#7 no2pencil   User is offline

  • Professor Snuggly Pants
  • member icon

Reputation: 6879
  • View blog
  • Posts: 31,672
  • Joined: 10-May 07

Re: Change select option values on return to a html page from a php page

Posted 21 June 2009 - 01:53 AM

View Postlivium, on 21 Jun, 2009 - 02:45 AM, said:

But... once again. If I have 150 values? Isn't it boring to write this code?

Then you use an array, & you can reference the array value by number, rather than the entire word.
Was This Post Helpful? 0
  • +
  • -

#8 livium   User is offline

  • D.I.C Addict

Reputation: 0
  • View blog
  • Posts: 554
  • Joined: 21-December 08

Re: Change select option values on return to a html page from a php page

Posted 21 June 2009 - 02:01 AM

View Postno2pencil, on 21 Jun, 2009 - 12:53 AM, said:

View Postlivium, on 21 Jun, 2009 - 02:45 AM, said:

But... once again. If I have 150 values? Isn't it boring to write this code?

Then you use an array, & you can reference the array value by number, rather than the entire word.


maybe i'll use an array, but can you tell me why this doesn't work? It gives me the same as above.

<select name="lang" id="lang">
		  
		  <option value="Romanian" id="Romanian">Romanian</option>
		  <option value="English"  id="English">English </option>
		  <option value="Deutsch" id="Deutsch">Deutsch</option>
		  <option value="French"   id="French">French</option>
		
	<?php
	 if (!isset($_SESSION['language'])) $_SESSION['language']="English";
	  
	  echo '<script type="text/javascript">';
	  
echo '	var myselect=document.getElementById("lang")';
echo 'for (var i=0; i<myselect.options.length; i++){';
echo 'if (myselect.options[i].value=="'.$_SESSION['language'].'"){';
echo 'myselect.options[i].selected=true';
echo 'break';
echo '}';
echo  '}';
	  
	  echo '</script>';   
	 ?> 
	
	   
		</select>


This post has been edited by livium: 21 June 2009 - 02:07 AM

Was This Post Helpful? 0
  • +
  • -

#9 no2pencil   User is offline

  • Professor Snuggly Pants
  • member icon

Reputation: 6879
  • View blog
  • Posts: 31,672
  • Joined: 10-May 07

Re: Change select option values on return to a html page from a php page

Posted 21 June 2009 - 02:15 AM

Javascript executes on the browser, & PHP executes on the server. PHP can't see the output of Javascript.
Was This Post Helpful? 0
  • +
  • -

#10 livium   User is offline

  • D.I.C Addict

Reputation: 0
  • View blog
  • Posts: 554
  • Joined: 21-December 08

Re: Change select option values on return to a html page from a php page

Posted 21 June 2009 - 02:27 AM

View Postno2pencil, on 21 Jun, 2009 - 01:15 AM, said:

Javascript executes on the browser, & PHP executes on the server. PHP can't see the output of Javascript.



so.. what must I do to make the above code work? what must i change?

View Postlivium, on 21 Jun, 2009 - 01:24 AM, said:

View Postno2pencil, on 21 Jun, 2009 - 01:15 AM, said:

Javascript executes on the browser, & PHP executes on the server. PHP can't see the output of Javascript.



so.. what must I do to make the above code work? what must i change?

i don't think this is the problem because i have used similar syntaxes in the same page: for example i have included a php file wich contained a javascript echo, and it worked.
Was This Post Helpful? 0
  • +
  • -

#11 no2pencil   User is offline

  • Professor Snuggly Pants
  • member icon

Reputation: 6879
  • View blog
  • Posts: 31,672
  • Joined: 10-May 07

Re: Change select option values on return to a html page from a php page

Posted 21 June 2009 - 02:35 AM

Look at the HTML code (not PHP) from your browser. Does it look like you expect?
Was This Post Helpful? 0
  • +
  • -

#12 livium   User is offline

  • D.I.C Addict

Reputation: 0
  • View blog
  • Posts: 554
  • Joined: 21-December 08

Re: Change select option values on return to a html page from a php page

Posted 21 June 2009 - 02:43 AM

View Postno2pencil, on 21 Jun, 2009 - 01:35 AM, said:

Look at the HTML code (not PHP) from your browser. Does it look like you expect?



well... it doesn't give me any errors. But i thruly think there's someting wrong with my javascript code that it doesn't do anything.


I don't get it. What i'm i trying to do is someting that (i guess) many have tried. Why does'nt anybody know how to do this from javascript?
Was This Post Helpful? 0
  • +
  • -

#13 noorahmad   User is offline

  • Untitled
  • member icon

Reputation: 209
  • View blog
  • Posts: 2,290
  • Joined: 12-March 09

Re: Change select option values on return to a html page from a php page

Posted 21 June 2009 - 02:46 AM

i am doing it like this:
first of all i am storing Languages in array
$lang = array("English","Romanian","Deutsch","French");

and here is my session where i am storing data after submitting the select value
$_SESSION['language']=$_POST['languages'];


and here is my simple code for selecting
<form name="form1" method="post" action="">
  <select name="languages" id="languages">
<?php
for($i=0;$i<=(count($lang)-1);$i++)
{
?>
    <option value="<?php echo $lang[$i]?>" <?php if($_SESSION['language']==$lang[$i]){ echo "selected";}?>><?php echo $lang[$i]?></option>
<?php
}
?>
  </select>
</form>


Hope it Helps You :)
Was This Post Helpful? 1
  • +
  • -

#14 livium   User is offline

  • D.I.C Addict

Reputation: 0
  • View blog
  • Posts: 554
  • Joined: 21-December 08

Re: Change select option values on return to a html page from a php page

Posted 21 June 2009 - 02:54 AM

ok thanks. I will use your code after this post:

my code from php is

<?php
	 if (!isset($_SESSION['language'])) $_SESSION['language']="English";
	  
	  echo '<script type="text/javascript">';
	  
echo 'var myselect=document.getElementById("lang")';
echo 'for (var i=0; i<myselect.options.length; i++) {';
echo 'if (myselect.options[i].value=="'.$_SESSION['language'].'"){';
echo 'myselect.options[i].selected=true';
echo 'break';
echo '}';
echo  '}';
	  
	  echo '</script>';   
	 ?> 
	



and from page source

 <select name="lang" id="lang">
		  
		  <option value="Romanian" id="Romanian">Romanian</option>
		  <option value="English"  id="English">English </option>
		  <option value="Deutsch" id="Deutsch">Deutsch</option>

		  <option value="French"   id="French">French</option>
	   <script type="text/javascript">	var myselect=document.getElementById("lang")for (var i=0; i<myselect.options.length; i++) {if (myselect.options[i].value=="English"){myselect.options[i].selected=truebreak}}</script> 
		   
		</select>
		





as you can see there's no space between lines
is this the problem?

This post has been edited by livium: 21 June 2009 - 02:56 AM

Was This Post Helpful? 0
  • +
  • -

#15 noorahmad   User is offline

  • Untitled
  • member icon

Reputation: 209
  • View blog
  • Posts: 2,290
  • Joined: 12-March 09

Re: Change select option values on return to a html page from a php page

Posted 21 June 2009 - 03:15 AM

and here is what you want
<?php
session_start();
$_SESSION['language']="French";
$lang = array("English","Romanian","Deutsch","French");
?>
<form name="form1" method="post" action="">
  <select name="languages" id="languages">
<?php
for($i=0;$i<=(count($lang)-1);$i++)
{
?>
    <option value="<?php echo $lang[$i]?>" <?php if($_SESSION['language']==$lang[$i]){ echo "selected";}?>><?php echo $lang[$i]?></option>
<?php
}
?>
  </select>
  <label>
  <input type="button" name="button" id="button" value="Submit" onclick="show()" />
  </label>
</form>
<script language="javascript">
	var i = "<?php echo $_SESSION['language']?>";
	document.getElementById('language').selected =i;
</script>

Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2