Jump to content

Recommended Posts

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.

Link to comment
https://forums.phpfreaks.com/topic/272111-my-first-code-in-php/
Share on other sites

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 by Christian F.

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

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.

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..

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

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

@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 by Muddy_Funster

What the hell is a webDemon? ;)

hmm too nixy a term for a linux guy is it? :P 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 ;)

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.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.