/    Sign up×
Community /Pin to ProfileBookmark

handling java script arrays in php

i had two list boxes and two submit buttons.Submit button is used to populate the items from one select box into another and vice versa. When the form is submitted the selectBox2.value must be passed to php file.

<SCRIPT LANGUAGE=”JavaScript” TYPE=”text/javascript”>

function MoveOption(objSourceElement, objTargetElement)
{
var values= new Array();
var aryTempSourceOptions = new Array();
var x = 0;

//looping through source element to find selected options
for (var i = 0; i < objSourceElement.length; i++) {
if (objSourceElement.options[i].selected) {
//need to move this option to target element
var intTargetLen = objTargetElement.length++;
objTargetElement.options[intTargetLen].text = objSourceElement.options[i].text;
objTargetElement.options[intTargetLen].value = objSourceElement.options[i].value;

}
else {
//storing options that stay to recreate select element
var objTempValues = new Object();
objTempValues.text = objSourceElement.options[i].text;
objTempValues.value = objSourceElement.options[i].value;
aryTempSourceOptions[x] = objTempValues;

x++;
}

}

//resetting length of source
objSourceElement.length = aryTempSourceOptions.length;
//looping through temp array to recreate source select element

for (var i = 0; i < aryTempSourceOptions.length; i++)
{
objSourceElement.options[i].text = aryTempSourceOptions[i].text;
objSourceElement.options[i].value = aryTempSourceOptions[i].value;
objSourceElement.options[i].selected = false;

}

}

function posted_values(objTargetElement)
{
var values= new Array();
// var aryTempSourceOptions = new Array();
for (var i = 0; i < objTargetElement.length; i++)
{

values[i]= objTargetElement.options[i].value;
}
//document.write(values);
return (values);

}

</script>

<html>
<form name=”theForm” action=”<?=$PHP_SELF?>” method=”post” onSubmit=”return posted_values(theForm.sDisabled);”>
<select name=”sEnabled” size=”5″ multiple style=”width: 100px;”>
<option value=” 1″>Option 1</option>
<option value=” 2″>Option 2</option>
<option value=” 3″>Option 3</option>
<option value=” 4″>Option 4</option>
<option value=” 5″>Option 5</option>
<option value=” 6″>Option 6</option>
</select>

<input type=”button” name=”Disable” value=”&nbsp;&nbsp;&nbsp; Disable -&gt; ” style=”width: 100px;” onClick=”MoveOption(this.form.sEnabled, this.form.sDisabled)”><br>
<br>
<input type=”button” name=”Enable” value=” &lt;- Enable &nbsp;&nbsp;&nbsp;” style=”width: 100px;” onClick=”MoveOption(this.form.sDisabled, this.form.sEnabled)”><br>
<select name=”sDisabled” size=”5″ multiple style=”width: 100px;”>
<option value=”Option 7″>Option 7</option>
<option value=”Option 8″>Option 8</option>
</select>

<?php

for($i = 0; $i < sizeof($array); $i++)
{
// echo ‘<input type=”checkbox” name=”theArray[]” id=”box’.$i.'” value=”‘.$array[“$i”].'”>’.$array[“$i”].”n”;
}

?>
<input type=”submit” value=”Submit” />
</form>
</html>
<?php
print_r($_POST);exit;
?>

The problem here is the function named “posted_values(objTargetElement)
” returns values but its not able to return all the values in selectBox2 instead it returns only those values selected in the selectBox2 while the form is submitted.

with regards.
james

to post a comment
JavaScript

0Be the first to comment 😎

×

Success!

Help @jamesdavids spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.19,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...