The new forums will be named Coin Return (based on the most recent vote)! You can check on the status and timeline of the transition to the new forums here.
The Guiding Principles and New Rules document is now in effect.

More PHP stuff

oniianoniian Registered User regular
edited June 2007 in Help / Advice Forum
So I am trying to pass some php into html to make the coding more compact. I have a form that is filled out in the HTML page that includes the PHP, which is then parsed to another PHP page for evaluation. Esstentially I am trying make a drop down list of 100 selection in a few lines of code. Here is what I have so far but I am not sure if it is any good, I haven't found any good resources on embedding PHP in a mainly HTML form. I guess a better question: is this possible?
<?php
echo ('Select an interger');
echo ('<select name=\"interger\">');
for ($n=0; $n=<100; $n++) {
echo ('<option value=\"$n\">$n</option>');
}
echo ('</select>');
?>




instead of:
<select name="interger">
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
<option value="5">5</option>
...
...
...
</select>

oniian on

Posts

  • SeguerSeguer of the Void Sydney, AustraliaRegistered User regular
    edited June 2007
    That should work, but will give you 0 - 100.

    Also, integer, not interger.


    As to "embedding PHP within HTML" - this specific thing is impossible, as PHP runs on server-side and HTML is parsed client-side. PHP can output HTML, but you will never see working PHP code in the View Source of a HTML page

    Seguer on
  • JaninJanin Registered User regular
    edited June 2007
    Yeah, that's possible, and would probably work as written. However, I suggest using a template engine such as Smarty rather than mixing PHP and HTML.

    Janin on
    [SIGPIC][/SIGPIC]
  • oniianoniian Registered User regular
    edited June 2007
    Seguer wrote: »
    As to "embedding PHP within HTML" - this specific thing is impossible, as PHP runs on server-side and HTML is parsed client-side. PHP can output HTML, but you will never see working PHP code in the View Source of a HTML page

    Thats what I was thinking, someone tried to convince me otherwise but I think they were getting confused with Javascript.

    Seguer wrote: »
    Also, integer, not interger.

    Eh, thats just me writing an example on the fly as I am talking w/ my GF on the phone.

    For some reason, I am using IIS and htt://localhost/... to test my script but I am not getting any error codes in response, just blank pages or section of the codes.

    oniian on
  • Alistair HuttonAlistair Hutton Dr EdinburghRegistered User regular
    edited June 2007
    oniian wrote: »
    For some reason, I am using IIS and htt://localhost/... to test my script but I am not getting any error codes in response, just blank pages or section of the codes.

    I don't know IIS but if it was Apache that sound like the file extension options haven't been set right ad the sever is trying to treat the files as plain html without processing the PHP. What's the file extension of the files you are testing? If they are .htm/.html then that might well be the reason. Try changing the extensions to .php to see if that makes a difference or fiddling around with IIS to alter the file associations.

    Alistair Hutton on
    I have a thoughtful and infrequently updated blog about games http://whatithinkaboutwhenithinkaboutgames.wordpress.com/

    I made a game, it has penguins in it. It's pay what you like on Gumroad.

    Currently Ebaying Nothing at all but I might do in the future.
  • SeguerSeguer of the Void Sydney, AustraliaRegistered User regular
    edited June 2007
    Are you seeing your PHP code in those blank pages/sections of code? Then Alistair is correct - PHP isn't being told to parse the page, thus PHP won't do anything there. Easiest way is to make a new PHP file ("phpinfo.php") with the following contents:
    <?php
    phpinfo();
    ?>
    

    and run it.

    Seguer on
  • SporkAndrewSporkAndrew Registered User, ClubPA regular
    edited June 2007
    jmillikin wrote: »
    Yeah, that's possible, and would probably work as written. However, I suggest using a template engine such as Smarty rather than mixing PHP and HTML.

    Agreeing with this.

    Smarty makes everything so much simpler. We used it at my old work to power 10+ sites, and the templates we used were so easy to read because of Smarty..

    Instead of doing silly things like:
    print "<input type=\"text\" name=\"first_name\" size=\"40\" value=\"".$first_name."\" />";
    

    You'd just do:
    <input type="text" name="first_name" size="40" value="{$first_name}" />
    

    The separation of data / logic from presentation is something that you should already be doing in your XHTML / CSS - so taking it a step further and doing it with your PHP is the next logical progression.

    Of course, for something that's probably one form I wouldn't bother - but for mulitple scripts it's a Godsend.

    <3 Smarty.

    SporkAndrew on
    The one about the fucking space hairdresser and the cowboy. He's got a tinfoil pal and a pedal bin
Sign In or Register to comment.