#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2011
    Posts
    24
    Rep Power
    0

    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
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Jul 2003
    Posts
    3,502
    Rep Power
    594
    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.
  4. #3
  5. Sarcky
    Devshed Supreme Being (6500+ posts)

    Join Date
    Oct 2006
    Location
    Pennsylvania, USA
    Posts
    10,908
    Rep Power
    6351
    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.
  6. #4
  7. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2011
    Posts
    24
    Rep 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
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Jun 2009
    Posts
    677
    Rep Power
    7
    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?
  10. #6
  11. No Profile Picture
    Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Jul 2003
    Posts
    3,502
    Rep Power
    594
    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.
    There are 10 kinds of people in the world. Those that understand binary and those that don't.
  12. #7
  13. Sarcky
    Devshed Supreme Being (6500+ posts)

    Join Date
    Oct 2006
    Location
    Pennsylvania, USA
    Posts
    10,908
    Rep Power
    6351
    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.
    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.
  14. #8
  15. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2011
    Posts
    118
    Rep Power
    50
    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>

IMN logo majestic logo threadwatch logo seochat tools logo