Solved

option value in php

Posted on 2009-07-14
14
273 Views
Last Modified: 2012-05-07
Hi

please find attached snippet of my code the problem that $val brings me the last value of the table (database table called type)

<form action="form.php" method="GET" >
<fieldset><legend><b>Type Product</b></legend>
<table><tr>
<td>Type Product:</td>
<?php echo "<option value='$val'>$tt</option>";?>
<td><select name="type" size="1"> 
<?php
$query="select * from type";
$result=mysql_query($query);
$i=0;
          for($i=0;$i<mysql_num_rows($result);$i++){
              $tt=mysql_result($result,$i,"type");
              $type=mysql_result($result,$i,"type_id");
              $val=mysql_result($result,$i,"valeur");
               
              echo "<option value='$type'>$val.$tt</option>\n";
        
              
  }
?>    
<td><input type="hidden" name="val" value='<?php echo $val;?>'</td></tr> 
</select>  
</tr></table>
<table>
<tr><td><input type="submit" name="submit" value="Submit"></td></tr></table>
<?php echo"<h2><a href='index.php'>Return to The Main Menu</a></h2>";?>
</table>
</form>
</body>
</html>

Open in new window

0
Comment
Question by:asaidi
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
14 Comments
 
LVL 31

Expert Comment

by:Zoppo
ID: 24847789
Hi asaidi,

what do you want '$val' to be set to? In the code you iterate through all entries in the table 'type' - with each iteration you set '$val' to the value of 'valeur', so after the for-loop '$var' contains the last entries 'valeur'.

ZOPPO
0
 
LVL 14

Expert Comment

by:flob9
ID: 24847796
Since you use the $val variable in the loop, it's normal to get it to the last value from the database ...

Use another variable name inside the loop.
0
 

Author Comment

by:asaidi
ID: 24847857
Hi
$val i want to set for the value i choose in my <option value>
there is valeur in my database set to 21.5 and when i choose it ,i have everytimes the value 1 means the last value of my database table
0
PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

 
LVL 14

Expert Comment

by:profya
ID: 24850266
Replace your php script block with this:
<?php
$query="select * from type";
$result=mysql_query($query);
if ($result && mysql_num_rows($result)>)
while($rec=mysql_fetch_array($result)
{
	echo "<option value='".$rec['type_id']."'>".$rec['valeur'].".".$rec['type']."</option>\n";
}
?>    

Open in new window

0
 

Author Comment

by:asaidi
ID: 24850382
Hi PROFYA

if ($result && mysql_num_rows($result)>)
greater in what ?

and how i can pass then the $rev['valeur'] to a variable to another page
0
 
LVL 14

Expert Comment

by:profya
ID: 24850480
>0
Sorry for that. This checks if there is data returned by the query.
0
 
LVL 14

Expert Comment

by:profya
ID: 24850522
In this case you need to use javascript. When the user selects an option then you pass it to the hidden input. When the form is then posted, the value is sent. Why? Because you have a loop, the last valuer will then be sent.
I'll give you an example:
0
 
LVL 14

Expert Comment

by:profya
ID: 24850591
Change the select tag to:
<select name="type" size="1" onchange="document.forms[0].elements.val.value=this.options[this.selectedIndex].value">

Open in new window

0
 
LVL 7

Expert Comment

by:ajkhalifa
ID: 24850676
cancel the code from line 11 till line 16, and instead put this:
while($R=mysql_fetch_array($result)){
              echo "<option value='".$R[0]."'>".$R[1].".".$R[2]."</option>\n"
}
0
 
LVL 14

Expert Comment

by:profya
ID: 24850703
"and how i can pass then the $rev['valeur'] to a variable to another page"
Since your select is within the form, when form is posted, its value (user selection) sent with other form inputs, you can get the value from the other page via:
$_GET['type']
But you don't want to send the type you want them both the type and the value, you choosed to add a new hidden input, the code I attached, updates the hidden input with the value selected by the user.

You can also acheive this by butting both the type and value in the option attribute VALUE, separated by comma. Then when you get the value in the other page you split the value by comma. For example:
change the option line to:
echo "<option value='".$rec['type_id'].", ".$rec['valeur']."'>".$rec['valeur'].".".$rec['type']."</option>\n"
each option value now holds the type and the value. In the other form
list($mytype, $myvalue)=split(",", $_GET['type']);
echo $mytype;
echo $myvalue;

I hope this helps.
You added a new hidden input
0
 

Author Comment

by:asaidi
ID: 24851192
Hi Profya

it is working but the type holds the type and the value and how i can make seperates because i have to select from a table with where type =$type for the moment i have (type,value)
and how i can make them type=type and value=value(= not equal it is the set option)
0
 
LVL 14

Expert Comment

by:profya
ID: 24851229
What technique you are using now, the javascript or the joining of the two into one?
0
 

Author Comment

by:asaidi
ID: 24851245
no not the java one the second technique
0
 
LVL 14

Accepted Solution

by:
profya earned 500 total points
ID: 24851302
Since the select input type now holds the type and the value, when the form is submitted, $_GET['type'] gives us the selection, for example: "type1, 2091"
All we need is to split it by comma, to get "type1" in a variable and then "2091" in a variable. The code attached does this, it splits the value into two:
$mytype which contains the type
$myvalue which contains the value.

Then you can use these variables in the rest of your code.
list($mytype, $myvalue)=split(",", $_GET['type']);
echo $mytype;
echo $myvalue;

Open in new window

0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
3 proven steps to speed up Magento powered sites. The article focus is on optimizing time to first byte (TTFB), full page caching and configuring server for optimal performance.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

751 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question