Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

PHP Forum


You are currently viewing our PHP Forum as a guest. Please register to participate.
Login



Freelance Jobs

Reply
Displaying value from a Drop Down Menu ( form ) using php ?
Old 06-17-2009, 02:05 PM Displaying value from a Drop Down Menu ( form ) using php ?
jameswhite's Avatar
Average Talker

Posts: 15
Name: James White
Trades: 0
Hi,

I want to take the value selected from a drop down menu I have and then add the value too somewhere else on the page. The reason for this is so eventually I can hide this value and add it into a URL ( it's a long story..).

So anyway this is my code so far:
Code:
<form id="countries_select" name="countries_select" method="post" action="$_SERVER['PHP_SELF']">
  <select name="countries" id="countries" onchange="">
    <option value="US">United States</option>
    <option value="GB" selected="selected">United Kingdom</option>
    <option value="ES">Spain</option>
  </select>
  <label>
    country_selected:
    <?php $country = $_POST[''];
    echo  $country;?>
    
     
  </label>
</form>
So basically I just need to know how to get the value from the drop down into the variable "$country"

Thanks
jameswhite is offline
Reply With Quote
View Public Profile
 
 
Register now for full access!
Old 06-17-2009, 02:07 PM Re: Displaying value from a Drop Down Menu ( form ) using php ?
anderswc's Avatar
Super Talker

Posts: 132
Name: Will Anderson
Location: Terre Haute, IN
Trades: 0
$_POST['countries']
__________________
Will Anderson

Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
anderswc is offline
Reply With Quote
View Public Profile Visit anderswc's homepage!
 
Old 06-17-2009, 02:12 PM Re: Displaying value from a Drop Down Menu ( form ) using php ?
jameswhite's Avatar
Average Talker

Posts: 15
Name: James White
Trades: 0
Funnily enough I had already tried that..

It returns this error Notice: Undefined index: countries in "replaced my website location on server" on line 38

So I just assumed it wasn't the right way to do it
jameswhite is offline
Reply With Quote
View Public Profile
 
Old 06-17-2009, 02:32 PM Re: Displaying value from a Drop Down Menu ( form ) using php ?
NullPointer's Avatar
Will Code for Food

Posts: 2,815
Name: Matt
Location: Irvine, CA
Trades: 0
That's not an error, its a notice. By default error reporting is set to E_ALL & ~E_NOTICE meaning that notices are not reported. What the notice means is that there is no index countries in POST.
__________________

Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
NullPointer is offline
Reply With Quote
View Public Profile Visit NullPointer's homepage!
 
Old 06-17-2009, 02:41 PM Re: Displaying value from a Drop Down Menu ( form ) using php ?
jameswhite's Avatar
Average Talker

Posts: 15
Name: James White
Trades: 0
^ Oh right, any ideas on what I can do then ?
jameswhite is offline
Reply With Quote
View Public Profile
 
Old 06-17-2009, 02:47 PM Re: Displaying value from a Drop Down Menu ( form ) using php ?
Decaf's Avatar
Ultra Talker

Posts: 489
Name: Adam
Trades: 0
You need to send a value with $_POST['countries'].
__________________

Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE

Decaf is offline
Reply With Quote
View Public Profile Visit Decaf's homepage!
 
Old 06-17-2009, 03:26 PM Re: Displaying value from a Drop Down Menu ( form ) using php ?
jameswhite's Avatar
Average Talker

Posts: 15
Name: James White
Trades: 0
Ok I've got it so that it sends the value to a php page called "countries.php" but with the use of a submit button.

I was hoping to get the value to sort of update itself when the country selected from the drop down was changed. And do all this whilst staying on the same page.

Thanks for all the help so far

Updated Code:
HTML Code:
<form id="countries_select" name="countries_select" method="post" action="countries.php">
  <select name="countries" id="countries" onchange="">
    <option value="US">United States</option>
    <option value="GB" selected="selected">United Kingdom</option>
    <option value="ES">Spain</option>
  </select>
  <label>
    <input type="submit" name="button" id="button" value="Submit" />
  </label>
  <label>
    country_selected: <!--This is where I am hoping the country selected will be displayed-->
</label></form>

countries.php
PHP Code:
<?php
$country 
$_POST['countries'];

echo 
"Country Selected: "$country "<br />";

?>
jameswhite is offline
Reply With Quote
View Public Profile
 
Old 06-17-2009, 04:19 PM Re: Displaying value from a Drop Down Menu ( form ) using php ?
maxxximus's Avatar
Extreme Talker

Posts: 219
Name: Rob
Location: UK
Trades: 0
Just use a conditional isset to avoid the notice.

PHP Code:
<form id="countries_select" name="countries_select" method="post" action="<?php echo $_SERVER['PHP_SELF']?>">
  <select name="countries" id="countries" onchange="">
    <option value="US">United States</option>
    <option value="GB" selected="selected">United Kingdom</option>
    <option value="ES">Spain</option>
  </select>
  <label>
    <input type="submit" name="button" id="button" value="Submit" />
  </label>
  <label>
    <?php if (isset($_POST['countries'])){
    
$country $_POST['countries'];

echo 
"Country Selected: "$country "<br />";

}
?>
</label></form>
maxxximus is offline
Reply With Quote
View Public Profile
 
Old 06-17-2009, 05:37 PM Re: Displaying value from a Drop Down Menu ( form ) using php ?
jameswhite's Avatar
Average Talker

Posts: 15
Name: James White
Trades: 0
That works perfectly thanks, one last request.

Is it possible to make it so that the form basically submits itself automatically without the button everytime an option is changed ?

Thanks
jameswhite is offline
Reply With Quote
View Public Profile
 
Old 06-17-2009, 06:57 PM Re: Displaying value from a Drop Down Menu ( form ) using php ?
NullPointer's Avatar
Will Code for Food

Posts: 2,815
Name: Matt
Location: Irvine, CA
Trades: 0
You can do that with javascript. Just set an onclick event for each option to submit the form.

Off the top of my head here is one way to do this:

Code:
function submitForm()
{
     document.countries_select.submit();
}
Code:
onclick="submitForm();"
Using Ajax you might be able to come up with something cleaner or more dynamic.
__________________

Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
NullPointer is offline
Reply With Quote
View Public Profile Visit NullPointer's homepage!
 
Old 06-17-2009, 07:32 PM Re: Displaying value from a Drop Down Menu ( form ) using php ?
jameswhite's Avatar
Average Talker

Posts: 15
Name: James White
Trades: 0
Not Sure if i've done this right or not, because it still doesn't seem to be automatically doing it. Only works once the submit button has been pressed ( which I have now deleted )

HTML Code:
  <form id="countries_select" name="countries_select" method="post" action="<?php echo $_SERVER['PHP_SELF']?>">
  [php]<?php function submitForm()
{
     document.countries_select.submit();
}?>[/php]
  <select name="countries" id="countries" onchange="submitForm();">
    <option onclick="submitForm();" value="US" input type="submit">United States</option>
    <option onclick="submitForm();"value="GB" >United Kingdom</option>
    <option onclick="submitForm();"value="ES">Spain</option>
  </select>
  <label>
    [php]<?php if (isset($_POST['countries'])){
    $country = $_POST['countries'];

echo "Country Selected: ".$country. "<br />";

}?>[/php]
jameswhite is offline
Reply With Quote
View Public Profile
 
Old 06-17-2009, 09:38 PM Re: Displaying value from a Drop Down Menu ( form ) using php ?
NullPointer's Avatar
Will Code for Food

Posts: 2,815
Name: Matt
Location: Irvine, CA
Trades: 0
The code I posted is javascript not PHP.

Code:
  <form id="countries_select" name="countries_select" method="post" action="<?php echo $_SERVER['PHP_SELF']?>">
<script type="text/javascript">
function submitForm()
{
     document.countries_select.submit();
}
</script>
  <select name="countries" id="countries" onchange="submitForm();">
    <option onclick="submitForm();" value="US" input type="submit">United States</option>
    <option onclick="submitForm();"value="GB" >United Kingdom</option>
    <option onclick="submitForm();"value="ES">Spain</option>
  </select>
  <label>
    <?php if (isset($_POST['countries'])){
    $country = $_POST['countries'];

echo "Country Selected: ".$country. "<br />";

}?>
__________________

Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
NullPointer is offline
Reply With Quote
View Public Profile Visit NullPointer's homepage!
 
Old 06-18-2009, 09:20 AM Re: Displaying value from a Drop Down Menu ( form ) using php ?
jameswhite's Avatar
Average Talker

Posts: 15
Name: James White
Trades: 0
^ oops yh sorry It was pretty late at night when I read your post

One last thing now I've got more $country variable working I want to insert it into a custom URL I'm generating but everytime I put it in it says 'Undefined Variable' again.

I want to place the country that is selected after the 'country=' part of the URL

HTML Code:
<?php
table_add('Fall Into Place','Apartment');
table_add('Kids','MGMT');
table_add('Closer','Travis');

function table_add($title,$artist) {
$term = str_replace(' ','+',$title.' '.$artist);
$link = '<a href="http://ax.phobos.apple.com.edgesuite.net/WebObjects/MZStoreServices.woa/wa/wsSearch?term='.$term.'&limit=1&entity=musicTrack&country='.$country.'">Download</a>';
echo '<tr><td>'.$title.'</td><td>'.$artist.'</td><td>'.$link.'</td></tr>';};

?>

</tbody>
</table> 
&nbsp;</th>
  </tr>
<form id="countries_select" name="countries_select" method="post" action="<?php echo $_SERVER['PHP_SELF']?>">
    <form id="countries_select" name="countries_select" method="post" action="<?php echo $_SERVER['PHP_SELF']?>">
<script type="text/javascript">
function submitForm()
{
     document.countries_select.submit();
}
</script>
<select name="countries" id="countries" onchange="submitForm();">
    <option value="US" selected="selected">Select Country</option>
    <option onchange="submitForm();" value="US" input type="submit">United States</option>
    <option onchange="submitForm();" value="GB">United Kingdom</option>
    <option onchange="submitForm();" value="ES">Spain</option>
    <option onchange="submitForm();" value="DE">Germany</option>
    <option onchange="submitForm();" value="NL">Netherlands</option>
    <option onchange="submitForm();" value="FR">France</option>
    <option onchange="submitForm();" value="IT">Italy</option>
    <option onchange="submitForm();" value="PL">Poland</option>
    <option onchange="submitForm();" value="MX">Mexico</option>
    <option onchange="submitForm();" value="PT">Portugal</option>
    <option onchange="submitForm();" value="BR">Brazil</option>
  </select>
  
<label>

<?php
    $country = false;
    if (isset($_POST['countries'])){
    $country = $_POST['countries'];
echo 'Country Selected: '.$country. '<br />';

}?>
Thanks you've all been really helpful
jameswhite is offline
Reply With Quote
View Public Profile
 
Old 06-18-2009, 12:14 PM Re: Displaying value from a Drop Down Menu ( form ) using php ?
NullPointer's Avatar
Will Code for Food

Posts: 2,815
Name: Matt
Location: Irvine, CA
Trades: 0
Your variables must be defined before you call them. You call $country near the beginning of your function, but you don't define it until the end.
__________________

Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
NullPointer is offline
Reply With Quote
View Public Profile Visit NullPointer's homepage!
 
Old 06-18-2009, 02:32 PM Re: Displaying value from a Drop Down Menu ( form ) using php ?
maxxximus's Avatar
Extreme Talker

Posts: 219
Name: Rob
Location: UK
Trades: 0
Quote:
Originally Posted by NullPointer View Post
Your variables must be defined before you call them. You call $country near the beginning of your function, but you don't define it until the end.
Shirly its because the OP is trying to use a variable that's in the global scope, within a function(local scope). Import it into local scope with $GLOBALS['country']

PHP Code:
$link '<a href="http://ax.phobos.apple.com.edgesuite.net/WebObjects/MZStoreServices.woa/wa/wsSearch?term='.$term.'&limit=1&entity=musicTrack&country='.$GLOBALS['country'].'">Download</a>'
maxxximus is offline
Reply With Quote
View Public Profile
 
Old 06-18-2009, 03:50 PM Re: Displaying value from a Drop Down Menu ( form ) using php ?
jameswhite's Avatar
Average Talker

Posts: 15
Name: James White
Trades: 0
^ I understand what your saying but it still quotes Notice: Undefined Index ? And the link isn't updated with the country variable.

Current Code:
PHP Code:
<?php
table_add
('Fall Into Place','Apartment');
table_add('Kids','MGMT');
table_add('Closer','Travis');

function 
table_add($title,$artist) {
$term str_replace(' ','+',$title.' '.$artist);
$link '<a href="http://ax.phobos.apple.com.edgesuite.net/WebObjects/MZStoreServices.woa/wa/wsSearch?term='.$term.'&limit=1&entity=musicTrack&country='.$GLOBALS['country'].'">Download</a>';
echo 
'<tr><td>'.$title.'</td><td>'.$artist.'</td><td>'.$link.'</td></tr>';};

?>

<form id="countries_select" name="countries_select" method="post" action="<?php echo $_SERVER['PHP_SELF']?>">
    <form id="countries_select" name="countries_select" method="post" action="<?php echo $_SERVER['PHP_SELF']?>">
<script type="text/javascript">
function submitForm()
{
     document.countries_select.submit();
}
</script>
<select name="countries" id="countries" onchange="submitForm();">
    <option value="US" selected="selected">Select Country</option>
    <option onchange="submitForm();" value="US" input type="submit">United States</option>
    <option onchange="submitForm();" value="GB">United Kingdom</option>
    <option onchange="submitForm();" value="ES">Spain</option>
    <option onchange="submitForm();" value="DE">Germany</option>
    <option onchange="submitForm();" value="NL">Netherlands</option>
    <option onchange="submitForm();" value="FR">France</option>
    <option onchange="submitForm();" value="IT">Italy</option>
    <option onchange="submitForm();" value="PL">Poland</option>
    <option onchange="submitForm();" value="MX">Mexico</option>
    <option onchange="submitForm();" value="PT">Portugal</option>
    <option onchange="submitForm();" value="BR">Brazil</option>
</select>
  
<label>
<?php
    $country 
false;
if (isset(
$_POST['countries'])){
    
$country $_POST['countries'];    
echo 
'Country Selected: '.$country'<br />';

}
?>
Thanks again
jameswhite is offline
Reply With Quote
View Public Profile
 
Old 06-18-2009, 04:35 PM Re: Displaying value from a Drop Down Menu ( form ) using php ?
NullPointer's Avatar
Will Code for Food

Posts: 2,815
Name: Matt
Location: Irvine, CA
Trades: 0
There is no global variable country, there is however a global countries (more specifically a variable in POST).

Try:

PHP Code:
<?php 
table_add
('Fall Into Place','Apartment'); 
table_add('Kids','MGMT'); 
table_add('Closer','Travis'); 

function 
table_add($title,$artist) { 
$term str_replace(' ','+',$title.' '.$artist); 
$link '<a href="http://ax.phobos.apple.com.edgesuite.net/WebObjects/MZStoreServices.woa/wa/wsSearch?term='.$term.'&limit=1&entity=musicTrack&country='.$_POST['countries'].'">Download</a>'
echo 
'<tr><td>'.$title.'</td><td>'.$artist.'</td><td>'.$link.'</td></tr>';}; 

?> 

<form id="countries_select" name="countries_select" method="post" action="<?php echo $_SERVER['PHP_SELF']?>"> 
    <form id="countries_select" name="countries_select" method="post" action="<?php echo $_SERVER['PHP_SELF']?>"> 
<script type="text/javascript"> 
function submitForm() 

     document.countries_select.submit(); 

</script> 
<select name="countries" id="countries" onchange="submitForm();"> 
    <option value="US" selected="selected">Select Country</option> 
    <option onchange="submitForm();" value="US" input type="submit">United States</option> 
    <option onchange="submitForm();" value="GB">United Kingdom</option> 
    <option onchange="submitForm();" value="ES">Spain</option> 
    <option onchange="submitForm();" value="DE">Germany</option> 
    <option onchange="submitForm();" value="NL">Netherlands</option> 
    <option onchange="submitForm();" value="FR">France</option> 
    <option onchange="submitForm();" value="IT">Italy</option> 
    <option onchange="submitForm();" value="PL">Poland</option> 
    <option onchange="submitForm();" value="MX">Mexico</option> 
    <option onchange="submitForm();" value="PT">Portugal</option> 
    <option onchange="submitForm();" value="BR">Brazil</option> 
</select> 
   
<label> 
<?php 
    $country 
false
if (isset(
$_POST['countries'])){ 
    
$country $_POST['countries'];     
echo 
'Country Selected: '.$country'<br />'

}
?>
Just to reiterate, your error reporting is not set to the default. Notices can be useful, but your script can function even if notices occur.
__________________

Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE

Last edited by NullPointer; 06-18-2009 at 04:43 PM..
NullPointer is offline
Reply With Quote
View Public Profile Visit NullPointer's homepage!
 
Reply     « Reply to Displaying value from a Drop Down Menu ( form ) using php ?
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.54039 seconds with 12 queries