Page 1 of 1
Define variable through list of options
Posted: Wed Jun 17, 2009 4:59 pm
by guyver4mk
Hello,
I was wondering if anyone knew if it is possible to define a variable, depending on what another variable is?
This isn't too self explanitory, so let me elaborate.
if i have a set of options, like so;
Code: Select all
<select name="$option" id="$option">
<option value="option1">Some Text</option>
<option value="option2">Some Text</option>
<option value="option3">Some Text</option>
</select>
is there any way to define another variable dependant on what is selected. i.e say if the type of option was to vary on what was selected?
I'm trying IF statements, but none seem to work.
Any help would be appreciated.
Re: Define variable through list of options
Posted: Wed Jun 17, 2009 6:27 pm
by califdon
What language are you using? You posted this in the PHP - Code forum, but all you are showing is HTML. You are aware, I assume, that you can't use PHP, which executes in the web server, to make choices based on what the user does later in the browser, after the PHP script has finished. If you want a variable to be set based on the user doing anything, that will have to be done in Javascript (or by sending the entire form back to the server as another request).
Re: Define variable through list of options
Posted: Thu Jun 18, 2009 12:16 pm
by guyver4mk
Indeed your assumption holds no water. Funnily enough, yes I am familiar with php and have done a lot of work with it, hense why I posted in a php code forum. My question was based on HTML, meaning I need the values of a selected option to reflect the value of a subsequent variable.
Although I posted the script in the form of HTML, this was for simplicity's sake; the corresponding php would be
Code: Select all
<?php
echo ("
<select name=\"$option\" id=\"$option\">
<option value=\"option1\">Some Text</option>
<option value=\"option2\">Some Text</option>
<option value=\"option3\">Some Text</option>
</select>");
?>
Hense the "$option" as the name.
What I am trying to do is parse the details from the $option to reflect another object's value.
I don't mind using Javascript, I just find it tedious if I have to build a script full of arrays.
An IF statement implementing;
Code: Select all
if(document.formname.option.value == "option1"){
document.formname.type.value = "This"
}
Else { document.formname.type.value = "That"}
Would suffice, but they don't ever seem to want to work for me.
I am grateful for your reply, and you have sparked a lightbulb in the back of my mind if I can't get an IF statment that works in realtime.
But please, just because you have over 2000 posts, don't condescend me, or my programming competence.
Re: Define variable through list of options
Posted: Thu Jun 18, 2009 12:34 pm
by califdon
I'm sorry you took my comments to be condescending. I had no such intention. I just read what is posted and try to respond to it.
guyver4mk wrote:What I am trying to do is parse the details from the $option to reflect another object's value.
I don't mind using Javascript, I just find it tedious if I have to build a script full of arrays.
An IF statement implementing;
if(document.formname.option.value == "option1"){
document.formname.type.value = "This"
}
Else { document.formname.type.value = "That"}
Would suffice, but they don't ever seem to want to work for me.
I am grateful for your reply, and you have sparked a lightbulb in the back of my mind if I can't get an IF statment that works in realtime.
I'm just not understanding what you're trying to do. When you say "realtime," I assume you mean when the user is interacting with the web page. Thus, my comment that only Javascript, a client-side language, can detect the user's actions and make something happen based on those actions. Since you are experienced, you already know this, but I had no way of knowing that from your first post.
I'm afraid I don't follow your statement, "parse the details from the $option to reflect another object's value." Can you be more specific about what it is you need to do? The only thing that comes to mind is, in generating the HTML code for a <select> form element, the values of the <options> are often generated dynamically by PHP, for example, from a database. Is that the sort of thing you're after? I would like to help you, but I can't if I don't understand your objective.
Re: Define variable through list of options
Posted: Fri Jun 19, 2009 6:19 am
by guyver4mk
Thats okay, thats the problem with making your first post and putting out an incomprehendible question.
Okay, my objective sounds simple, but believe me, its not. I've tried for 2 weeks to get this working on my own before asking for help.
I am making an online store, but rather than specifying quantities to a single pre determined price, the quantity determines the price.
So if a customer has say a denomination of 30 of an item, the price would be £50.
I am using a select dropdown menu in order to highlight to the customer that if they select this option, the quantity would be 30 and the price £50. The price is in a seperate textbox, but no matter what I try, whether Javascript or php, I cant seem to make the select dropdown menu dictate the value of the price textbox.
I hope this explains it a little bit better.
Re: Define variable through list of options
Posted: Fri Jun 19, 2009 12:36 pm
by califdon
guyver4mk wrote:I am using a select dropdown menu in order to highlight to the customer that if they select this option, the quantity would be 30 and the price £50. The price is in a seperate textbox, but no matter what I try, whether Javascript or php, I cant seem to make the select dropdown menu dictate the value of the price textbox.
Once again, by the time the user sees the page and can make a selection, there is no PHP code, since that ran in the server before the page was sent to the browser. Thus, the solution can only be found with Javascript code, which runs in the browser. Probably you are having a problem with obtaining the value of the user's selection, that's something I've frequently struggled with. I just created this demo, which should get you where you need to go. Run this and see if this is the sort of behavior you want.
Code: Select all
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=windows-1250">
<meta name="generator" content="PSPad editor, http://www.pspad.com">
<title></title>
<script type='text/javascript'>
function fillbox(val) {
document.getElementById("result").value = val
return True
}
</script>
</head>
<body>
<form method='post' action=''>
<select name='choice'>
<option value='1' onClick='fillbox(this.value);'>One</option>
<option value='2' onClick='fillbox(this.value);'>Two</option>
<option value='3' onClick='fillbox(this.value);'>Three</option>
</select>
<input id='result' name='result' type='text' value='' size='2'>
</form>
</body>
</html>
Re: Define variable through list of options
Posted: Sat Jun 20, 2009 12:13 pm
by guyver4mk
Hi califdon, I ran the script but nothing happened.
I've been playing with something else that has the exact behavior I am looking for, I just can't get an IF statement that works to finish the job
Take a look at this,
Code: Select all
<html>
<head>
<script language="text/Javascript" type="text/javascript">
function showSelected()
{
var selObj = document.getElementById('quantnew');
var txtIndexObj = document.getElementById('txtIndex');
var txtValueObj = document.getElementById('txtValue');
var txtTextObj = document.getElementById('txtText');
var txtpriceObj = document.getElementById('price');
var txtprice1Obj = document.getElementById('txtprice1');
var txtprice2Obj = document.getElementById('txtprice2');
var txtprice3Obj = document.getElementById('txtprice3');
var selIndex = selObj.selectedIndex;
txtIndexObj.value = selIndex;
txtValueObj.value = selObj.options[selIndex].value;
txtTextObj.value = selObj.options[selIndex].text;
txtpriceObj.value = selObj.options[selIndex].value;
txtpriceObj.value = selObj.options[selIndex].value;
txtprice1Obj.value = document.getElementById('price1').value;
txtprice2Obj.value = document.getElementById('price2').value;
txtprice3Obj.value = document.getElementById('price3').value;
}
if (selObj.options[selIndex].value == "30")
{
txtpriceObj = 'txtprice1Obj.text';
}
else if (selObj.options[selIndex].value == "90")
{
txtpriceObj = 'txtprice2Obj.text';
}
else if (selObj.options[selIndex].value == "180")
{
txtpriceObj = 'txtprice3Obj.text';
}
else {""}
</script>
</head>
<body>
<INPUT Type="text" id="price1" class="field" value="$row[140]">
<INPUT Type="text" id="price2" class="field" value="$row[141]">
<INPUT Type="text" id="price3" class="field" value="$row[142]">
<select id="quantnew" onchange="showSelected();">
<option value="30">30 Pairs £53</option>
<option value="90">90 Pairs £162</option>
<option value="180">180 Pairs £310</option>
<option value="val3">sea three</option>
<option value="val4">sea four</option>
</select>
<input type="button" value="Show Index" />
</p>
<p>
<input type="text" id="txtIndex" />
selectedIndex
<br />
<input type="text" id="txtValue" />
options[].value
<br />
<input type="text" id="txtText" />
options[].text
<br />
<input type="text" name="price" />
price
<br />
</p>
</form>
</body>
</html>
If you run this script, upon changing the ddmenu, the values below change. I just need to get th IF statement so that it can Populate the "price" field accordingly.
Any help with that would be great. It just seems that no matter what I try, the code either won't read the "30", or will not replace it in the "price" text box with the appropriate price1 price2 price3 field. (I know they are still formatted as PHP variables, they retrieve the price from an sql database upon the page loading so that when the value of "price" is replaced, it will display the correct price for that quantity.)
Re: Define variable through list of options
Posted: Sat Jun 20, 2009 5:26 pm
by califdon
Why don't you just use the price as the value of each selection and get rid of 80% of your code, while making it more general and easier to maintain?
The code I posted works fine for me and illustrates what I just said about using what you're after as the value of each selection.
Re: Define variable through list of options
Posted: Sun Jun 21, 2009 3:27 am
by guyver4mk
It's weird you've said that, I had a small revelation this morning and thought why not just kill of the quantity altogether and just define the price, as the quantity will be listed in the description anyway.
I'm going to try this radical new approach, and I'll let you know how i get on lol.
Re: Define variable through list of options
Posted: Tue Jun 23, 2009 7:37 am
by guyver4mk
Hi Califdon.
I have gone back and rebuilt the drop down menu and loe and behold 2 weeks of testing, rebuilding, testing and research are now at an end! I've finally got the menu working.
Thanks for you patience and assistance over th past week. I appreciate it.
Regards.
Re: Define variable through list of options
Posted: Tue Jun 23, 2009 12:09 pm
by califdon
You're welcome, and thanks for being thoughtful enough to return to acknowledge the help. If you think we can be of help again, just drop in again--or just browse the forums from time to time. I learn a lot just by scanning the topic subjects and reading those that I find interesting.