开发者

How can I easily manipulate the state of page elements using PHP?

In ASP.NET

  • I can show a hidden panel that contains many controls by using simple command such as:

    Pa开发者_StackOverflow社区nel.Visible = CheckBox1.Checked;
    
  • I can disable a Button easily:

    <asp:Button ID="Button1" runat="server" onclick="Button1_Click" 
    Text="Submit" Enabled="false"  />
    

But now I'm looking at using PHP. Does it offer a similarly simple technique for hiding / disabling page elements?


The difference with ASP.Net is that all the controls in the page are (automatically) parsed into objects that can be manipulated because that is how the architecture is designed. The HTML tags that you are writing are not actually the final HTML output that will go straight to the user. It first passes through the parser and the code behind the page is executed with the objects available.

In PHP, you are emitting HTML code on your own as you need it onto the screen, and once it's been emitted, that's the final output. There is no abstraction of HTML entity objects that you can manipulate unless you are using some third-party library or you build your own. Instead, you do things like this:

if ($show) {
    echo '<a href="test.html">Testing</a>';
}

or:

echo '<input type="checkbox" ';
if ($checked) echo 'checked';
echo '/>'


The short answer is no. ASP.NET procedurally generates JavaScript and HTML that provides the functionality you anticipate. When using PHP you have to generate those controls yourself (or use one of the many PHP frameworks to help).


I personally see the features you are referring to as Javascript/HTML features rather than PHP. PHP primarily is a scripting language. For me the place it belongs on the server. Hiding/Disabling certain elements on the HTML forms remains to be a frontend function which is handled by Javascript/CSS/HTML. ASP.NET would provide you with added functionality to make it easier I guess but Enabled="false" really has more to do with HTML than ASP.NET

I am sure if you look around you will find libraries for PHP which will help you do very nice stuff with HTML forms, making your life easy. If you are looking to move to PHP for those features than I would suggest that you stay with .NET.


You could use a js library such as jQuery to hide/show elements on a page. Functionality like this belongs in the presentation layer of the page rather than the logic layer such as in PHP.


You're basically talking about Microsoft "WebForms". Which is a small part of .Net.

There's no direct analog to WebForms in PHP.

There is a good discussion of ASP.Net vs. PHP here.

Personally, I think the user experience tends to be better when you do more on the client side (e.g. with Javascript, or Java applets).

IMHO .. pSM

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