Click to See Complete Forum and Search --> : Javascript /PHP- How to retrieve the value selected after a form is posted ?


jayce
September 23rd, 2005, 04:07 PM
hi there,

The way I proceed is to check my form thru PHP, so I submit the form and with PHP I check if all values have been entered/selected. If somehting is wrong I redisplay the form with the values previously entered/selected

so far my code works perfectly except to display the values from an exisiting arrays
but I would like to improve it in order to know which line(item) was selected after before the post.

So would you have an idea on how to know which line was selected before the post or my select fields ?

Please advise. Thanks,


<script type="text/javascript" language="Javascript">
var countryCode = new Array("","country01","country02","country03");
var countryDesc = new Array("","AUSTRALIA","UNITED KINGDOM","UNITED STATES");

function DisplayParentValues() {
var maxValues=0;
var listValues = document.myForm.country;
maxValues = countryCode.length; //matching the number of lines of this countrycode array
listValues.options.length = 0; // reset previous values in the select form
for (items=0; items<maxValues; items++){
listValues.options[listValues.options.length] = new Option(countryDesc[items],countryCode[items]); //populates the select list with country code and description
}
}
</script>

<body>
<form name="myForm" method="post">
....
<input tyep="text" name="myfield" value="<?php echo $_POST['myfield']?>">
<select name="country" ><script>DisplayParentValues('service');</script> </select>
...
<input type="submit" value="submit" >
</form>
</body>

PeejAvery
September 23rd, 2005, 04:56 PM
I did not fully understand your first post. Is this what you want?

<html>
<script type="text/javascript" language="Javascript">
var countryCode = new Array("","country01","country02","country03");
var countryDesc = new Array("","AUSTRALIA","UNITED KINGDOM","UNITED STATES");

function DisplayParentValues() {
var maxValues=0;
var listValues = document.myForm.country;
maxValues = countryCode.length; //matching the number of lines of this countrycode array
listValues.options.length = 0; // reset previous values in the select form
for (items=0; items<maxValues; items++){
listValues.options[listValues.options.length] = new Option(countryDesc[items],countryCode[items]); //populates the select list with country code and description
}
}

function update(){
window.alert(myForm.country.value);
}
</script>

<body>
<form name="myForm" method="post">
....
<input type="text" name="myfield" value="<?php echo $_POST['myfield']; ?>">
<select name="country" onchange="update()"><script>DisplayParentValues('service');</script> </select>
...
<input type="submit" value="submit" >
</form>
</body>
</html>

jayce
September 23rd, 2005, 05:18 PM
Peejavery,
thanks for the update,
what I would like to obtain is something like this

say you have selected Australia from the select component and you posted the form but there was an error.
so I would like to re-display the form with Australia pre-selected.

and that should look like

function DisplayParentValues() {
var maxValues=0;
var listValues = document.myForm.country;
maxValues = countryCode.length; //matching the number of lines of this countrycode array
listValues.options.length = 0; // reset previous values in the select form
for (items=0; items<maxValues; items++){
if (myForm.country.value == listValues.options[items].value) listValues.options[listValues.options.length] = new Option(countryDesc[items],countryCode[items], true); //option selected by default
else listValues.options[listValues.options.length] = new Option(countryDesc[items],countryCode[items], false); //option NOT selected
}
}


my problem resides in the point I do not know how to obtain the selected value with the syntax : listValues.options[items].value
Thanks