zeta1600 Posted January 17, 2012 Share Posted January 17, 2012 I have the following code currently: <?php foreach ((array)$node->field_buy_at as $item) { ?> <?php print $item['view'] ?> <?php } ?> I would like to make the list a drop down with a link so that when a user selects, he goes to a new page. I tried the following: <select name="select"> <?php foreach ((array)$node->field_buy_at as $item) { ?> <?php $url = $node->field_buy_at[0]['url']; $store = $item['view']; ?> <? echo "<option value='$url'>$store</option>";?> <?php } ?> </select> I'm pretty sure it's this "$url = $node->field_buy_at[0]['url'];" that I don't have correct. Quote Link to comment https://forums.phpfreaks.com/topic/255191-select-dropdown-list/ Share on other sites More sharing options...
geeks Posted January 17, 2012 Share Posted January 17, 2012 shouldn't $url = $node->field_buy_at[0]['url']; be : $url = $item; Quote Link to comment https://forums.phpfreaks.com/topic/255191-select-dropdown-list/#findComment-1308560 Share on other sites More sharing options...
SergeiSS Posted January 17, 2012 Share Posted January 17, 2012 zeta1600 - why do you use PHP tags at every line? You may place a lot of code inside these tags <?php a lot of lines with PHP code here ?> Quote Link to comment https://forums.phpfreaks.com/topic/255191-select-dropdown-list/#findComment-1308568 Share on other sites More sharing options...
zeta1600 Posted January 17, 2012 Author Share Posted January 17, 2012 Well, SergeiSS, that's a good question. It's because I'm not a programmer. I actually got something to work through fumbling the www. Here it is. If you have any suggestions on how to clean this up, I would appreciate it. I have this to open in the same window (and another below to open in a new window): <!-- Pull Down Store --> <select onChange="window.location='<? print ($url); ?>'+this.value"> <?php foreach ((array)$node->field_buy_at as $item) { ?> <?php $store = $item['display_title']; $url = $item['display_url']; ?> <? echo "<option value='$url'>$store</option>";?> <?php } ?> </select> <!-- Pull Down Store --> Or open it in a new window: <form action="../"> <select name="myDestination" onchange="this.form.WINDOW_NAMER.value++; ob=this.form.myDestination; window.open(ob.options[ob.selectedIndex].value ,'Window_Name'+this.value)"> <?php foreach ((array)$node->field_buy_at as $item) { ?> <?php $store = $item['display_title']; $url = $item['display_url']; ?> <? echo "<option value='$url' target='_blank'>$store</option>";?> <?php } ?> </select> <input name="WINDOW_NAMER" type="HIDDEN" value="1"> </form> Quote Link to comment https://forums.phpfreaks.com/topic/255191-select-dropdown-list/#findComment-1308597 Share on other sites More sharing options...
Psycho Posted January 17, 2012 Share Posted January 17, 2012 OK, here are some edits to your code and some pointers: 1. Do not use PHP short tags (i.e. '<?') they have been deprecated and will not work on all servers 2. Create your code in a modular fashion so that maintenance/edits is easier. For example, you do not need two different blocks of code for opening in a separate or the same window. 3. As SergeiSS stated you do not need to exit/enter PHP tags like that. You can, but it makes the code more difficult to read. 4. Do not put "code" into your JavaScript event handlers. Create functions and then call the functions. So, here is what I would do. Put this PHP code at the top of your script. <?php //PHP configuration variable $newWindow = true; //Determines if the link will open in a new window //Generate a variable for the javascript code $newWindowJS = ($newWindow) ? 'true' : 'false'; //Create the options for the destinations select field $destOptions = ''; foreach ((array)$node->field_buy_at as $item) { $destOptions .= "<option value='{$item['display_url']}'>{$item['display_title']}</option>\n"; } ?> Add this javascript function to the HEAD of the page <script type="text/javascript"> function openURL(selObj, newWindow) { urlValue = selObj.options[selObj.selectedIndex].value; if(newWindow) { selObj.form.WINDOW_NAMER.value++; window.open(selValue, 'Window_Name'+urlValue); } else { window.location = '<?php echo $url; ?>' + urlValue; } } </script> Lastly, create the form using this <form action="../"> <select name="myDestination" onchange="openURL(this, <?php echo $newWindowJS; ?>);"> <?php echo $destOptions; ?> </select> <input name="WINDOW_NAMER" type="HIDDEN" value="1"> </form> Note: all the above was written on-the-fly and is not tested. So, there may be some errors or a couple minor bugs. But, with that logic you can easily change the behavior by simply changing the variable $newWindow to true or false based upon your needs. Quote Link to comment https://forums.phpfreaks.com/topic/255191-select-dropdown-list/#findComment-1308633 Share on other sites More sharing options...
zeta1600 Posted January 17, 2012 Author Share Posted January 17, 2012 Wow, Psycho, thanks! (never thought I'd ever say those words together) I'll give this a shot and let you know what other questions I have. I really hope this helps somebody else. Boy, I have so often wished I knew php. Can you recommend a basic bare bones tutorial? .... Hmmmm.... the selection didn't go anywhere nor opened a new window. Quote Link to comment https://forums.phpfreaks.com/topic/255191-select-dropdown-list/#findComment-1308644 Share on other sites More sharing options...
Psycho Posted January 17, 2012 Share Posted January 17, 2012 Wow, Psycho, thanks! (never thought I'd ever say those words together) I'll give this a shot and let you know what other questions I have. I really hope this helps somebody else. Boy, I have so often wished I knew php. Can you recommend a basic bare bones tutorial? .... Hmmmm.... the selection didn't go anywhere nor opened a new window. There are plenty of tutorials out there. I learned through reading existing code snippets, tutorials, the manual and this forum. I think Tizag has some pretty good tutorials. As for the code working, I really didn't expect it to work off the bat. I wrote all that code without any testing as I don't have your data to test against and I made some assumptions. I'll try some mock data to test it but I'm not going to spend too much time on it. Quote Link to comment https://forums.phpfreaks.com/topic/255191-select-dropdown-list/#findComment-1308680 Share on other sites More sharing options...
Psycho Posted January 17, 2012 Share Posted January 17, 2012 OK, I had one typo in the code I provided. I changed a var name from selValue to urlValue and didn't change it in one place. The below is a complete page that works. The one thing I'm not sure if you need is the $url variable. Go ahead and try it against real data for both options. If one does not work you should see what URL is being used and how you need to modify it <?php //Test data $node->field_buy_at[] = array('display_url'=>'page1.php', 'display_title'=>'One'); $node->field_buy_at[] = array('display_url'=>'page2.php', 'display_title'=>'Two'); $node->field_buy_at[] = array('display_url'=>'page3.php', 'display_title'=>'Three'); //PHP configuration variable $newWindow = true; //Determines if the link will open in a new window //Generate a variable for the javascript code $newWindowJS = ($newWindow) ? 'true' : 'false'; //Create the options for the destinations select field $destOptions = ''; foreach ((array)$node->field_buy_at as $item) { $destOptions .= "<option value='{$item['display_url']}'>{$item['display_title']}</option>\n"; } ?> <html> <head> <script type="text/javascript"> function openURL(selObj, newWindow) { urlValue = selObj.options[selObj.selectedIndex].value; if(urlValue=='') { return false; } if(newWindow) { selObj.form.WINDOW_NAMER.value++; window.open(urlValue, 'Window_Name'+urlValue); } else { window.location = '<?php echo $url; ?>' + urlValue; } } </script> </head> <body> <form action="../"> <select name="myDestination" onchange="openURL(this, <?php echo $newWindowJS; ?>);"> <option value=''>-- Select One --</option> <?php echo $destOptions; ?> </select> <input name="WINDOW_NAMER" type="HIDDEN" value="1"> </form> </body> </html> Also, you should add one entry as the first item in the select list - else the user cannot "select" the first value since it is already selected so there would be no onchange event. I've made those changes above. Quote Link to comment https://forums.phpfreaks.com/topic/255191-select-dropdown-list/#findComment-1308687 Share on other sites More sharing options...
zeta1600 Posted February 22, 2012 Author Share Posted February 22, 2012 Psycho... I ended up using the following to open in a new window. It works fine in almost all browsers except for Internet Explorer. Can you help? <form action="../"> <select name="myDestination" onchange="this.form.WINDOW_NAMER.value++; ob=this.form.myDestination; window.open(ob.options[ob.selectedIndex].value ,'Window_Name'+this.value)"> <?php foreach ((array)$node->field_buy_at as $item) { ?> <?php $store = $item['display_title']; $url = $item['display_url']; ?> <? echo "<option value='$url' target='_blank'>$store</option>";?> <?php } ?> </select> <input name="WINDOW_NAMER" type="HIDDEN" value="1"> </form> Quote Link to comment https://forums.phpfreaks.com/topic/255191-select-dropdown-list/#findComment-1320106 Share on other sites More sharing options...
Psycho Posted February 22, 2012 Share Posted February 22, 2012 Psycho... I ended up using the following to open in a new window. It works fine in almost all browsers except for Internet Explorer. Can you help? Sounds like a JavaScript problem. To debug those I suggest generating a complete/working HTML page. Save it as a flat file. Then, work on the JavaScript code there to get it working. Once you have it working then you can determine what changes are needed in the PHP script. Trying to fix JavaScript problems in a PHP script can be very difficult. Quote Link to comment https://forums.phpfreaks.com/topic/255191-select-dropdown-list/#findComment-1320137 Share on other sites More sharing options...
zeta1600 Posted February 23, 2012 Author Share Posted February 23, 2012 Oh my... are you able to peak at the java code you give me above to see what the problem might be? Quote Link to comment https://forums.phpfreaks.com/topic/255191-select-dropdown-list/#findComment-1320299 Share on other sites More sharing options...
Psycho Posted February 23, 2012 Share Posted February 23, 2012 Output the page to an HTML file and debug the code to at least narrow the cause down to a particular process. If you can't find the problem then post the the JavaScript forum. Quote Link to comment https://forums.phpfreaks.com/topic/255191-select-dropdown-list/#findComment-1320470 Share on other sites More sharing options...
phppup Posted February 23, 2012 Share Posted February 23, 2012 Is PHP the best way to open the new window. Zeta said she hoped this helps someone else, so here i am thinking it through. I have a PHP for putting data into a DB. The idea is that someone else will be able to access the column totals periodically. My structure is to have another form with options. Each option will run into a SELECT function that will provide information. Normally, this would be displayed on the browser, but I've found (not yet implemented) that Javascript can open a NewWindow and should be able to make the display there WITHOUT involving PHP. Am I doing something that makes sense, or sounds stupid? Feasible or futile? Quote Link to comment https://forums.phpfreaks.com/topic/255191-select-dropdown-list/#findComment-1320480 Share on other sites More sharing options...
Psycho Posted February 23, 2012 Share Posted February 23, 2012 Is PHP the best way to open the new window. Zeta said she hoped this helps someone else, so here i am thinking it through. I have a PHP for putting data into a DB. The idea is that someone else will be able to access the column totals periodically. My structure is to have another form with options. Each option will run into a SELECT function that will provide information. Normally, this would be displayed on the browser, but I've found (not yet implemented) that Javascript can open a NewWindow and should be able to make the display there WITHOUT involving PHP. Am I doing something that makes sense, or sounds stupid? Feasible or futile? PHP cannot open windows (or do anything on the client-side). But, you are probably misunderstanding some thing. The JavaScript should open the new window - but it would be calling a PHP script to generate the output in that page. Quote Link to comment https://forums.phpfreaks.com/topic/255191-select-dropdown-list/#findComment-1320551 Share on other sites More sharing options...
Shadowing Posted February 24, 2012 Share Posted February 24, 2012 In case no one was clear as to why you dont need to put php tags <?php *******php code******** ?> on every line its because you only use these php tags when you are going in and out of php. So for example <html> <body> <?php // php code here. ?> im ending the tag here cause the php code stops and html starts <form method="post" action=""> <input type="password" name="confirm" id="confirm" /> </form> <?php // php code here ?> im ending the tag here cause the php code stops and html starts </body> </html> Quote Link to comment https://forums.phpfreaks.com/topic/255191-select-dropdown-list/#findComment-1320674 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.