walking through php array - php

Can anyone point me in the right direction with the below
I have this HTML in a FROM (simplified for this example):
=============== Start HTML ===================
<select name="Postage[]" id="Unique-ID">
<option value="1">Postage Option 1</option>
<option value="2">Postage Option 2</option>
<option value="3">Postage Option 3</option>
</select>
<input name="PostagePrice[]" id="Price-Unique-ID" value="" />
<select name="Postage[]" id="Unique-ID">
<option value="1">Postage Option 1</option>
<option value="2">Postage Option 2</option>
<option value="3">Postage Option 3</option>
</select>
<input name="PostagePrice[]" id="Price-Unique-ID" value="" />
<select name="Postage[]" id="Unique-ID">
<option value="1">Postage Option 1</option>
<option value="2">Postage Option 2</option>
<option value="3">Postage Option 3</option>
</select>
<input name="PostagePrice[]" id="Price-Unique-ID" value="" />
================== End HTML =====================
I am collecting this from the form and setting it to an array ($arrPostageOptions) with the below code:
=============== Start PHP ===================
if (isset($_POST['Postage'])) {
if (is_array($_POST['Postage'])) {
$n = count($_POST['Postage']);
for ($i = 0; $i < $n; ++$i) {
// check to only collect completed entries
if( !empty($_POST['Postage'][$i]) && !empty($_POST['PostagePrice'][$i]) ){
// assign to array
$arrPostageOptions[$i] = array( "Postage" => $_POST['Postage'][$i], "PostagePrice" => $_POST['PostagePrice'][$i], );
}
}
}
}
=============== End PHP ===================
This results in the following array
============ array result ===============
Array(
[0] => Array
(
[Postage] => 1
[PostagePrice] => 12
)
[1] => Array
(
[Postage] => 2
[PostagePrice] => 24
)
[2] => Array
(
[Postage] => 3
[PostagePrice] => 48
)
)
============ end array result ===============
The answer (I think) I want is for each array is :
1 & 12
2 & 24
3 & 48
(that the array numbers match the postage numbers is just coincidence, I only want ‘Postage’ and ‘PostagePrice’)
I am trying the following:
=========== start of what I am trying ==========
foreach ($arrPostageOptions as $id) {
while ($id) {
$Postage = $id["Postage"];
$PostagePrice = $id["PostagePrice"];
// echo $Postage.' '.$PostagePrice.'<br>';
unset($id);
if (!$listing_obj->addPostageOptions($ListingID, $PostageOptionID, $PostagePrice, $db)) {
$err_text .= 'An error occurred adding Postage Option'.$PostageOptionID;
}
}
}
====== end of what I am trying ==============
Unfortunately this is not working as I expect (but is the closest I have gotten) . I have also tried multiple foreach loops, but again I don’t get what im after
(unsettling the $id is stopping the loop from continuing for ever, but is also giving me PHP notices of Undefined variable $id)
I’m sure I know how to do this, but seem to have forgotten and have possible over complicated it as these arrays are still confusing the s**t out of me.
Any advice would be greatly welcomed!

Why not simply:
foreach ($arrPostageOptions as $postageOption) {
$postage = $postageOption["Postage"];
$postagePrice = $postageOption["PostagePrice"];
echo $postage.' '.$postagePrice.'<br>';
...
}
Sorry for renaming, but $id was too confusing for me.

I'm not entirely sure I understood what you want to do but it would seem to me that you want to iterate the array like this, but I'm not sure.
foreach ($arrPostageOptions as $id=>$array) {
foreach($array as $key=>$val){
// do your stuff here using where you can also reference $id
}
}

