Solved

option value in php

Posted on 2009-07-14
14
274 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

Are You Using the Best Web Development Editor?

The worlds of web hosting and web development are constantly evolving. Every year we see design trends change, coding standards adapt and new frameworks/CMS created. With such a quick pace of change it’s easy to get lost trying to keep up.

See if your editor made the list.

Question has a verified solution.

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

Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
Originally, this post was published on Monitis Blog, you can check it here . In business circles, we sometimes hear that today is the “age of the customer.” And so it is. Thanks to the enormous advances over the past few years in consumer techno…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to dynamically set the form action using jQuery.

623 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