php and select and hidden value onchange

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
rentahippie
Forum Newbie
Posts: 4
Joined: Sun May 10, 2009 9:38 pm

php and select and hidden value onchange

Post 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
Last edited by Benjamin on Mon May 11, 2009 12:02 am, edited 1 time in total.
Reason: Changed code type from text to php.
Yossarian
Forum Contributor
Posts: 101
Joined: Fri Jun 30, 2006 4:43 am

Re: php and select and hidden value onchange

Post 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.
rentahippie
Forum Newbie
Posts: 4
Joined: Sun May 10, 2009 9:38 pm

Re: php and select and hidden value onchange

Post 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');>";
Last edited by Benjamin on Mon May 11, 2009 12:40 pm, edited 1 time in total.
Reason: Changed code type from text to php.
ldougherty
Forum Contributor
Posts: 103
Joined: Sun May 03, 2009 11:39 am

Re: php and select and hidden value onchange

Post by ldougherty »

Try this..

Code: Select all

$dropdownstring = "<select name='$postname' id='$postname' onchange='storeVal(this,'hiddenVal')'>";
Last edited by Benjamin on Mon May 11, 2009 1:22 pm, edited 1 time in total.
Reason: Changed code type from text to php.
Yossarian
Forum Contributor
Posts: 101
Joined: Fri Jun 30, 2006 4:43 am

Re: php and select and hidden value onchange

Post 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.
Last edited by Benjamin on Mon May 11, 2009 1:34 pm, edited 1 time in total.
Reason: Changed code type from text to php.
rentahippie
Forum Newbie
Posts: 4
Joined: Sun May 10, 2009 9:38 pm

Re: php and select and hidden value onchange

Post 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.
Yossarian
Forum Contributor
Posts: 101
Joined: Fri Jun 30, 2006 4:43 am

Re: php and select and hidden value onchange

Post 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 { }.
rentahippie
Forum Newbie
Posts: 4
Joined: Sun May 10, 2009 9:38 pm

Re: php and select and hidden value onchange

Post by rentahippie »

I didn't know they trick existed in php I use this in bash all the time.
Post Reply