there are a couple of things I would like to say is that the isset function is a bit pointless here and it is best used along with a submit button, not for text fields and other inputs.
Also, you are declaring your array inside the loop so accessing it anywhere outside will give an undefined error.
I just used your form and updated the php script slightly to give the output you desire. Please have a look.
Your form
<form action="yourscript.php" method="POST">
<select name="Postage[]" id="Unique-ID">
<option value="1">Postage Option 1</option>
<option value="2">Postage Option 2</option>
<option value="3">Postage Option 3</option>
</select>
<input name="PostagePrice[]" id="Price-Unique-ID" value="" />
<select name="Postage[]" id="Unique-ID">
<option value="1">Postage Option 1</option>
<option value="2">Postage Option 2</option>
<option value="3">Postage Option 3</option>
</select>
<input name="PostagePrice[]" id="Price-Unique-ID" value="" />
<select name="Postage[]" id="Unique-ID">
<option value="1">Postage Option 1</option>
<option value="2">Postage Option 2</option>
<option value="3">Postage Option 3</option>
</select>
<input name="PostagePrice[]" id="Price-Unique-ID" value="" />
<input type="submit" value="submit" name="submit"/>
</form>
I added a submit button and a the method and action in the header just to make it a bit more clear.
And here is the script I used, pretty similar to yours
<?php
$arrPostageOptions = array();
if(isset($_POST['submit']))
{
$postage = $_POST['Postage'];
$postagePrice = $_POST['PostagePrice'];
$array_size = count($postage);
for($i = 0; $i < $array_size; $i++)
{
if(!empty($postage[$i]) && !empty($postagePrice[$i]))
{
$arrPostageOptions[$i] = array("Postage"=>$postage[$i],
"PostagePrice"=>$postagePrice[$i]);
}
}
print_r($arrPostageOptions);
}
?>
I check if the submit button is set/clicked and enter the loop. I define the array variable outside so it can be used anywhere in the program.
If you see the print_r result, you will see the array in the format you desire.
I hope I answered a couple of your questions.
At the same time, its better to always put your POST data into variables before using them so you can check them and control them better than directly using $_POST['name']. This will be of more use when you work with databases and other related things.
Hope this helps.
Thanks,
Shawn.

The while within the foreach is redundant. foreach will only continue as long as there is a new entry to become $id anyhow.
The loop should be fine if you remove the while. If you want to test whether the data is valid, test on the array keys themselves like by putting this in the loop
if(!$id["Postage"] || !$id["PostagePrice"]){
continue;
}

Related

PHP : Get Multi Select Box value as comma separated value

Hi I have a multi select box as below
HTML
<form action="c3.php" method="post">
<select name="ary[]" multiple="multiple">
<option value="Option 1" >Option 1</option>
<option value="Option 2">Option 2</option>
<option value="Option 3">Option 3</option>
<option value="Option 4">Option 4</option>
<option value="Option 5">Option 5</option>
</select>
<input type="submit">
</form>
I need to get the values selected by user as comma separated.
eg. if user selects Option1 and Option4 I need to read that as Option1,Option4
if he is selecting Option1 only .It should return as Option1 (no comma)
I have a code like this
PHP
foreach ($ary as $a){
//echo $a;
$com_values = implode(",", array_filter([$a])) ;
}
but the above php code is giving me only one value it is not giving me comma separated values /Any issues ?
You need to implode on the actual array, looping over the array is only going to give you the one value in the loop.
$com_values = implode(",", $_POST["ary"]);
Should give you what you need.

How to send input values along with select option to php

