473,394 Members | 1,770 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

need help with php getting a selection from a drop down

27
I have the following html code:
[HTML]<form method="get">
<select name="zoni" onchange="showCustomer(this.value)">
<option value="0" selected="selected">[Select]</option>
<option value="1">zone А</option>
<option value="2">zone B</option>
<option value="3">zone C</option></select></form>[/HTML]

I am trying to create an if ... else statement in php to detect which option was selected and to echo certain text. I can't get the php to do it. any ideas? Any help would be highly appreciated.
Aug 14 '07 #1
3 1622
Dreea
37
Expand|Select|Wrap|Line Numbers
  1. if ($_GET['zoni']==$your_value) 
  2.       echo "some text"; 
  3. else echo "other text";
  4.  
I'm not sure i understand where you want to place the if -then statement, but if you want to echo select text depending on whether the value was selected or not you can do it like that:
[HTML]
<form method="get">
<select name="zoni" onchange="showCustomer(this.value)">
<option value="0" selected="selected">[Select]<?php if ($_GET['zoni']=='0') echo "is selected"?></option>
<option value="1">zone А <?php if ($_GET['zoni']=='1') echo "is selected";?></option>
<option value="2">zone B <?php if ($_GET['zoni']=='2') echo "is selected";?></option>
<option value="3">zone C <?php if ($_GET['zoni']=='3') echo "is selected";?></option>
</select>
</form>[/HTML]

If you want to do this more elegant, you should try this:
Expand|Select|Wrap|Line Numbers
  1. <?php
  2. $selectOptions[0]="[Select]";
  3. $selectOptions[1]="Zone A";
  4. $selectOptions[2]="Zone B";
  5. $selectOptions[3]="Zone C";
  6. $i=0;
  7. ?>
  8. <select name="zoni" onchange="showCustomer(this.value)">
  9. <?php
  10. while (isset($selectOptions[$i]))
  11. {
  12.    $text=$selectOptions[$i];
  13.    if ($_GET['zoni']==$i) $text.=" is selected";
  14.    echo '
  15.     <option value="'.$i.'">'.$text.'</option>
  16.    '; 
  17.    $i++;
  18. }
  19. ?>
  20. </select>
  21.  
Hope it helps
Aug 14 '07 #2
pbmods
5,821 Expert 4TB
Heya, bettor.

PHP does not communicate with the browser that way. You'd have to either submit the form or else use an AJAX call to send the value to PHP.
Aug 14 '07 #3
bettor
27
Expand|Select|Wrap|Line Numbers
  1. if ($_GET['zoni']==$your_value) 
  2.       echo "some text"; 
  3. else echo "other text";
  4.  
I'm not sure i understand where you want to place the if -then statement, but if you want to echo select text depending on whether the value was selected or not you can do it like that:
[HTML]
<form method="get">
<select name="zoni" onchange="showCustomer(this.value)">
<option value="0" selected="selected">[Select]<?php if ($_GET['zoni']=='0') echo "is selected"?></option>
<option value="1">zone А <?php if ($_GET['zoni']=='1') echo "is selected";?></option>
<option value="2">zone B <?php if ($_GET['zoni']=='2') echo "is selected";?></option>
<option value="3">zone C <?php if ($_GET['zoni']=='3') echo "is selected";?></option>
</select>
</form>[/HTML]

If you want to do this more elegant, you should try this:
Expand|Select|Wrap|Line Numbers
  1. <?php
  2. $selectOptions[0]="[Select]";
  3. $selectOptions[1]="Zone A";
  4. $selectOptions[2]="Zone B";
  5. $selectOptions[3]="Zone C";
  6. $i=0;
  7. ?>
  8. <select name="zoni" onchange="showCustomer(this.value)">
  9. <?php
  10. while (isset($selectOptions[$i]))
  11. {
  12.    $text=$selectOptions[$i];
  13.    if ($_GET['zoni']==$i) $text.=" is selected";
  14.    echo '
  15.     <option value="'.$i.'">'.$text.'</option>
  16.    '; 
  17.    $i++;
  18. }
  19. ?>
  20. </select>
  21.  
Hope it helps
I tried this:
Expand|Select|Wrap|Line Numbers
  1. if ($_GET['zoni']==$your_value) 
  2.       echo "some text"; 
  3. else echo "other text";
  4.  
as you said but it doesn't work. I don't know why. What I am trying to achieve here is a drop down menu which displays different values according the selected option via ajax. I am sure my ajax works but my php file which the js connects to doesn't. I guess I am writing my php in a wrong way. I just want an if...else statement which is able to detect which option has been selected by the user but no success so far.... However, I need a separate php file that has only the php code in it so I can call it with JS. :(
Aug 14 '07 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: Mudcat | last post by:
Howdy, I'm trying to create a selection helper, just like in Google or other places where a drop-down menu appears below the text selection box when you enter some text. As you type, the choices...
5
by: Rob Wire | last post by:
For the code below, how could I add an item in the drop down lists for both company and location to be an "All" selection that would send to the stored proc. spRptAttachments a value of "%" so...
3
by: Mike | last post by:
I have a web page that displays contact people in a drop down. the users selects a person then clicks the go button. The datagrid should pop with all the information on the select contact person,...
2
by: CW | last post by:
In an earlier thread, I was asking for help on "Invalid attempt to FieldCount when reader is closed" error when I was using a dataset rather than a datareader to bind to a datagrid. After...
4
by: deepee | last post by:
I have a drop down box in HTML using SELECT and OPTION tags: <select title="Choose a number" onchange="obscure()" name="Digit1" ID="Digit1"> <OPTION VALUE="">&nbsp;</OPTION> <OPTION...
1
by: pmarisole | last post by:
I need help in calculating a score from a row of drop-down values. I need to use the onChange to tally the score as the user moves across 9 categories (with drop-down selection of 1-9 or N/A)...
1
by: bvlmv | last post by:
Hello, relatively new to programming and just trying to create a simple asp page. I have the following drop down menu on a html page and when submitting i would like to call out on asp what someone...
4
by: deanndra | last post by:
First, I want to say thank you to Scott and the others who replied to my first post here. I had to put that database on hold for the moment when I was tasked with a new one. I am building another...
26
by: kpouya | last post by:
I am going to be as clear as possible about what i got right now and what i want to achieve Table1: Libary Title - memo Last Name - memo First Name - memo Company - memo Year - number Type -...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.