Submitting a value from a php selectbox

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
phpForX
Forum Newbie
Posts: 10
Joined: Fri Jun 17, 2005 5:51 am

Submitting a value from a php selectbox

Post by phpForX »

Hi there,
I've an issue with sending a value from within a PHP function with onChange (javaScript)
My function does very well, exept for submitting the appropriate value. Virtually nothing happens :(

So here's the function:
Actually this function displays a selectbox wich generates a formatted output from a datetime mysql data type field.

Code: Select all

// displays a selectbox with the db-record and appropriate labeling..
function database_select($tbl_value, $select_name, $label) {
    global $DB_WE;
    $menu = &quote;<label for=\&quote;&quote;.$select_name.&quote;\&quote;>&quote;.$label.&quote;</label>\n&quote;;

    // here is the problem:

    $menu .= &quote;<select name=\&quote;&quote;.$select_name.&quote;\&quote; onchange=\&quote;document.location.href='&quote;.$_SERVER&#1111;&quote;PHP_SELF&quote;].&quote;?ViewYear=&quote; ?WHAT DO I HAV TO PUT IN HERE? &quote;\&quote;>\n&quote;;

   //end problem

	$DB_WE->query(&quote;SELECT DATE_FORMAT(DateOrder,'%Y') AS DateOrder FROM &quote;.SHOP_TABLE . &quote; GROUP BY DateOrder&quote;);
	while ($DB_WE->next_record()) {
		$menu .= &quote;  <option value=\&quote;&quote;. $DB_WE->f(&quote;DateOrder&quote;).&quote;\&quote;&quote;;
        $menu .= (isset($_REQUEST&#1111;$select_name]) && $DB_WE->f(&quote;DateOrder&quote;) == $_REQUEST&#1111;$select_name]) ? &quote; selected=\&quote;selected\&quote;&quote; : &quote;&quote;;
        $menu .= &quote;>&quote; . $DB_WE->f(&quote;DateOrder&quote;) . &quote;\n&quote;;
	}
	
	$menu .= &quote;</select>\n&quote;;
		
	return $menu;
  
}
I think that something like this should do:

Code: Select all

onChange=&quote;document.location.href=\&quote;' . $_SERVER&#1111;'PHP_SELF'] . '?\&quote; + this.name + &quote;=&quote; + this.options&#1111;this.selectedIndex].value
..but all my attemts so far caused only Errors.
Some Genius out there?

Thanks
Bennettman
Forum Contributor
Posts: 130
Joined: Sat Jun 15, 2002 3:58 pm

Post by Bennettman »

Using escaped quotes in Javascript commands (as opposed to strings) doesn't work:

Code: Select all

document.location.href=\&quote;/path/index.php?\&quote; + this.name + &quote;=&quote; + this.options&#1111;this.selectedIndex].value
Just noticed you've got escaped quotes in one place, and no escaping in another (the = sign), so that'll cause problems in the HTML parsing and the JS parsing. This problem just seems to be that you've gotten really mixed up putting the PHP and JS together and made a few syntax errors.

With inline JS, use single quotes instead (escaping required parsed by PHP rather than the browser) and remember to quote the whole inline JS:

Code: Select all

echo 'onChange=&quote;document.location.href=\'' . $_SERVER&#1111;'PHP_SELF'] . '?\' + this.name + \'=\' + this.options&#1111;this.selectedIndex].value&quote;';
phpForX
Forum Newbie
Posts: 10
Joined: Fri Jun 17, 2005 5:51 am

yeah, but.........

Post by phpForX »

Thank you very much for your prompt response.

Can you help me once more with this, as this caused an Error again:

Code: Select all

$menu .= 
"<select name=\"".$select_name."\" onChange="document.location.href=\'' . $_SERVER['PHP_SELF'] . '?\' + this.name + \'=\' + this.options[this.selectedIndex].value"'"\">\n";
User avatar
phpScott
DevNet Resident
Posts: 1206
Joined: Wed Oct 09, 2002 6:51 pm
Location: Keele, U.K.

Post by phpScott »

Code: Select all

$menu .= 
"<select name=\"".$select_name."\" onChange=\"document.location.href=' ". $_SERVER['PHP_SELF'] ." ?\' + this.name + \'=\' + this.options[this.selectedIndex].value\'\">\n";
I think that should work for you. escaping quotes when going between php and javascript is always a pain. :wink:
phpForX
Forum Newbie
Posts: 10
Joined: Fri Jun 17, 2005 5:51 am

Post by phpForX »

phpScott wrote:

Code: Select all

$menu .= 
"<select name="".$select_name."" onChange="document.location.href=' ". $_SERVER['PHP_SELF'] ." ?\' + this.name + \'=\' + this.options[this.selectedIndex].value\'">\n";
I think that should work for you. escaping quotes when going between php and javascript is always a pain. :wink:
Thank you very much. Yeah it is a pain :)
Well, now I havn't got any errors, but apparently the javaScript - variables were not extracted accurately. Look at the chunk of source-code (..you know- view source...)

Code: Select all

<label for="ViewYear">Jahr auswählen:</label>
<select name="ViewYear" onChange="document.location.href=' /webEdition/we/include/we_modules/shop/edit_shop_revenueTop.php ?\' + this.name + \'=\' + this.options[this.selectedIndex].value\'">
  <option value="2003">2003
  <option value="2004">2004
  <option value="2005" selected="selected">2005
</select>
phpForX
Forum Newbie
Posts: 10
Joined: Fri Jun 17, 2005 5:51 am

It's done- thank you...

Post by phpForX »

Well, tank you all- it's done. The issue was with the appropriate quoting.
this is the chunk:

Code: Select all

$menu .=
"<select name=\"".$select_name."\" onChange=\"document.location.href='".$_SERVER['PHP_SELF'].
"?ViewYear='+ this.options[this.selectedIndex].value\">\n";
Post Reply