Jump to content

Splitting for dropdown into 2 variable


dropbop

Recommended Posts

Hi,

 

I have a form with a dropdown

<select name="county" id="county">
  <option selected="selected">Choose A County</option>
  <option value="1">Dublin</option>
  <option value="2">Westmeath</option>
  <option value="3">Galway</option>
  <option value="4">Roscommon</option>
</select>

 

What I am trying to do is seperate for example the value '1' and the option 'Dublin'.

 

The results are passed to hidden field on a second form page.

This is what I have, but it doesnt seem to be working...

 

<input type="hidden" name="county" value="<?php echo ($_POST['county']); ?>" />
<input type="hidden" name="county1" value="<?php echo ($_POST['county']); ?>" />

 

so I was wondering if anyone might be able to give me a pointer in the right direction.

 

I have tried using an array on the processing page to split it but its not working either.. like this:

 

$county = (float) $_POST['county'];
$county1 = in_array($county, array(1, 2, 3, 4)) ? $county : 5;

$county1 = $_POST['1']='Dublin';
$county1 = $_POST['2']='Westmeath';
$county1 = $_POST['3']='Galway';
$county1 = $_POST['4']='Roscommon';
$county1 = $_POST['5']='Cork';

 

I have been trying to get this to work for a few days and I have searched everywhere for the answer, but I can't find it anywhere. I would be so grateful if someone could help me with this and it the last part I need to do before I move on to the next part of my project.

 

Thanking you

 

Eoin

 

 

Link to comment
Share on other sites

Hi Eoin,

 

The problem is that the only thing that the form passes is the contents of the value="" attribute. The Actual name of the county is nothing more than a label so your users can select something meaningful.

 

There is an easy way around this.

 

Try adding both the ID and the label to the value like-

<select name="county" id="county">
  <option selected="selected">Choose A County</option>
  <option value="1,Dublin">Dublin</option>
  <option value="2,Westmeath">Westmeath</option>
  <option value="3,Galway">Galway</option>
  <option value="4,Roscommon">Roscommon</option>
</select>

 

Now the data passed will have both the ID and County name.

To seperate them use explode() like-

$County = $_POST['county'];
$CountyParts = explode(",", $County );
$CountyID = $CountyParts[0];
$CountyName = $CountyParts[1];

 

Give that a whirl.

 

Link to comment
Share on other sites

When the first form is submitted $_POST['county'] should only contain the value of the selected item from the drop down. So you could use switch to determine which county was selected.

 

<?php
...

switch($_POST['county']) {
     case '1': $county='Dublin'; break;
     case '2': $county='Westmeath'; break;
     ...
     default: $county='';
}

...
?>

 

 

Then later on when you're creating the second form, make sure you're using the newly created variable ($county).

Link to comment
Share on other sites

Try adding both the ID and the label to the value like-

<select name="county" id="county">
  <option selected="selected">Choose A County</option>
  <option value="1,Dublin">Dublin</option>
  <option value="2,Westmeath">Westmeath</option>
  <option value="3,Galway">Galway</option>
  <option value="4,Roscommon">Roscommon</option>
</select>

 

 

A solution like this would work, but keep in mind that it will be more difficult to sanatize the data.

 

With that said my solution would be different too. Is there a reason the hidden field in second form needs to be populated with the county name? Couldn't you keep passing the number? Then you would just use the switch method to get the county name in the end.

Link to comment
Share on other sites

@cyberRobot You could but that means you have to maintain two separate lists of counties whereas the explode version only requires one.

 

Just thinking about extensibility.

 

 

Depends on how the PHP script is set up. For example, if the entire script (all the forms and results script) are all contained in the same page you could keep the list of counties in an array. Or you import the counties from a database or some kind of external file. In the end you would have one list use throughout the entire process.

Link to comment
Share on other sites

 

Is there a reason the hidden field in second form needs to be populated with the county name? Couldn't you keep passing the number? Then you would just use the switch method to get the county name in the end.

 

Hi, yes the number of the county is used for a categoryID in the database and the name of the country needs to be shown on the websites front end.

 

The database is of a Zen Cart site that the info is inserted into. So if someone chooses Dublin for instance, then the details are inserted in the Dublin category with a catagory ID of the master category '1'.

 

There is an email sent to the person who filled in the form showing all the details they filled in including the county. Also when the form is submitted, it creats a txt file which is actually a download product that contains the full details of what was submitted in the form...

 

Im not sure if im explaining myself very well here but I will try and shorten it down for you.

 

A person is looking for quotes for their timber decking project...

1) the person fills in the form with the personal and project details

2) the form is submitted and the details are sent in 3 ways

  • job details inserted into database
    full details emailed to person looking for quote
    txt file created as a downloadable attribute which can be purchased by contractors (this file contains the full details also, which inludes the county in the address part)

3) The job details are on display on zen cart front end as products

4) Contractors purchase the products (leads) and download the full details so they can contact the potential client and provide a quote.

 

If i am confusing anyone, please let me know because I think I am starting to confuse myself lol

 

here it is if you need to see it for yourself  www.timberdecks.ie  and the zencart where the contractors will purchase leads is here www.timberdecks.ie/trades

 

I will try what you guys have suggested so far and see if any works, hopefully it will and I will be able to move on to the next stage.

 

Thanks all for your replies so far, very much appreciated indeed.

 

Eoin

 

 

 

 

Link to comment
Share on other sites

I am back again after trying all day again with no joy. I have tryied the above but its just not working.

 

I though that if I used an if statement to do it, that it might work but it only seems to be outputting the first one ie. 1 - Dublin

 

Maybe im doing it wrong.

 

$county=$_POST['county'];

if ($county = "1") {
$countyname = "Dublin";
}
elseif ($county = "2") {
	$countyname = "Westmeath";
}
elseif ($county = "3") {
	$countyname = "Galway";
}

Link to comment
Share on other sites

You need to use == for comparison; otherwise you're just assigning a value.

 

YEAH!!! Finally got it to work!!!

 

Really appreciate the help guys... Although they were not the solutions I needed for this particular bit, I have learned a bit more today.

 

A simple little 'if' was the man for the job.

 

Thanks again guys for taking the time to help me, it wont go unforgotten.

 

Eoin

 

PS: I dont see any Thank You or Helped buttons or I would have been clicking the whole way down the thread :)

 

 

Link to comment
Share on other sites

No problem, glad it works  :D

 

Note that a switch() structure could easily replace the if/else solution mentioned above and it will save on typing. Might be worth looking into:

http://php.net/manual/en/control-structures.switch.php

 

For now I just want to move on to the next part, I will however be going back over most of the code again near the end to give it all a good clean up.

 

Im learning so much from this forum, have been popping in and out for a good while, and really glad of the few bits of help I got from it so far.

 

Thanks again guys

 

Eoin

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.