To register for an Internet.com membership to receive newsletters and white papers, use the Register button ABOVE.
To participate in the message forums BELOW, click here
PHPBuilder.com  
 

 

Go Back   PHPBuilder.com > Misc Help > ClientSide Technologies

ClientSide Technologies Discuss HTML/CSS/Javascript, and any other client-side technologies, here.

Reply
 
Thread Tools Rate Thread Display Modes
Old 06-12-2006, 06:28 AM   #1
obrienkev
Senior Member
 
Join Date: Jan 2004
Posts: 979
Javascript Arrays

Hi all,

I have tried the below on PHP forum but was told to try here. I have a select menu. When the user makes a selection from it, two separate textareas should be automatically populated with data from database query.

However, below not working. If I use only one array works fine for populating just one textarea.

Any ideas please? Thanks.
PHP Code:
      <?php
        
        
// 1) Get all Investment Types
        
$query = "SELECT investment_type_ID, investment_type, inv_note, inv_note2 ".
                         
"FROM investment_type";
        
$result = mssql_query($query) or die('Select Error');
        
        
$type_array = array();
        
$type1_array = array();
        
$id_array = array();

        while(
$row = mssql_fetch_assoc($result))
        {
                         
// option string
                        
$option .= '<option value="'.$row['investment_type_ID'].'">'.$row['investment_type'].'</option>';

                        
// arrays to build javascript array string
                        
$type1_array[] = $row['inv_note2'];
                        
$type_array[] = $row['inv_note']; //addslashes for javascript escape
                        
$id_array[] = $row['investment_type_ID'];
        }
// end while
        
        //create string for javascript assoc array format. "key1:value1", "key2:value2", ........
        
for($i=0; $i < sizeof($id_array); $i++)
        {
$javascript_array[$i] = '"'.$id_array[$i].'":"'.$type_array[$i].'"';
$javascript_array1[$i] = '"'.$id_array[$i].'":"'.$type1_array[$i].'"';
}
        
?>

        <script language="JavaScript">
      <?php
    
//echo javascript array var notes = {"key1:value1", "key2:value2", ...}
    
echo 'var notes = {'.implode(',',$javascript_array).'};';
    echo
'var notes2 = {'.implode(',',$javascript_array1).'};';
        
?>

        function change(id)
        {
    document.getElementById('inv_type_note').innerHTML = notes[id];
    document.getElementById('inv_type_note2').innerHTML = notes2[id];
        }
    </script>
        
    <p>
          <?php if(isset($error_message['investment_type'])){ echo $error_message['investment_type'].'<br />'; } ?>
      <label for="investment_type">Investment Type</label>
            <select name="investment_type" id="investment_type" onchange="change(this.options[selectedIndex].value);">
          <option value="0">Select Investment Type</option>
          <?php
                    
echo $option;
          
?>
            </select>
    </p>
        
    <p>
          <label for="inv_type_note">Note</label>
      <textarea cols="55" rows="5" name="inv_type_note" id="inv_type_note">
            </textarea>
    </p>
obrienkev is offline   Reply With Quote
Old 06-12-2006, 12:09 PM   #2
JPnyc
Administrator
 
Join Date: Jan 2005
Posts: 963
You're passing id in both of them, and then setting the value of id in the function call I assume? Well I thinkthat's the problem. Both arrays are being set to the same value of id.

Where's the function calls? That'll tell us what is being passed
__________________
PHP builder Online Community Manager

JPnyc is offline   Reply With Quote
Old 06-12-2006, 12:20 PM   #3
obrienkev
Senior Member
 
Join Date: Jan 2004
Posts: 979
Here are the arrays echoed out... values are correctly assigned...

PHP Code:
var notes = {"2":"item2",
                 
"3":"item3"
                
};

var
notes2 = {"2":"new1"};
Function called here...

<select name="investment_type" id="investment_type" onchange="change(this.options[selectedIndex].value);">
obrienkev is offline   Reply With Quote
Old 06-12-2006, 12:53 PM   #4
JPnyc
Administrator
 
Join Date: Jan 2005
Posts: 963
But there's one function, so you're setting the value of id to the same selectedIndex value. Will that work the way you have the arrays setup? I didn't get to see the actual arrays.
__________________
PHP builder Online Community Manager

JPnyc is offline   Reply With Quote
Old 06-12-2006, 01:09 PM   #5
obrienkev
Senior Member
 
Join Date: Jan 2004
Posts: 979
so what should I change?
obrienkev is offline   Reply With Quote
Old 06-12-2006, 02:17 PM   #6
JPnyc
Administrator
 
Join Date: Jan 2005
Posts: 963
Well it depends, I can't see the arrays so I dunno. If you have 2 arrays but only one index for them, then they better have corresponding values in the same bucket. Say if your index = 2, then you better have the 2 pieces of info you want in the 3rd bucket of each array. Otherwise you have to rewrite this function so that the id isn't passed as an arg. or else pass 2 args, id and id1 or something like that. However, if u DO that, you have to set both values in the call.
__________________
PHP builder Online Community Manager

