Building a Web Form in ASP.NET and PHP: a Comparison

While there are important differences between PHP and ASP.NET, both are used to build websites. Because of this, both need to enable developers to build web forms, among other tasks. This article compares building a web form in PHP with building the same form in ASP.NET to help those familiar with one set of tools to learn how to use the other set.

Contributed by
Rating: 5 stars5 stars5 stars5 stars5 stars / 8
March 09, 2010
Rate this Article:
MEH MEH++


SEARCH ASP FREE
TOOLS YOU CAN USE

advertisement

Let me get one thing straight: ASP.NET is a framework or platform, while PHP is a server side scripting language. Therefore, one cannot compare ASP.NET to PHP directly. However, since ASP.NET is a server-side web technology, it employs  server-side scripting languages too, like PHP. The most common server-side languages used in ASP.NET are Visual Basic.NET and C#.

You can find a lot of PHP developers -- more than ASP.NET developers (in 2009-2010), in fact. There are many reasons why this is the case. This article is purposely written as an introductory tutorial for those that have already learned how to develop websites in PHP and need to learn ASP.NET as well. It might appear easy to create certain web functionality in PHP, but it can be very hard to find an equivalent method in ASP.NET.

If you are ready, let's get started.

Developing Software in ASP.NET

Let's tackle the most important aspect. If you are developing ASP.NET websites, there are certain things you need to have, assuming you've already learned to develop with PHP. First, you need a Windows operating system, such as XP Home with Service Pack 3, Windows 7, XP Professional, etc. Of course, this is simply common sense; since ASP.NET is a Microsoft web technology, you need a Microsoft-based operating system when you start developing applications.

The Microsoft Windows operating system can cost additional money, because you need to purchase a license, and some developers have opted to install an invalid version of Windows in their computer. However, using an invalid Windows installation might have an impact on the .NET framework. If you use an invalid version of Windows, you will not  be able to obtain updates for it.

This means that if you are already developing websites in PHP and also plan to become a serious ASP.NET developer, you need a genuine Windows operating system. You'll need to be prepared to spend some cash, because Microsoft software is not open source.

Here's another difference to keep in mind. In PHP, you can run XAMPP as a test server in a LAMPP configuration (Linux -Apache - MySQL and PHP). The good thing about XAMPP is that it will run in both Windows and Linux operating systems. But if you plan to test ASP.NET websites in the local host, you need a full installation of Visual Web Developer Express 2008 (as of 2009- 2010, Microsoft may release a newer version in the future) in your Windows operating system.

Starting with Hello World

In PHP, the "Hello World" string output to the browser is very simple. You can create it using the following code snippet:

<?php

echo "Hello World!";

?>

 

How to accomplish this in ASP.NET

You can display the Hello World text to the browser without passing it to a server side scripting language, with an ASP.NET website. In fact, I've written an entire tutorial on developing your first ASP.NET website that covers this point in detail. In the tutorial, I use Visual Web Developer Express.

The section of that tutorial that's most relevant to our purposes in this article can be found here. Simply replace the text illustrated in that section with "Hello World!"

A web form that accepts input and displays output to the browser

Building a web form is one of the most important website development skills because of its enormous applications. Say, for example, you've built a form in PHP to ask the user to input their first and last name. The form might look like this (validation is not included):

<?php

if (!$_POST['submit']) {

//form not submitted show form

//entry field

echo '<form action="'.$SERVER['PHP_SELF'].'" method="post">';

echo 'Enter your FIRST and LAST name only: ';

echo '<input type="text" name="name" size="23">';

echo '<br />';

echo '<br />';

echo '<input type="submit" name="submit" value="Submit">';

echo '</form>';

}

else {

//form submitted get data

echo '<form action="'.$SERVER['PHP_SELF'].'" method="post">';

echo 'Enter your FIRST and LAST name only: ';

echo '<input type="text" name="name" size="23">';

echo '<br />';

echo '<br />';

echo '<input type="submit" name="submit" value="Submit">';

echo '</form>';

echo '<br />';

echo '<br />';

$name=trim($_POST['name']);

echo 'Your name is:&nbsp;&nbsp;'.$name;

echo '<br />';

}

?>

 

How to accomplish this in ASP.NET

If you want to build a similar form in ASP.NET, you will need to go through the following steps:

Step 1: Launch Visual Web Developer Express.

Step 2: Create an ASP.NET website. To do this, in the Dashboard à Create Website à Select "ASP.NET" under Visual Studio installed templates à set the options to the following:

