samuvk Posted December 17, 2012 Share Posted December 17, 2012 Hi, I'm trying to get my first lines in PHP. I have read some tutorials, but since I'm not a programer is a little hard for me to get it straight. Could someone help me out to put the following javascript into php so I could just call php from my HTML, something like MySelect and get the selector. <h3><font color="#3EA99F">Categories</font></h3> <select id="mySelect" onchange="if(this.options[this.selectedIndex].value != ''){window.top.location.href=this.options[this.selectedIndex].value}"> <option>Select an option</option> <option value="site1">Orange</option> <option value="site2">Pineapple</option> <option value="site3">Banana</option> </select> Thank you so much More Explanation: I want to have something like MyFile.php where I have definied the Selector, so I can call MySelect from Index.html and write the select in the html, so If I have 400 .html pages, I do not have to change the code in the 400 pages if I want to add a value or make any change, but just made the change in MyFile.php. Quote Link to comment https://forums.phpfreaks.com/topic/272111-my-first-code-in-php/ Share on other sites More sharing options...
Christian F. Posted December 17, 2012 Share Posted December 17, 2012 (edited) The easiest way to accomplish this, is to use include (). Think of it as a copy&paste function, where you take the code inside the other file and paste it into place (instead of the include () statement). Since you also need to tell the web-server that it's PHP code, add the <?php and ?> tags around it, so that it looks like this: <?php include ('myfile.html'); ?> The semicolon (;) is used to tell PHP that the command ends there. Very important to remember! PS: This isn't something I'd recommend for more complex code. However, since you only want to have some simple HTML code injected, and without having to maintain it in all of those files, it is quite acceptable to do so here. Edited December 17, 2012 by Christian F. Quote Link to comment https://forums.phpfreaks.com/topic/272111-my-first-code-in-php/#findComment-1399945 Share on other sites More sharing options...
samuvk Posted December 17, 2012 Author Share Posted December 17, 2012 This definetly looks like what I'm looking for, but I'm not able to make it work. This is what I did: 1. I created a file "Selector.html" with the following code: (If I open the file I can see the selector) <h3><font color="#3EA99F">Categories</font></h3> <select id="mySelect" onchange="if(this.options[this.selectedIndex].value != ''){window.top.location.href=this.options[this.selectedIndex].value}"> <option>Select an option</option> <option value="site1">Orange</option> <option value="site2">Pineapple</option> <option value="site3">Banana</option> </select> 2. I created another file: index.html and I introduce the following code whenever I want to selector to appear but the selector does not appear. <?php include ('selector.html'); ?> Am I missing something or doing anything wrong? Thanks so much again Quote Link to comment https://forums.phpfreaks.com/topic/272111-my-first-code-in-php/#findComment-1399973 Share on other sites More sharing options...
TOA Posted December 17, 2012 Share Posted December 17, 2012 Well, I'm assuming the use of a capital S on one and not the other is accidental, and taking that into account, are they in the same folder? Your path to the included file might not be correct.. Quote Link to comment https://forums.phpfreaks.com/topic/272111-my-first-code-in-php/#findComment-1399975 Share on other sites More sharing options...
Christian F. Posted December 17, 2012 Share Posted December 17, 2012 Ah, forgot to mention that the file containing the PHP code must be a .php file. Otherwise the PHP code will not be parsed, and thus the script will not work. That's in addition to what TOA just said about the filenames. You must ensure that the capitalization is 100% the same, going with lower-case only is thus recommended. Quote Link to comment https://forums.phpfreaks.com/topic/272111-my-first-code-in-php/#findComment-1399976 Share on other sites More sharing options...
TOA Posted December 17, 2012 Share Posted December 17, 2012 Ah, forgot to mention that the file containing the PHP code must be a .php file. Otherwise the PHP code will not be parsed, and thus the script will not work. That's in addition to what TOA just said about the filenames. You must ensure that the capitalization is 100% the same, going with lower-case only is thus recommended. Good point...might just be easier to use file_get_contents if there's never going to be php in it.. Quote Link to comment https://forums.phpfreaks.com/topic/272111-my-first-code-in-php/#findComment-1399978 Share on other sites More sharing options...
samuvk Posted December 18, 2012 Author Share Posted December 18, 2012 Thank you both for all your points: I realized about the capitalization after I posted and fixed but still didn't work. So that's was not the issue. On the other hand, both of my files were .html I didn't have any php file, so I guess that's a reason of why didn't work. However, I need my main file to be HTML. What about the option that TOA mentioned: file_get_contents How can I use that? Thanks again to the both of you Quote Link to comment https://forums.phpfreaks.com/topic/272111-my-first-code-in-php/#findComment-1399984 Share on other sites More sharing options...
bspace Posted December 18, 2012 Share Posted December 18, 2012 well if you need your main file to be .html then your gonna have to set up an .htaccess file containing AddHandler x-httpd-php5 .php .html Quote Link to comment https://forums.phpfreaks.com/topic/272111-my-first-code-in-php/#findComment-1399988 Share on other sites More sharing options...
samuvk Posted December 18, 2012 Author Share Posted December 18, 2012 Thank you both for all your points: I realized about the capitalization after I posted and fixed but still didn't work. So that's was not the issue. On the other hand, both of my files were .html I didn't have any php file, so I guess that's a reason of why didn't work. However, I need my main file to be HTML. What about the option that TOA mentioned: file_get_contents How can I use that? Thanks again to the both of you Well I may use php, but I need to find out how to make it work, becuase I'm not able to make it work. Any idea of why it is not working? Thanks Quote Link to comment https://forums.phpfreaks.com/topic/272111-my-first-code-in-php/#findComment-1400010 Share on other sites More sharing options...
Beeeeney Posted December 18, 2012 Share Posted December 18, 2012 Well I may use php, but I need to find out how to make it work, becuase I'm not able to make it work. Any idea of why it is not working? Thanks You do have it installed on a local server, right? Quote Link to comment https://forums.phpfreaks.com/topic/272111-my-first-code-in-php/#findComment-1400031 Share on other sites More sharing options...
Muddy_Funster Posted December 18, 2012 Share Posted December 18, 2012 (edited) @samvuk - give us a bit more info about your development setup. What webDemon/php/os are you using? create a new page in your webroot and call it myPHPinfo.php and put the following code in it : <?php phpinfo(); ?> @Christian F. - You shouldn't really use parenthesis for include or require as they arn't really functions. And just to clarify for the OP's benefit. By default your file extension must be .php, if you edit your webDemon config for what application to launch what extension with you can add to it to make it be whatever you want. using .htm or .html isn't even close to wize though, as the native bevhaviour of the webDemon will interfeer with your settings. also, while the extension of the file you are navigating to in your browser is important, the extension of the included or required file is not at all. You can call it anything you like as long as it's there PHP will try to parse it. Edt: tag Errors Edited December 18, 2012 by Muddy_Funster Quote Link to comment https://forums.phpfreaks.com/topic/272111-my-first-code-in-php/#findComment-1400033 Share on other sites More sharing options...
trq Posted December 18, 2012 Share Posted December 18, 2012 What the hell is a webDemon? Quote Link to comment https://forums.phpfreaks.com/topic/272111-my-first-code-in-php/#findComment-1400046 Share on other sites More sharing options...
Muddy_Funster Posted December 18, 2012 Share Posted December 18, 2012 What the hell is a webDemon? hmm too nixy a term for a linux guy is it? My definition of a webDemon would be the service application used to serve http responses over an IP network: such as apache, IIS or any of the ones listed here. Hope that clears up my shorhand term Quote Link to comment https://forums.phpfreaks.com/topic/272111-my-first-code-in-php/#findComment-1400047 Share on other sites More sharing options...
DavidAM Posted December 18, 2012 Share Posted December 18, 2012 In our world, it is Daemon ... Wikipedia - Daemon The word daemon is an alternative spelling of demon, and is pronounced /ˈdiːmən/ DEE-mən. In the context of computer software, the original pronunciation /ˈdiːmən/ has drifted to /ˈdeɪmən/ DAY-mən for some speakers. Demons, on the other hand, are the guys who run around injecting bugs into my perfectly good release code. Kind of like Gremlins, only meaner and smarter. Quote Link to comment https://forums.phpfreaks.com/topic/272111-my-first-code-in-php/#findComment-1400054 Share on other sites More sharing options...
Muddy_Funster Posted December 18, 2012 Share Posted December 18, 2012 well the smarter part wouldn't be much of a challenge would it Quote Link to comment https://forums.phpfreaks.com/topic/272111-my-first-code-in-php/#findComment-1400056 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.