JPnyc is offline   Reply With Quote
Old 06-13-2006, 05:22 AM   #7
obrienkev
Senior Member
 
Join Date: Jan 2004
Posts: 979
Hi,

Here are how my Javascript arrays are created... are they correct?
PHP Code:
//create string for javascript assoc array format. "key1:value1", "key2:value2", ........
for($i=0; $i < sizeof($id_array); $i++)
{
$javascript_array[$i] = '"'.$id_array[$i].'":"'.$type_array[$i].'"';
$javascript_array1[$i] = '"'.$id_array[$i].'":"'.$type1_array[$i].'"';
}
?>

<script language="JavaScript">
<?php
//echo javascript array var notes = {"key1:value1", "key2:value2", ...}
    
echo 'var notes = {'.implode(',',$javascript_array).'};';
    echo
'var notes2 = {'.implode(',',$javascript_array1).'};';
?>
obrienkev is offline   Reply With Quote
Old 06-13-2006, 09:30 AM   #8
JPnyc
Administrator
 
Join Date: Jan 2005
Posts: 963
Well thats the php which will spit out the JS arrays. It'd be easier to diagnose if I saw the output.

But basically the issue is this, you have 2 arrays, but only one index. Therefore if you pass the value of that index in the function calls, you will end up getting the same number bucket of each of the 2 arrays. Is that going to do what you wish? I still have no idea what you're really trying to do here, but that's the 1st thing you need to tell me.

For example, if you need buck 2 of the 1st array, and bucket 6 of the 2nd array, then this approach will not give you that.
__________________
PHP builder Online Community Manager

JPnyc is offline   Reply With Quote
Old 06-13-2006, 12:30 PM   #9
obrienkev
Senior Member
 
Join Date: Jan 2004
Posts: 979
OK. What I want to achieve is this...

-- The user makes a selection from a select menu
-- Two textareas are populated with the relevant data from a database based on the above selection.

1/ Get Data from Database
PHP Code:
$query = "SELECT investment_type_ID, investment_type, inv_note, inv_note2 ".
        
"FROM investment_type";
$result = mssql_query($query) or die('Select Error');
2/ Declare Arrays
PHP Code:
$type_array = array();
$type1_array = array();
$id_array = array();
3/ Loop Through Query Results + Assign Values to Arrays
PHP Code:
while($row = mssql_fetch_array($result))
{
// option string
$option .= '<option value="'.$row['investment_type_ID'].'">'.$row['investment_type'].'</option>';

// arrays to build javascript array string
$type1_array[] = $row['inv_note2'];
$type_array[] = $row['inv_note'];
$id_array[] = $row['investment_type_ID'];
}
// end while
4/ Create string for javascript assoc array format. "key1:value1", "key2:value2",
PHP Code:
for($i=0; $i < sizeof($id_array); $i++)
{
$javascript_array[$i] = '"'.$id_array[$i].'":"'.$type_array[$i].'"';
$javascript_array1[$i] = '"'.$id_array[$i].'":"'.$type1_array[$i].'"';
}
5/ Function when select menu changed
PHP Code:
                <script language="Javascript">
                 // Define first array
        var notes = new Array();
        // Assign PHP value to Javascript array
        var notes = '<?php print_r($javascript_array) ?>';
        // Define second Array
        var notes2 = new Array();
        var notes2 = '<?php print_r($javascript_array1) ?>';
        
        // Put all elements into a string
        document.write(notes.join(","));
        document.write(notes2.join(","));

        function change(id)
        {
    document.getElementById('inv_type_note').innerHTML = notes[id];
    document.getElementById('inv_type_note2').innerHTML = notes2[id];
        }
</script>
6/ Select Menu
PHP Code:
<select name="investment_type" id="investment_type" onchange="change(this.options[selectedIndex].value);">
<option value="0">Select Investment Type</option>
          <?php
                    
echo $option;
          
?>
            </select>
Make sense? Is what I am doing correct?
Thanks.
obrienkev is offline   Reply With Quote
Old 06-13-2006, 01:32 PM   #10
JPnyc
Administrator
 
Join Date: Jan 2005
Posts: 963
Well there's still something I don't understand. If you're pulling data from a server, why is JS being used at all? It doesn't play nice with PHP. Do the entire thing in PHP. It'll work much better and be easier.
__________________
PHP builder Online Community Manager

JPnyc is offline   Reply With Quote
Old 06-14-2006, 05:33 AM   #11
obrienkev
Senior Member
 
Join Date: Jan 2004
Posts: 979
How would I do it using only PHP??

When the user selects from the select menu the other textfields should then be automatically populated with data relevant to the users selection.

Thought Javascript was the only solution for this??
obrienkev is offline   Reply With Quote
Old 06-14-2006, 11:18 AM   #12
JPnyc
Administrator
 
Join Date: Jan 2005
Posts: 963
Do you want to do this without refreshing the whole page? Then what you need is AJAX
__________________
PHP builder Online Community Manager

