I want to pass a combo box value from url in php. I have no idea how to do it. I just try like this.
This my combo box html code.

 <select id="ID1" name="place" >
<option value="Colombo" selected="selected">Colombo</option>
<option value="Matara">Matara</option>
<option value="Galle">Galle</option>
</select>

//What i want is pass selected value through URL

   <div style="float:right; padding-right:1px; padding-bottom:2px;"> <a href="MyCity.php?city1= <?php echo $_GET["place"] ; ?> > Click here </a></div>

I'm bit new to php and what is wrong with this code?
im sure that 'MyCity.php' coding is correct, because i pass, hard coded value throgh above URL and could get that value from MyCity.php

Recommended Answers

All 2 Replies

Member Avatar for diafol

This depends if you want the link to appear in the same page and shown interactively on selection or whether the link resides on a different page or only needs to appear after form submission.

If the first case is true, you can do that from simple js (or jQuery):

Something like this (not tested):

<!-- set a default value for the link -->
<div style="float:right; padding-right:1px; padding-bottom:2px;"> <a class="city" href="MyCity.php?city1=Colombo" > Click here </a></div>

<!-- just before the close body tag -->
<script src="//ajax.googleapis.com/ajax/libs/jquery/2.0.0/jquery.min.js"></script>
<script>
    $('ID1').change(function(){
        city = $(this).val();
        $('.city').attr('href', 'MyCity.php?city1=' + city);
    });
</script>

Otherwise, you have a choice with regard to how you pass the data - GET or POST. Forms, other than search forms usually use the POST method.

<form action="yourdestinationfile" method="get">

<form action="yourdestinationfile" method="post">

The first example will show the chosen city in the url, so you can pick up the value with:

$_GET['city1'];

Otherwise you pick up the value with:

$_POST['city1'];

A couple of typos in your code:

 <div style="float:right; padding-right:1px; padding-bottom:2px;"> <a href="MyCity.php?city1= <?php echo $_GET["place"] ; ?> > Click here </a></div>

Should be:

<div style="float:right; padding-right:1px; padding-bottom:2px;"> <a href="MyCity.php?city1=<?php echo $_GET["place"]; ?>"> Click here </a></div>

If the code you posted is currently all on one the same page, it is logical why it isn't working at the moment: you cannot read your $_GET if it has not been set. That means that if your select is changed right now, it does not set your $_GET immediately; only after the form in which your <select> resides is submitted, you will be able to use it.

If this is all your code on one and the same page:

<select id="ID1" name="place" >
    <option value="Colombo" selected="selected">Colombo</option>
    <option value="Matara">Matara</option>
    <option value="Galle">Galle</option>
</select>

//What i want is pass selected value through URL
<div style="float:right; padding-right:1px; padding-bottom:2px;">
    <a href="MyCity.php?city1= <?php echo $_GET["place"] ; ?> > Click here </a>
</div>

Nothing will happen, because the line in which you are asking for $_GET['place'], it has not yet been set if no form has been submitted.

What you could do to make it work is, indeed, like diafol says, make your link change dynamically using Javascript or make it so that a form is submitted so that your $_GET data is accessible. As he also says, note that

<a href="MyCity.php?city1= <?php echo $_GET["place"] ; ?> >

will not work, as there are spaces before and after your <?php ?> part (and the closing double quotes (") are missing).

Diafols example is great, but I would still like to provide another example to you of how you could use a form to make this work (sorry diafol, not that your answer is not good, it's just to explain it even more clearly so that he'll hopefully understand it easier ^^):

File 1 (form.php, for example):

<form action="destination.php" action="post">
    <select id="ID1" name="place">
        <option value="Colombo" selected="selected">Colombo</option>
        <option value="Matara">Matara</option>
        <option value="Galle">Galle</option>
    </select>
</form>

I have set the form's action to "post", as we are submitting data here, not retrieving it (it's not really a MUST, using POST for this, but it is the way I like to do it).

File 2 (destination.php):

<?php
// Get the data that was submitted through the form.
$place = $_POST['place'];
?>

<div style="float:right; padding-right:1px; padding-bottom:2px;">
    <a href="MyCity.php?city1=<?php echo $place; ?>">Click here</a>
</div>

I hope this helps in understanding it better :).

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.