Page 1 of 1

php and select and hidden value onchange

Posted: Sun May 10, 2009 9:56 pm
by rentahippie
I am trying to do onchange from drop down menus.
The values are all coming from mysql on teh fly.
I am trying to build a frontend to mysql that the user can pick what they want to display from the query they build drop down menus.

Here is the basic code I Use to make the drop down.

Code: Select all

    function GenericDropDown($title, $postname, $AddThese)
    {
        echo "\r\n" . $title . "\r\n";
 
        $dropdownstring = "<select name='$postname' id='" . $postname . "'>"; //"<select name='Local_Name[]' multiple='multiple'>";
 
        foreach ($AddThese as $item)
        {
            if (isset($_POST[$postname]) && ( $item == $_POST[$postname] ))  // Is not a Array single drop down menu
                $dropdownstring .= "\r\n\t<option value=\"" . $item . "\" selected='selected'>" . $item . "</option>";
            else
                $dropdownstring .= "\r\n\t<option value=\"" . $item . "\">" . $item . "</option>";
        }
        $dropdownstring .= "\r\n</select>\r\n";
        echo $dropdownstring;
    }
    function SingleDropDown($title, $postname, $fieldName, $dataset)
    {   
        echo "\r\n" . $title . "\r\n";
        
        $dropdownstring = "<select name='$postname' id='" . $postname . "'>"; //"<select name='Local_Name[]' multiple='multiple'>";
        //$dropdownstring = "<select name='$postname' >"; //"<select name='Local_Name[]' multiple='multiple'>";
        $dropdownstring .= "\r\n\t<option value=\"NONE\">--- All ---</option>";
        
        while($row = mysql_fetch_assoc($dataset))
        {
            //print_r ("row " . $row . "<br>");
            if (isset($_POST[$postname]) && ( $row[$fieldName] == $_POST[$postname] ))  // Is not a Array single drop down menu
            {
                $dropdownstring .= "\r\n\t<option value=\"{$row[$fieldName]}\" selected='selected'>{$row[$fieldName]}</option>";
            }
            else
            {
                $dropdownstring .= "\r\n\t<option value=\"{$row[$fieldName]}\">{$row[$fieldName]}</option>";
            }
        }
        $dropdownstring .= "\r\n</select>\r\n";
        echo $dropdownstring;
    }
 
    $query4 = "SELECT DISTINCT machine_name FROM SATA ORDER BY machine_name DESC";
    $dataset4 = @mysql_query($query4, $inventario);
 
    SingleDropDown("Machine Name:" , "Machine_Name", "machine_name" , $dataset4);
    print_r('</td>
<td class="DataLeft" height="25">
 
 
');
The Menu are all
I can't figure out how I am to add onchange to call a php function to continue.
I want to update the menus (16 of them) so it will update all the menus and make sure there is no invalid options available in the 16 drop down menus
would love it to use AJAX but I have spent 2 weeks trying to figure out to do that.
I trying to figure out cookies.
and input hidden items.
Any help would be great I can't sleep well I dream of ways to make it work.posting.php?mode=post&f=1&sid=cd27c5272 ... bddade97e1#
Anyone can help it would make my day.posting.php?mode=post&f=1&sid=cd27c5272 ... bddade97e1#

I have most of the code written in php there is some javascript to deal with the activewidget. I almost using AmCharts to display and the data collected.

I tried this but quoting is wrong in the php code

Code: Select all

$dropdownstring = "<select name='$postname' id='" . $postname . "' onchange="storeVal(this,'hiddenVal');>";
 
<script>
    function storeVal(selectObj,id)
    {
        var hiddenObj = document.getElementById(id);
        var optionArr = selectObj.getElementsByTagName("option");
    
        if (selectObj.value == "default") return false;
    
        for (i=1; i<=optionArr.length; i++)
        {
            if (selectObj.value == optionArr[i].value)
            {
                hiddenObj.value = valPairs[selectObj.value];
                return alert('The hidden value is "'+hiddenObj.value+'"');
            }
        }
    }
</script>
 
 
Someone please help.
Thanks
Steven

Re: php and select and hidden value onchange

Posted: Mon May 11, 2009 6:29 am
by Yossarian
Why don't you just create a mockup using real code (JS and HTML) and get that working first, then enhance it by getting real values from PHP, create functions etc etc.

Try turning on error_reporting as you work too.

Re: php and select and hidden value onchange

Posted: Mon May 11, 2009 11:55 am
by rentahippie
I have been doing that I just can't figure out the quoting on this one line.
How do I make this line work.

Code: Select all

$dropdownstring = "<select name='$postname' id='" . $postname . "' onchange="storeVal(this,'hiddenVal');>";

Re: php and select and hidden value onchange

Posted: Mon May 11, 2009 1:18 pm
by ldougherty
Try this..

Code: Select all

$dropdownstring = "<select name='$postname' id='$postname' onchange='storeVal(this,'hiddenVal')'>";

Re: php and select and hidden value onchange

Posted: Mon May 11, 2009 1:28 pm
by Yossarian
So the question is how do I correctly quote JS strings which are made with PHP.

Answer : with great difficulty ;)

try this #2

Code: Select all

 
$dropdownstring = "<select name='$postname' id='$postname' onchange=\"storeVal(this,'hiddenVal')\">";
 
My advice is to stick to one convention, and get your target sample working in straight html/JS first.

Re: php and select and hidden value onchange

Posted: Mon May 11, 2009 6:55 pm
by rentahippie
Thanks that helped out alot simple me that was too simple I was not thinking of escaping the characters.
I am working on too many things at once.

Re: php and select and hidden value onchange

Posted: Tue May 12, 2009 3:31 am
by Yossarian

Code: Select all

$var = "easier" ;
 
$js = <<<EOL  // Can be any token you like, used EOL here
 
<a href="#" onclick="alert('This is a whole lot {$var}');">Just html</a>
 
EOL;  // NO tabs, or spaces ! IMPORTANT !
 
echo $js ;
Another trick when outputting JS from PHP is to use the heredoc method as shown above, in which you do not need to escape quotes.

You just write your JS in a test page, get all your quotes in the correct order so your xhtml validates, and your JS works, then paste it into a heredoc block.

Replace concrete values with PHP and make sure they dont "leak" into your JS by delineating them with { }.

Re: php and select and hidden value onchange

Posted: Tue May 12, 2009 4:50 am
by rentahippie
I didn't know they trick existed in php I use this in bash all the time.