JPnyc is offline   Reply With Quote
Old 06-15-2006, 06:02 AM   #13
obrienkev
Senior Member
 
Join Date: Jan 2004
Posts: 979
Thanks.

Here's my select menu...
PHP Code:
<select name="investment_type" id="investment_type" onchange="change(this.options[selectedIndex].value);">
<option value="0">Select Investment Type</option>
<?php
echo $option;
?>
</select>
javascript:
PHP Code:
<script language="JavaScript">
function
change(id)
{
document.getElementById('inv_type_note').innerHTML = $type_array[];
document.getElementById('inv_type_note2').innerHTML = $type1_array[];
}
</script>
and the textarea to be automatically populated...
PHP Code:
<textarea name="inv_type_note" id="inv_type_note">
</
textarea>

<
textarea name="inv_type_note2" id="inv_type_note2">
</
textarea>
Database Query:
PHP Code:
// 1) Get all Investment Types
$query = "SELECT investment_type_ID, investment_type, inv_note, inv_note2 ".
         
"FROM investment_type";
$result = mssql_query($query) or die('Select Error');

while(
$row = mssql_fetch_array($result))
{
// option string
$option .= '<option value="'.$row['investment_type_ID'].'">'.$row['investment_type'].'</option>';

// arrays to build javascript array string
$type1_array[] = $row['inv_note2'];
$type_array[] = $row['inv_note'];
$id_array[] = $row['investment_type_ID'];
}
// end while
Any ideas how I assign the database data to the Javascript?

Thanks.
obrienkev is offline   Reply With Quote
Old 06-15-2006, 08:47 AM   #14
obrienkev
Senior Member
 
Join Date: Jan 2004
Posts: 979
After making changes now. Seems to make more sense o me but unable to get the relevant Database data to display in the textfields.

Any ideas why this is?? Thanks.

PHP Code:
<?php
        
// 1) Get all Investment Types
$query = "SELECT investment_type_ID, investment_type, inv_note, inv_note2 ".
        
"FROM investment_type";
$result = mssql_query($query) or die('Select Error');

while(
$row = mssql_fetch_array($result))
{
        
// Create Arrays + Populate from DB
        
$id_array[] = $row['investment_type_ID'];
        
$type_array[] = $row['inv_note'];
        
$type1_array[] = $row['inv_note2'];
        
$investment_type[] = $row['investment_type'];                    

}
// end while
        
// Loop through $id_array to populate select menu
for($i=0; $i<sizeof($id_array); $i++)
{
        
$option .= '<option value="'.$id_array[$i].'">'.$investment_type[$i].'</option>';    
}
?>
        
<script language="Javascript">
                        
            // Declare Javascript Arrays
            var js_id_array = new Array();
            var js_type_array = new Array();
            var js_type1_array = new Array();
        
</script>
        
<?php
        
        
// Loop through PHP Arrays
        
for($i=0; $i <sizeof($id_array); $i++)
        {
?>

<script language="Javascript">
        
    // Assign PHP Arrays to Javascript Arrays
    var js_id_array['<?php echo $i ?>'] = '<?php echo $id_array[$i] ?>';
    var js_type_array['<?php echo $i ?>'] = '<?php echo $type_array[$i] ?>';
    var js_type1_array['<?php echo $i ?>'] = '<?php echo $type1_array[$i] ?>';
        
</script>

<?php
    
} // end for loop
?>
    
<script language="Javascript">

function change(id)
{
   document.getElementById('inv_type_note').innerHTML = js_type_array[id];
   document.getElementById('inv_type_note2').innerHTML = js_type1_array[id];
}
</script>

    
    <p>
      <label for="investment_type">Investment Type</label>
<select name="investment_type" id="investment_type" onchange="change(this.options[selectedIndex].value);">
          <option value="0">Select Investment Type</option>
          <?php
                    
echo $option;
          
?>
            </select>
    </p>
        
    <p>
          <label for="inv_type_note">Note</label>
      <textarea cols="55" rows="5" name="inv_type_note" id="inv_type_note">
      </textarea>
    </p>

    <p>
<label for="inv_type_note2">Note</label>
<textarea cols="45" rows="8" name="inv_type_note2" id="inv_type_note2"></textarea>
    </p>
obrienkev is offline   Reply With Quote
Old 06-16-2006, 01:00 AM   #15
JPnyc
Administrator
 
Join Date: Jan 2005
Posts: 963
My advice, ditch the JS altogether, and do this entirely with PHP. Post it back in the php section and someone will be able to help with it. Or, if you want to do it without refreshing the whole page, then your only recourse is AJAX, in which case you won't need PHP at all.
__________________
PHP builder Online Community Manager

JPnyc is offline   Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump


All times are GMT -4. The time now is 03:00 PM.






Acceptable Use Policy

internet.comMediabistrojusttechjobs.comGraphics.com

WebMediaBrands Corporate Info


Advertise | Newsletters | Feedback | Submit News

Legal Notices | Licensing | Permissions | Privacy Policy


Powered by vBulletin® Version 3.7.2
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.