Solved

option value in php

Posted on 2009-07-14
14
268 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
14 Comments
 
LVL 30

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
 
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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
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

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Deprecated and Headed for the Dustbin By now, you have probably heard that some PHP features, while convenient, can also cause PHP security problems.  This article discusses one of those, called register_globals.  It is a thing you do not want.  …
Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this.Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it is …
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 look for a specific file type in a local or remote server directory using PHP.

708 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now