Location: File system

Language: Visual Basic

Path: This depends on where you save it in your Windows computer. If you wish to save it in Drive E and would like to have the folder name aspnetwebform, the path would be:

E:aspnetwebform

Step 3: Unlike in PHP, in which you hard code everything from scratch, ASP.NET offers user-friendly web controls for creating web forms. Since the web form is a text box, you will go to View à Toolbox à Standard à click and drag "TextBox" to between <div> </div> in the Default.aspx source code file:

 

After dragging, Visual Web Developer automatically pastes in server side text box markup code. There's no need to memorize codes in ASP.NET; it's just a click-and-drag web development strategy.

Of course you need to label the text box for users to know what to type: "Enter your FIRST and LAST name only:" (see screen shot above; the changes have been added inside the red box).

ASP.Net web form, continued

 

Step 4: Now that we have a text box, you need add a submit button, just as you would have to in PHP. First, add two break lines after </asp:TextBox>

<br /><br />

Then go to View à Toolbox à Standard à click and drag "Button" next to <br /> statements. Change Text="Button" to Text="Submit" so that the form button will be more meaningful to users. See screen shot below (changes added in red box):

Step 5: Now we have the button, but ASP.NET still does not know what to do if the form is submitted (e.g. the user clicks the "submit" button). To give instructions to ASP.NET when the  user clicks the submit button, you will add what is called a "click handler event." Go to Design View and double click the "Submit" button.

The Visual Editor for a click handler event will show.

 

In the above screen shot, ASP.NET asked you for the Visual Basic script to insert as a click handler event. Basically, this script tells ASP.NET what it will do if the submit button is clicked. The code will be:

'Get the user input FIRST name and LAST name from the text box field

'Assigned it as username

Dim Username As String = TextBox1.Text

 

'Output the user first and last name back to the browser

 displayname.Text = "Your name is: " & Username

However, displayname is not declared. So we need to declare it in the next step. But first, you need to save all files.

Step 6: The equivalent to the PHP echo command in ASP.NET is the server side "label" web control. First, go back to the "Source" view. Add a space and break after </form>:

Go to View à Toolbox à Standard à click and drag "Label" next </form><br /><br />

Change Label ID="Label1" to Label ID="displayname", to make the ID consistent with the one used by the Visual Basic script. Also change Text="Label" to Text="" 

 

 

Step 7: Go to File à View in Browser and then you will see the ASP.NET equivalent web form of the PHP web form shown in the previous section.

 

 

Overall Summary and Recommendations

ASP.NET development might look complex to start with (because it requires a lot of software to get started), but if you already have the tools, web development in ASP.NET is a breeze. In PHP, you only get a small set of tools to start with, but you mostly have to deal with complex hard coding. Reusable codes can smooth the process.

For further code equivalence tutorials, I recommend that you read the following articles:

Retrieving Data from Microsoft SQL Server 2008 using ASP.NET 3.5

Creating an ASP.NET Dynamic Web Page using a MS SQL Server 2008 Database (GridView Display)

These tutorials show how to work with databases in ASP.NET, just as you do with MySQL databases in PHP.

blog comments powered by Disqus
ASP.NET ARTICLES

- Implementing ASP.NET 4.0 Page.MetaDescriptio...
- ASP.Net Development Tips
- Intro to Sessions in ASP.Net
- Google Maps API Introduction in ASP.NET usin...
- Creating an ASP.NET 3.5 Gridview Image Galle...
- Encrypt QueryString in ASP.NET 3.5 using VB....
- ASP.NET 3.5 Drop Down List Controls
- Connect to Access Database with ASP.Net
- Secure Audio Streaming with ASP.Net and Flash
- Dynamic Sitemap and Navigation in ASP.Net
- Implement Gzip and Deflate Compression in AS...
- Run ASP.Net in Ubuntu with Apache
- ASP.Net Mono Website Contact Forms
- ASP.Net URL Rewriting Methods
- Murach`s ASP.NET 4 Web Programming with C# 2...

ASP Web Hosting ASP.Net Web Hosting Windows Web Hosting
ASP Free Forums 
 RSS  Tutorials RSS
 RSS  Forums RSS
 RSS  All Feeds
Site Map 
Request Media Kit
Write For Us Get Paid 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
Privacy Policy 
Support 


© 2003-2012 by Developer Shed. All rights reserved. DS Cluster 6 - Follow our Sitemap
Most Popular Topics
All ASP.Net Tutorials