#1
  1. No Profile Picture
    Gone PHP Crazy
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2003
    Location
    Melbourne, Australia
    Posts
    393
    Rep Power
    12

    Passing option select form values to javascript


    I am trying to send a value of an Option form object to a javascript. Here is the code:
    PHP Code:
        <tr>
            <td width="40%" align="right" class="pagecontentrightalign"><strong>Delete Product Folder:</strong>&nbsp;&nbsp;</td>
            <td width="20%" >            
                <?
                
    if ($dir = @opendir("products")) 
                {
                    while ((
    $file readdir($dir)) !== false
                    {
                        if(
    $file != ".." && $file != ".")
                        {
                            
    $deleteing_directory[] = $file;
                        }
                    } 
                    
    closedir($dir);
                }
                
    ?>
                <select name="del_dir" >
                <option>-- Select Folder --</option>
                <?php 
                asort
    ($deleteing_directory);
                while (list (
    $key$val) = each ($deleteing_directory)) 
                {
                    echo 
    "<option>$val</option>";
                }
                
    ?>
                </select>&nbsp;&nbsp;
            </td>
            <td><input name="delete_directory" type="submit" value="Delete Folder"  onclick="return VerifyDelete('<?php echo $del_dir;?>')" /></td>
        </tr>
    There will be a list of folder names in the option list and depending on which one is selected and submit button is clicked the value of the option selected will get transferred to a javascript for processing. Appreciate any ideas.
    This is the code which needs some modification:
    PHP Code:
    onclick="return VerifyDelete('<?php echo $del_dir;?>')"
    visit http://www.techsatcomputers.com
  2. #2
  3. Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Jul 2001
    Location
    England
    Posts
    967
    Rep Power
    13
    Assuming you label the form, "my_form", the value will be available to javascript from (amongst other things):

    document.my_form.elements["del_dir"].value;

    So, you could do:

    PHP Code:
    <SCRIPT LANGUAGE="JavaScript">
    <!--
        function 
    VerifyDelete()
        {
            if(
    confirm("Are you sure you want to delete the directory, " document.my_form.elements["del_dir"].value "?"))
            {
                
    document.my_form.submit();
            }
        }
        
    //-->
    </SCRIPT> 
    And your form submit button would be something like:
    <input type="button" name="delete" value="Delete Directory" onClick="VerifyDelete();">

    Is that what you had in mind?
  4. #3
  5. No Profile Picture
    Gone PHP Crazy
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2003
    Location
    Melbourne, Australia
    Posts
    393
    Rep Power
    12
    Yes thats exactly what I wanted. I had to make an other modification to get it working. We should not forget to get a value property into the option form object. Final code looks like this:

    PHP Code:
        <tr>
            <td width="40%" align="right" class="pagecontentrightalign"><strong>Delete Product Folder:</strong>&nbsp;&nbsp;</td>
            <td width="20%" >            
                <?
                
    if ($dir = @opendir("products")) 
                {
                    while ((
    $file readdir($dir)) !== false
                    {
                        if(
    $file != ".." && $file != ".")
                        {
                            
    $deleteing_directory[] = $file;
                        }
                    } 
                    
    closedir($dir);
                }
                
    ?>
                <select name="del_dir" >
                <option id="del" value="-- Select Folder --" >-- Select Folder --</option>
                <?php 
                asort
    ($deleteing_directory);
                while (list (
    $key$val) = each ($deleteing_directory)) 
                {
                    echo 
    "<option id=\"del\" value=\"$val\">$val</option>";
                }
                
    ?>
                </select>&nbsp;&nbsp;
            </td>
            <td><input name="delete_directory" type="submit" value="Delete Folder"  onclick="return VerifyDelete();" /></td>
        </tr>
    Javascript function:
    Code:
    function VerifyDelete()
    {
    	//var folder = document.directory.del_dir.value;	//Works the same as below
    	var folder = document.directory.elements["del_dir"].value 
    	//alert(folder);
    	
    	if (confirm("This will Delete folder " + folder + " and its contents.\nProceed with Deletion ?")) return true;
    	else return false;
    }
    visit http://www.techsatcomputers.com

IMN logo majestic logo threadwatch logo seochat tools logo