I have this following codes.How do i send this form to php so in php i can access it like studentName course2 year3.Basically i have a table which looks like this studentName|course|year.I am looking for the right way to organize the data in order to insert it to my table.I have tried sending the form not as an array but i have to write 3 queries to insert 3 inputs.Is there any way to loop through the data and insert?
<form id="myform" method="POST">
<label>college:</label><input type="text" id="clg" name="clg"/><br/>
<label>Student:-</label><input type="text" name="name[]" id="name" />
<select name="taskOpen[]">
<option value="1">course1</option>
<option value="2">course2</option>
<option value="3">course3</option>
<option value="4">course4</option>
</select>
<select name="year[]">
<option value="1">year1</option>
<option value="2">year2</option>
<option value="3">year3</option>
<option value="4">year4</option>
</select>
<label>Student:-</label><input type="text" name="name[]" id="name" />
<select name="taskOpen[]">
<option value="1">course1</option>
<option value="2">course2</option>
<option value="3">course3</option>
<option value="4">course4</option>
</select>
<select name="year[]">
<option value="1">year1</option>
<option value="2">year2</option>
<option value="3">year3</option>
<option value="4">year4</option>
</select>
<input type="button" value="search" id="btn"/>
</form>
Here is what i have in php--
if(isset($_POST['name']) && isset($_POST['taskOpen'])
&& isset($_POST(['year'])){
$name=$_POST['name'],$course=$_POST['taskOpen'],$year=$_POST['year']
}
else {
echo "something";
}
foreach($name as $student){
$sql=$conn->prepare("INSERT INTO `student`(`studentName`) VALUES(:s_n)");
$sql->execute(array(":s_n"=>$student));
}
foreach($course as $courseN){
$sql=$conn->prepare("INSERT INTO `student`(`course`) VALUES(:co)");
$sql->execute(array(":co"=>$courseN));
}
foreach($year as $yearD){
$sql=$conn->prepare("INSERT INTO `student`(`year`) VALUES(:ya)");
$sql->execute(array(":ya"=>$yearD));
}
Here is what i think you are going for...
HTML
<!-- THE NAMES OF INPUT ELEMENTS WRAPPED IN A college_data-->
<form id="myform" method="POST">
<label>college:</label><input type="text" id="clg" name="college_data[clg]"/><br/>
<label>Student:-</label><input type="text" name="college_data[name]" id="name"/>
<select name="college_data[taskOpen1]">
<option value="1">course1</option>
<option value="2">course2</option>
<option value="3">course3</option>
<option value="4">course4</option>
</select>
<select name="college_data[year1]">
<option value="1">year1</option>
<option value="2">year2</option>
<option value="3">year3</option>
<option value="4">year4</option>
</select>
<label>Student:-</label><input type="text" name="name[]" id="name"/>
<select name="college_data[taskOpen2]">
<option value="1">course1</option>
<option value="2">course2</option>
<option value="3">course3</option>
<option value="4">course4</option>
</select>
<select name="college_data[year2]">
<option value="1">year1</option>
<option value="2">year2</option>
<option value="3">year3</option>
<option value="4">year4</option>
</select>
<input type="submit" value="search" id="btn"/>
</form>
PHP
<?php
$college = isset($_POST['college_data']['clg']) ? htmlspecialchars(trim($_POST['college_data']['clg'])) :null;
$studentName = isset($_POST['college_data']['name']) ? htmlspecialchars(trim($_POST['college_data']['name'])) :null;
$studentName2 = isset($_POST['college_data']['name2']) ? htmlspecialchars(trim($_POST['college_data']['name2'])) :null;
$taskOpen1 = isset($_POST['college_data']['taskOpen1']) ? htmlspecialchars(trim($_POST['college_data']['taskOpen1'])) :null;
$taskOpen2 = isset($_POST['college_data']['taskOpen2']) ? htmlspecialchars(trim($_POST['college_data']['taskOpen2'])) :null;
$year1 = isset($_POST['college_data']['year1']) ? htmlspecialchars(trim($_POST['college_data']['year1'])) :null;
$year2 = isset($_POST['college_data']['year2']) ? htmlspecialchars(trim($_POST['college_data']['year2'])) :null;
// TEST::: DELETE EVERYTHING BELOW THIS LINE ONCE YOU ARE DONE CONFIRMING THAT IT IS OK
// CLICK THE SUBMIT BUTTON TO SEE THE VALUES REFLECTED HERE AS SOON AS THE FORM GETS SUBMITTED...
var_dump($college);
var_dump($studentName);
var_dump($studentName2);
var_dump($taskOpen1);
var_dump($taskOpen2);
var_dump($year1);
var_dump($year2);
?>
VALIDATION
<?php
// FIRST CREATE AN ARRAY TO HOLD MESSAGES TO BE SHOWN TO THE USER
// SHOULD ANYTHING GO WRONG... & ALSO AN EMPTY ERROR MESSAGE STRING...
$arrErrorBag = array();
$strErrorMsg = "";
if(isset($_POST['submit'])) {
// LET'S ASSUME FOR THE MOMENT YOU HAVE SPECIAL CONSTRAINTS
// TO BE APPLIED TO EACH FIELD DURING YOUR VALIDATION PROCESS.
// WE WILL USE SIMPLE REGULAR EXPRESSIONS HERE FOR DEMONSTRATION
// BUILD VALIDATION REG-EXES FOR THE FIELD:: WE WANT ONLY A MINIMUM OF 10 CHARACTERS FOR STUDENT & COLLEGE
// AGAIN; YOU CAN GO WILD BUT WE ARE KEEPING IT SIMPLE
// PLUS, DOING IT LIKE THIS IS JUST SO THAT YOU GET THE DRIFT... ;-)
$rxColStudent = "#(.){10,}#si";
// FOR THE SELECTS, WE KNOW THAT THE VALUES ARE NUMBERS
// WHICH ARE BETWEEN 1 & 4; BUT WE TAKE IT TO "BETWEEN 1 & 9"
// AND WE INTEND USING ONE REGEX FOR ALL OF THEM SINCE THEY ARE SIMILAR.
$rxCourseYear = "#\d#";
// NOW WE HAVE THE REGEXES: THEN WHAT?
// VALIDATION BEGINS... ;-)
// TO MAKE IT EASIER ON OURSELVES, WE BUNDLE ALL THE STUDENT, STUDENT1 & COLLEGE FIELDS
// INTO AN ARRAY & LOOP THROUGH THE ARRAY, BUILDING THE ERRORS ON EACH ITERATION THROUGH THE LOOP.
// VALIDATE COLLEGE, STUDENT & STUDENT2...
$arrStringValFields = array(
"student" => array("Student Nr. 1", $studentName),
"student2" => array("Student Nr. 2 ", $studentName2),
"college" => array("College", $college),
);
// RUN THE LOOP & KEEP BUILDING THE ERRORS AS YOU VALIDATE THROUGH EACH ITERATION.
foreach ($arrStringValFields as $fieldName => $arrErrStringVal) {
if (!preg_match($rxColStudent, $arrErrStringVal[1])) {
$arrErrorBag[$fieldName] = $arrErrStringVal[0] . " is expected to have a minimum of 10 Characters. Please, check that you have not missed something.";
}
}
// TO MAKE IT AGAIN EASIER ON OURSELVES, WE BUNDLE ALL OTHER FIELDS THAT HAVE NUMERIC VALUES
// INTO AN ARRAY & LOOP THROUGH THE ARRAY, BUILDING THE ERRORS ON EACH ITERATION THROUGH THE LOOP.
$arrNumericFields = array(
"taskOpen1" => array("Open Task Nr. 1", $taskOpen1),
"taskOpen2" => array("Open Task Nr. 2 ", $taskOpen2),
"year1" => array("College Year Section 1", $year1),
"year2" => array("College Year Section 2", $year2),
);
// RUN THE LOOP & KEEP BUILDING THE ERRORS AS YOU VALIDATE THROUGH EACH ITERATION.
foreach ($arrNumericFields as $fieldName => $arrErrStringVal) {
if (!preg_match($rxCourseYear, $arrErrStringVal[1])) {
$arrErrorBag[$fieldName] = $arrErrStringVal[0] . " is expected to be a Single Digit & it should. Did you try to *NUKE OUR ASSES OFF*. Please, don't. We are your Brothers ;-)";
}
}
// MAMA MIA!!! SO MUCH FOR A SIMPLE 6-FIELD VALIDATION...
// HAPPILY ENOUGH; WE ARE THERE... ONLY; NOT JUST YET...
// CHECK THE ERROR BAG TO SEE IF IT CONTAINS ANYTHING.
// IF IT DOES; THEN WE HAVE TO FIND A WAY TO DISPLAY THIS TO THE END-USER.
if (!empty($arrErrorBag)) {
// TURN THE ERROR BAG ARRAY TO A STRING & ASSIGN IT BACK TO THE ERROR MESSAGE STRING FOR DISPLAY...
$strErrorMsg = "<span class='has-error'>" . implode("</span><br /><span class='has-error'>", $arrErrorBag);
}
else {
// WE HAVE REACHED THE CLIMAX OF OUR POLICE-WORK...
// SO WE EITHER STORE THE DATA TO THE DATABASE TABLE OR
// WE BAKE & CAKE IT FOR CHRISTMAS - YOUR CALL ;-)
}
}
?>
Now, that's OK. But how does the User get to know that there were Errors and how does s/he avoid typing-in the same information over again?
Our HTML File should have been built earlier to take that into consideration, but then again, EINTEIN WAS RIGHT!!! Human Stupidity is totally as Infinite as the Universe... but that's why we are here, right? To cut down the infinitude of our Stupidity and grow into real Humans;-) So we revise our HTML to take these into account.
HTML - REVISED: UNLEASH THE DRAGON!!!
<!-- WE ADD A SLOT FOR OUR ERROR MESSAGE: JUST BEFORE THE FORM. -->
<div class="error-slot"><?php echo $strErrorMsg; ?></div>
<form id="myform" method="POST">
<label>college:</label><input type="text" id="clg" name="college_data[clg]" value="<?php echo $college; ?>" /><br/>
<label>Student:-</label><input type="text" name="college_data[name]" value="<?php echo $studentName; ?>" id="name"/>
<select name="college_data[taskOpen1]">
<option value="1" <?php if($taskOpen1=="1"){echo "selected";} ?>>course1</option>
<option value="2" <?php if($taskOpen1=="2"){echo "selected";} ?>>course2</option>
<option value="3" <?php if($taskOpen1=="3"){echo "selected";} ?>>course3</option>
<option value="4" <?php if($taskOpen1=="4"){echo "selected";} ?>>course4</option>
</select>
<select name="college_data[year1]">
<option value="1" <?php if($year1=="1"){echo "selected";} ?>>year1</option>
<option value="2" <?php if($year1=="2"){echo "selected";} ?>>year2</option>
<option value="3" <?php if($year1=="3"){echo "selected";} ?>>year3</option>
<option value="4" <?php if($year1=="4"){echo "selected";} ?>>year4</option>
</select>
<label>Student:-</label><input type="text" name="college_data[name2]" value="<?php echo $studentName2; ?>" id="name"/>
<select name="college_data[taskOpen2]">
<option value="1" <?php if($taskOpen2=="1"){echo "selected";} ?>>course1</option>
<option value="2" <?php if($taskOpen2=="2"){echo "selected";} ?>>course2</option>
<option value="3" <?php if($taskOpen2=="3"){echo "selected";} ?>>course3</option>
<option value="4" <?php if($taskOpen2=="4"){echo "selected";} ?>>course4</option>
</select>
<select name="college_data[year2]">
<option value="1" <?php if($year2=="1"){echo "selected";} ?>>year1</option>
<option value="2" <?php if($year2=="2"){echo "selected";} ?>>year2</option>
<option value="3" <?php if($year2=="3"){echo "selected";} ?>>year3</option>
<option value="4" <?php if($year2=="4"){echo "selected";} ?>>year4</option>
</select>
<input type="submit" value="search" name="submit" id="btn"/>
</form>
From your table design in the database it looks like you want to insert a student, their course and the year they are taking it in. Unless you are going to validate the form so that the number of students matches the number of courses and numbers of years so you can easily loop and insert based on their indexes, i don't see how it can be easily done.
Example:
$_POST = Array(
'name' = Array ( 'Kelly', 'Tom', 'Jack' ),
'course' = Array ( 'Course1', 'Course2', 'Course3' ),
'year' = Array ( '2007', '2008', '2009' ),
);
for ( $i = 0; i < count($_POST['student_name']); i++ ) {
$sql = sprintf("INSERT INTO test ( student_name, course, year ) VALUES ( %s, %s, %d )",
$_POST['student_name'][$i], $_POST['course'][$i], $_POST['year'][$i]
);
$conn->query($sql);
}

How I explode this kind of string in PHP?

I have a dropdown to display price ranges for searching.
HTML for dropdown:
<select name="price">
<option value="">All (-)</option>
<option value="">Rs.400 to Rs.1,000 (3)</option>
<option value="">Rs.1,000 to Rs.2,000 (6)</option>
<option value="">Rs.2,000 to Rs.4,000 (1)</option>
<option value="">Rs.4,000+ (1)</option>
</select>
Using this option values, now I need to separate first and second price from user selection.
Eg. If someone select Rs.400 to Rs.1,000 (3), then I need to get 400 and 1,000.
Can anybody tell me how can I do this in php. I tired it with php explode. but I could not figure this out correctly.
Hope somebody may help me out.
Thank you.
What about something like this:
HTML
<select name="price">
<option value="all">All (-)</option>
<option value="400|1000">Rs.400 to Rs.1,000 (3)</option>
<option value="1000|2000">Rs.1,000 to Rs.2,000 (6)</option>
<option value="2000|4000">Rs.2,000 to Rs.4,000 (1)</option>
<option value="Over">Rs.4,000+ (1)</option>
</select>
PHP
<?php
//Get the data from the form
$price = $_POST['price'];
$prices = explode("|", $price);
?>
For example, if the user selects "Rs.400 to Rs.1,000", the value of $prices will be:
$price[0] = "400";
$price[1] = "1000";
I hope that helps. If it doesn't, I suppose I wasn't clear on what you were asking.
Inspired by a previous answer. I'd suggest.
HTML
<select name="price">
<option value="|">All (-)</option>
<option value="400|1000">Rs.400 to Rs.1,000 (3)</option>
<option value="1000|2000">Rs.1,000 to Rs.2,000 (6)</option>
<option value="2000|4000">Rs.2,000 to Rs.4,000 (1)</option>
<option value="4000|">Rs.4,000+ (1)</option>
</select>
PHP
<?php
function get_numeric($val) {
if (is_numeric($val)) {
return $val + 0;
}
return false;
}
$price_selected = isset($_REQUEST['price']) && trim($_REQUEST['price'])!="" ? $_REQUEST['price'] : "|"; // sets a default value
list($lower, $upper) = array_map('get_numeric',explode('|', $price_selected, 2));
var_dump($lower); // false when there's no lower limit
var_dump($upper); // false when there's no upper limit
?>

multi-dimensional array from forminput

I think my solution is in a multi-dimensional array, but how...
If i have the following (or similar) in a HTML form (simplified for example)
<select name="Postage[]" id="Unique-ID">
<option value="1">Postage Option 1</option>
<option value="2">Postage Option 2</option>
<option value="3">Postage Option 3</option>
</select>
<input name="PostagePrice[]" id="Price-Unique-ID" value="" />
<select name="Postage[]" id="Unique-ID">
<option value="1">Other Postage Option 1</option>
<option value="2">Other Postage Option 2</option>
<option value="3">Other Postage Option 3</option>
</select>
<input name="PostagePrice[]" id="Price-Unique-ID" value="" />
<select name="Postage[]" id="Unique-ID">
<option value="1">Another Postage Option 1</option>
<option value="2">Another Postage Option 2</option>
<option value="3">Another Postage Option 3</option>
</select>
<input name="PostagePrice[]" id="Price-Unique-ID" value="" />
How do i get this stored into a PHP array (so i can later add it to my DB)
So far i have the below, but it is clearly not finnished
if (isset($_POST['Postage'])) {
if (is_array($_POST['Postage'])) {
foreach($_POST['Postage'] as $PostateID=>$PostageOption){
// this is where i am tottally stuck
// need to assosiate postageID with a Postage Option and a PostagePrice
}
}
}
Im sorry to sound dumb but i have not yet had the "Erika" moment with multi-dimensional array's
I would be grateful for any advice
$_POST['Postage'] is not a multidimensional array. You can easily see it if you var_dump($_POST['Postage']); It's just an array of all the selected indexes in your selects:
array(3) {
[0]=>
string(1) "1"
[1]=>
string(3) "1"
[2]=>
string(3) "1"
}
Here is what I input for testing:
And here I get the postages and associated prices, using a for loop:
<?php
$n = count($_POST['Postage']);
for ($i = 0; $i < $n; ++$i)
{
print $_POST['Postage'][$i] . " " . $_POST['PostagePrice'][$i] . "<br>";
}
prints:
1 1
2 2
2 3
Consider your array of inputs as below:
<?
//Let your inputs be Postage1, Price1, Postage2, Price2...
//Then your Received POST will be..
$_POST = Array(
"Postage" => Array( 0 => 'Postage1', 1 => 'Postage2', 2 => 'Postage3'),
"PostagePrice"=> Array( 0 => 'Price1', 2 => 'Price2', 3 => 'Price3')
);
//Now, you can see that index 0 points to a price of POSTAGE and so on 1 and 2..
//Store corresponding values in an Array.
$store = Array();
foreach($_POST['Postage'] as $PostateID=>$PostageOption){
$store[$PostageOption] = $_POST['PostagePrice'][$PostateID];
}
print_r($store);
?>
<?PHP
$final = array_combine($_POST['Postage'], $_POST['PostagePrice']);
?>
By this way, you will associate Postage and PostagePrice in this array.

jQuery change form action based on selected value

ive run into a quite tricky problem while using a method to set the form action when value changes. I have got the following code :
var actions = new Array();
actions[0] = "some-site.html";
actions[1] = "some-other-site.html";
actions[2] = "another-site.html";
etc....
$("select#feld-urlaubsart").change(function () {
if ($("select#fieldname").val() !== "") {
$("form#searchform").attr("action","http://www.mysite.com/" +
actions[$("select#fieldname").val()]);
}
});
On the other side, my select looks like this :
<select id="fieldname" name="fieldname">
<option value="0" selected="selected">All</option>
<optgroup label="Golfurlaub mit Greenfees">
<option value="1">Alle Greenfee-Angebote</option>
<option value="2">Top-Angebote</option>
<option value="3">Golfurlaub mit 4 Tage Greenfee</option>
</optgroup>
Actually it does work but i am not happy with it as i need to rely on the value="1" value="2" etc.
Now i need to use the actual description as value to be compatible with another script that i pass the value to for usage in PHP/JSON. I would need the data like this
<option value="All">All</option>
<option value="Option 1">Option 1</option>
etc
How can i change the action of the form based on the selected VALUE while not needing to use array keys like "1" "2" etc?
Thanks for any advice.
Use a json object:
actions = {"All":"some-site.html","Option1":"some-other-site.html"}
Then
actions["All"]
is
"some-site.html"

Categories