1

In my script I have the following:-

<script>
      $("#country").on("change", function(){
        var selected = $(this).val();
        $("#results").html("<div class='alert-box success'><span><img src='images/shipping_ukrm.jpg'></span> <b>&pound" + selected + " plus Packaging</b></div>");
		$('#frmelement').val(selected);
      })
    </script>

I select the country which in turn gets the value of the option value in dropdown select field, but I want to store the dropdown value which is country name in a variable to pass and use within a form

Anyone help

This is the select dropdown box information

echo '<select name="country" id="country" class="span5" />';
	echo '<option value="0" selected>Select Country</option>';
	mysql_connect("$host", "$username", "$password")or die("cannot connect");
	mysql_select_db("$db_name")or die("cannot select DB");
	$sql1 = "SELECT c.country_id,c.name,pr.price,pr.weight,pr.weight1,pr.class,c.Zone FROM country as c LEFT JOIN postagerates as pr ON pr.class = c.Zone WHERE (pr.weight <= '$totweight' AND pr.weight1 >= '$totweight') ORDER BY c.name ASC";
	$query1 = mysql_query($sql1);
	while ($row2 = mysql_fetch_array($query1)) { 
	echo '<option value = "'.money_format('%.2n', $row2["price"]).'">'.$row2["name"].'</option>';
	}
		echo '</select>

0

2 Answers 2

1

Try this:

var selected = $(this).find('option:selected').text()

See Fiddle

Sign up to request clarification or add additional context in comments.

3 Comments

ok, that gets the selected text, how do you store this as a variable and can use it and display the data in that variable
My above code is already storing the chosen country name in the selected variable. I cannot know in advance where and how exactly you want to further show this variable data. You need to elaborate more.
Well I have taken the selected variable, transferred it into an input text field which is hidden and on processing of the form taken the input text value and stored in a session variable and now able to use it as and when.
0

Here is code :

     echo '<select name="country" id="country"  />';
     echo '<option value="0" selected>Select Country</option>';
     echo '<option value = "'.money_format('%.2n', $row2["price"]).'">'.$row2["monday"].'</option>';
 }
 echo '</select>

1 Comment

I suggest you to add some text to descrive your solution. This will make your answer more helpful (and more likely to be upvoted ;) ).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.