The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
|
 |
|
Dev Shed Forums
> Programming Languages
> PHP Development
|
PHP-General - My First Lines in PHP
Discuss My First Lines in PHP in the PHP Development forum on Dev Shed. My First Lines in PHP PHP Development forum discussing coding practices, tips on PHP, and other PHP-related topics. PHP is an open source scripting language that has taken the web development industry by storm.
|
|
 |
|
|
|
|

Dev Shed Forums Sponsor:
|
|
|

December 17th, 2012, 02:32 PM
|
|
Registered User
|
|
Join Date: Nov 2011
Posts: 14
Time spent in forums: 1 h 59 m 40 sec
Reputation Power: 0
|
|
|
PHP-General - My First Lines in PHP
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 MySelector 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
|

December 17th, 2012, 02:38 PM
|
|
|
You can't turn Javascript into PHP. Javascript is client side and PHP is server side. However, to output your HTML with PHP you simply do this:
PHP Code:
echo '
<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>
';
__________________
There are 10 kinds of people in the world. Those that understand binary and those that don't.
|

December 17th, 2012, 02:50 PM
|
 |
Likely to be eaten by a grue.
|
|
Join Date: Oct 2006
Location: Pennsylvania, USA
|
|
|
Also, this isn't javascript, it's HTML. Well, a small bit of it is, but not the majority of it.
The new user guide explains more.
__________________
HEY! YOU! Read the New User Guide and Forum Rules
"They that can give up essential liberty to obtain a little temporary safety deserve neither liberty nor safety." -Benjamin Franklin
"The greatest tragedy of this changing society is that people who never knew what it was like before will simply assume that this is the way things are supposed to be." -2600 Magazine, Fall 2002
Think we're being rude? Maybe you asked a bad question or you're a Help Vampire. Trying to argue intelligently? Please read this.
|

December 17th, 2012, 05:26 PM
|
|
Registered User
|
|
Join Date: Nov 2011
Posts: 14
Time spent in forums: 1 h 59 m 40 sec
Reputation Power: 0
|
|
|
Thanks for your answer.
It looks like this may work, but How would I call the PHP code from the HTML to produce the select in my HTML page?
Thanks
|

December 18th, 2012, 01:01 AM
|
|
Contributing User
|
|
Join Date: Jun 2009
Posts: 297
  
Time spent in forums: 3 Days 8 h 45 m 39 sec
Reputation Power: 5
|
|
|
Do you mean to create the list of option tags for your drop-down? If so, are these items stored in a database or such that can be queried by PHP so it knows how to define each option?
|

December 18th, 2012, 06:56 AM
|
|
|
|
Just to clarify, you don't "call PHP from the HTML." Rather when a user hits a URL that is a PHP page, the code therein outputs the appropriate HTML. From the client side it knows nothing about PHP, it only sees HTML. In other words, PHP is just another HTML page from the browser's perspective.
|

December 18th, 2012, 09:11 AM
|
 |
Likely to be eaten by a grue.
|
|
Join Date: Oct 2006
Location: Pennsylvania, USA
|
|
|
Right, like it says in the new user guide, PHP and HTML don't interact. You click a link to a PHP script. It creates a static HTML document, and with its dying breath sends it to you. PHP is dead before you see the document on your screen. You have to invoke another PHP script to have anything else happen. Javascript, on the other hand, runs on your browser and allows small interactions with the interface without invoking any server-side code. AJAX allows you to invoke a server-side operation (PHP) without reloading your page.
|

December 18th, 2012, 04:03 PM
|
|
|
samuvk, you may want to back up and describe at a higher level what you are trying to do. As a couple of people have pointed out, what you asked in your original and follow up ("call PHP from my HTML") isn't how PHP is used. The specific problem as you have posted it (create a selector), does not require PHP.
If what you mean is "How do I create a selector from PHP?" The short answer is you don't, or at least do not need to if you already know the content of the selector. gw1500se's initial answer demonstrated that. If you take out the "echo '" "';", it is identical to the HTML so essentially pointless PHP by itself. Although putting it in a PHP block could be useful if your PHP provides something that determines what you are displaying on your selector.
If what you mean is "How do I call a PHP script from a form based on what someone selects on a selector?" The short answer is: you have the form open the target PHP script.
If what you mean is "How do I mix some PHP in with HTML?" It would look something like this...
PHP Code:
<html>
<!-- more html stuff -->
<?php
/* here is your php stuff */
?>
<!-- more html stuff -->
<!-- The PHP could have been before/after the HTML and you can have many interspersed blocks. -->
</html>
|
Developer Shed Advertisers and Affiliates
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|