Posting Data To PHP Using PostBackUrl - Part 2

by Saurabh 22. January 2008 20:55

Couple of years ago I did a blog post about using PostBackUrl with PHP. Although the code sample I used as illustration was a ASP.net page in it's simplest form, real world ASP.net pages aren't as simple as that. Almost any ASP.net web app that does a little more than just input some form data and flush it out would be doing so with the help of user controls and/or Master Pages. This complicates the HTML form entity names as it prefixes the user specified IDs with a number of _ (underscores) and $ signs.

So how can your PHP script read form data when element names ain't that simple anymore? Allow me to illustrate. Say we have a very basic Master page like this:

   1: <%@ Master Language="C#" AutoEventWireup="true" CodeFile="verybasic.master.cs" Inherits="Masters_verybasic" %>
   2:  
   3: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
   4:  
   5: <html xmlns="http://www.w3.org/1999/xhtml" >
   6: <head runat="server">
   7:     <title>Untitled Page</title>
   8: </head>
   9: <body>
  10:     <form id="form1" runat="server">
  11:     <div style="clear: both; width: 100%; text-align: center;">MASTER HEADER. <br />
  12:         <asp:CheckBox ID="CheckBox1" runat="server" Text="Master's chkBox" /></div>
  13:     <div>
  14:         <asp:contentplaceholder id="ContentPlaceHolder1" runat="server">
  15:         </asp:contentplaceholder>
  16:     </div>
  17:     </form>
  18: </body>
  19: </html>

And we have a simple webform that uses the above master:

   1: <%@ Page Language="C#" MasterPageFile="~/Masters/verybasic.master" AutoEventWireup="true" CodeFile="p2php.aspx.cs" Inherits="p2php" Title="Post To PHP" %>
   2: <asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
   3:     <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
   4:     <br />
   5:     <asp:Button ID="Button1" runat="server" Text="Submit" PostBackUrl="catchpostback.php" />
   6: </asp:Content> 

To read values of controls that are part of the Master page, the form itself or user controls, we will exploit the fact that in all such cases the form elements are "prefixed" with their containers and a bunch of underscores & dollar signs to the original developer specified control IDs. We can build a function similar to the one below:

   1: function getFormVar($var,$isPostBack = false)
   2: {
   3:     $retVal = "";
   4:     if($isPostBack == true)
   5:     {
   6:         $var = $var.'$';
   7:         foreach($_POST as $key => $r)
   8:         {
   9:             if(ereg($var,$key))
  10:                 return $_POST[$key];
  11:         }
  12:         return "";
  13:     }
  14:     else
  15:     {
  16:         if(isset($_REQUEST[$var]))
  17:         {
  18:             return $_REQUEST[$var];
  19:         }
  20:         else
  21:             return $retVal;
  22:     }
  23: }

To read the value of a control say TextBox1 you do something like this: $myvar = getFormVar("TextBox1",true);

The function getFormVar can read regular form elements as if they were from a PHP or HTML page, but when the 2nd parameter is set to true it loops through the POST variables and returns the value of the element of a ASP.net control (buried inside a user control or a master page) while you only specify the control ID you used in the ASP.net itself.

Some tidbits of this function you should know:

  • Although it gets you started, this function is not a complete solution. For example if the form element you want to read is part of a user control which has more than one instances on the page, it will fail flat out. But you can easily change the regex to enable reading in those cases.
  • I used ereg() instead of preg_match()  (comparatively fast) in my function as preg_match() might not be available on many setups.

A complete working demo can be downloaded from here.

There's one more thing that's pretty interesting IMO; ASP.net posts the entire form to the target page specified in PostBackUrl and of course __EVENTTARGET is empty!

Demo in action!

Tags: ,

Programming

Comments

11/4/2009 2:16:17 PM #

good post Thank for sharing

scratch and dent United States | Reply

3/26/2010 1:24:10 AM #

Aw, this was a really quality post. In theory I'd like to write like this too - taking time and real effort to make a good article... but what can I say... I procrastinate alot and never seem to get something done.

unsecured payday loans United States | Reply

3/27/2010 4:51:15 AM #

Hey, i've been reading this blog for a while and have a question, maybe you can help... it's how do i add your feed to my rss reader as i want to follow you. Thanks.

direct deposit cash advance United States | Reply

3/27/2010 4:53:46 AM #

Hey I love your style I will subscribe for your feed please keep posting!

cash advance checks United States | Reply

3/29/2010 7:50:13 PM #

I wanted to thank you for this great read!! I definitely enjoying every little bit of it.I have you bookmarked to check out new stuff you post.

fast loans online United States | Reply

3/29/2010 7:52:43 PM #

I admire what you have done here. I like the part where you say you are doing this to give back but I would assume by all the comments that this is working for you as well.

payday loan fast United States | Reply

3/30/2010 5:42:42 AM #

I usually dont post in Blogs but your blog forced me to, amazing work.. beautiful

bad credit unsecured loan United States | Reply

3/30/2010 5:45:15 AM #

Aw, this was a really quality post. In theory I'd like to write like this too - taking time and real effort to make a good article... but what can I say... I procrastinate alot and never seem to get something done.

really bad credit loans United States | Reply

4/1/2010 4:20:29 PM #

In searching for sites related to web hosting and specifically comparison hosting linux plan web, your site came up.You are a very smart person!

stretch mark treatment United States | Reply

4/25/2010 2:02:24 PM #

Hi, I�m quite impressed by your blogging talent. Please make contact with me if a part-time blog employment feels like some thing you be thinking about

Renna Passer Liechtenstein | Reply

4/25/2010 7:37:11 PM #

asp.net asp.net asp.netYou made some good points there about asp.net . I did a search on the topic and found the majority will agree with your blog.

locksmith Estonia | Reply

4/29/2010 8:45:26 PM #

Hello. Great job. I did not expect this on a Wednesday. This is a great story. Thanks!

instant approval personal loans United States | Reply

4/29/2010 8:47:45 PM #

Found your site on del.icio.us today and really liked it.. i bookmarked it and will be back to check it out some more later ..

personal loans United States | Reply

5/7/2010 11:05:28 AM #

I be supposed to in reality be working

locksmith 24 hrs Sri Lanka | Reply

5/13/2010 3:18:22 PM #

Glorious post!

car games online United States | Reply

5/21/2010 1:28:44 AM #

This is exactly what i was looking for. thank you for the informative post and keep up the good work!

cash advance pay day loan United States | Reply

5/30/2010 10:14:40 PM #

Thanks for taking the time to post.

the golden ticket license United States | Reply

5/31/2010 11:22:27 AM #

Hey, i've been reading this blog for a while and have a question, maybe you can help... it's how do i add your feed to my rss reader as i want to follow you. Thanks.

designer handbags United States | Reply

6/20/2010 8:57:19 AM #

I never realised this before, but you make a very fabulous point indeed!

Charbroil United States | Reply

6/20/2010 7:52:28 PM #

I was very delighted to find this site.I wanted to thank you for this great read!! I definitely enjoyed every little bit of it and I have you bookmarked to check out new stuff you post.

grills cookers United States | Reply

6/21/2010 1:27:46 AM #

sure...

Tyson F. Gautreaux United States | Reply

6/21/2010 8:49:36 PM #

Great write-up, I am a big believer in writing comments on sites to help the blog writers know that they’ve added some thing worthwhile to the world wide web!

seo wilmington United States | Reply

6/22/2010 7:27:54 AM #

You lost me, buddy. I mean, I assume I get what youre indicating. I realize what you're saying, but you just seem to have forgotten that you will find some other persons within the world who view this issue for what it truly is and may not agree with you. You may possibly be turning away a lot of persons who might have been fans of your site.

explore talent reviews United States | Reply

6/30/2010 2:22:36 AM #

I do not agree all, but I do congratulate the author of the site for the topic.

cigarette United States | Reply

6/30/2010 7:52:56 PM #

I was just looking at relevant blog posts for my project research and I happened to stumble upon yours. Thanks for the useful material!

Norris Soose United States | Reply

7/1/2010 7:10:27 AM #

Hows it going
I attempted to follow your feed but it doesn't appear to be working.  I'm going to give it a shot later.

Merchant Service Providers United States | Reply

7/1/2010 11:54:37 AM #

Hey cool site, I saw your web site as I was  study on how to improve my web log. I was just now wondering which spam software package you utilize for comments as I get tons on my blog.

Cheap Reading Glasses United States | Reply

7/2/2010 8:05:00 AM #

The theme is especially exciting. Congratulations to the author of the site

abercrombie United States | Reply

7/4/2010 5:51:30 PM #

Since this is the first visit to your blog, I just wanted to say hi there! Very good website by the way.

Felicitas Wootton United States | Reply

7/7/2010 5:47:40 PM #

Between me and my husband we've owned more MP3 players over the years than I can count, including Sansas, iRivers, iPods (classic & touch), the Ibiza Rhapsody, etc. But, the last few years I've settled down to one line of players. Why? Because I was happy to discover how well-designed and fun to use the underappreciated (and widely mocked) Zunes are.

heel shoes United States | Reply

7/8/2010 8:29:38 AM #

You should wite a blog post about a recommendation of sites like this one. I found your site two days ago when I was googling, however I am not really into the blog thing. I don't think it's because I do not enjoy blogs, but more than likely due to the fact that I'm a little ignorant to them.  But blogengine is cool.

Verifone Manuals United States | Reply

7/8/2010 6:41:12 PM #

You are a extremely good person!

An Leonick Guatemala | Reply

7/8/2010 6:55:41 PM #

Thanks again for a pleasant site.

Tereasa Rydolph India | Reply

7/9/2010 8:55:07 AM #

There are those who argue that we should not move forward with <a href="http://www.boot-outlet.co.uk" >ugg boots for sale</a> any other elements of reform until we have fully sealed our borders," he said. "Our borders are just too vast for us to be able to solve the problem only with fences and border patrols. It won't work.

prada handbag People's Republic of China | Reply

7/9/2010 10:45:08 PM #

Hey i think your weblog is very F - Excellent i discovered it in yahoo and i put it on my favorite list  wish to see other excellent posts from u  asap.

Hildegarde Spicknall Hong Kong S.A.R. | Reply

7/11/2010 12:33:08 AM #

Sorry, I have removed this phrase

loans United States | Reply

7/11/2010 12:34:10 AM #

Very amusing idea

payday loans toronto United States | Reply

7/11/2010 12:41:54 AM #

Is there any other output?

toronto cash advance United States | Reply

7/11/2010 1:04:56 AM #

Congratulations, this remarkable idea is very useful by the way

payday loans toronto United States | Reply

7/11/2010 1:22:32 AM #

Valuable message

payday loans toronto United States | Reply

7/11/2010 1:33:12 AM #

You are a very talented person

payday loans United States | Reply

7/11/2010 1:35:18 AM #

Tomorrow is a new day.

bad credit loans toronto United States | Reply

7/11/2010 1:38:35 AM #

I apologize, I would like to give you another choice.

loans toronto United States | Reply

7/11/2010 1:41:31 AM #

Rather useful message

payday loans toronto United States | Reply

7/11/2010 1:58:17 AM #

You have quickly thought of such an original answer

payday loans toronto United States | Reply

7/11/2010 1:59:38 AM #

This topic is simply original Smile, it is very interesting to me.

toronto cash advance United States | Reply

7/11/2010 2:09:28 AM #

What awesome words... super, brilliant idea

bad credit loans United States | Reply

7/11/2010 2:19:33 AM #

It is remarkable, it is a very valuable answer

toronto bad credit loans United States | Reply

7/11/2010 2:21:02 AM #

You have thought of such an original answer

cash advance toronto United States | Reply

7/11/2010 2:21:10 AM #

I have removed this message

canadian payday loans United States | Reply

7/11/2010 2:26:12 AM #

What would we do without your wonderful idea?

loans toronto United States | Reply

7/11/2010 2:30:51 AM #

It is remarkable, it is amusing information

loans United States | Reply

7/14/2010 9:46:27 PM #

amazon coupon codes

amazon coupons United States | Reply

7/15/2010 10:45:04 AM #

Lenen zonder BKR toetsing gaat vandaag heel gemakkelijk. Binnen een paar uur geld lenen zonder BKR toetsing doet u hier, lees snel verder

lenen zonder bkr toetsing United States | Reply

7/15/2010 11:06:36 AM #

good post Thank for sharing

nike air max 95 People's Republic of China | Reply

7/18/2010 10:39:18 PM #

When I firstly saw your web, I've been around in many pages that I'll use to get my final assignment. I googling with my short idea in my brain, and then I met with useful article. I hope you can give several advices to me, because your knowledge is great, it can help many students to get great score in every task or project. If you don't mind, I will contact you to make short discussion for my project. I write my short comment for you to give support for your site; my friends had used your article as reference in specific term and condition. After that, my friends are satisfied with your article (I've given reference to read many pages in your site). We can make other discussion later, because it will be worth to me. Thanks in advance, hope you contact me, so we can broaden our knowledge together.

good quotes United States | Reply

7/19/2010 9:19:26 PM #

I ever watched great stories about doing small time but make great time. I was surprised, so I ignored them. But it is my fault, so you must not capable and follow me to do that. When the time was changed the ecosystem will be changed too. But one can’t be changed, it is the changes itself. Why? No more to explain that. Getting the most valuable person like you is my future lucky. The best time to do that is with my brain. Offline time can be bored, so why don’t you use online time to become rich together? Vacation can be good one; especially we hope that after reaching success, time we spent can be substituted with great time for vacation. I am sure that you are capable in doing that, so great people will meet great time for spending time. Have a nice travel with chosen one. Really wonderful one!

Today Investment Tips United States | Reply

7/21/2010 4:35:13 PM #

Surprising time in here; I forget to do my other activity because of your wonderful site. It doesn't matter with me, because it is worthed and I will learn new knowledge, hope progressively I can meet with your speech. Linguists and educationalists (in my school) had conflicted and debated in several subject, I got the correction when I read the full article here. The positive effects of debatable discussion in my school are great brain for future time (for me and for my friends). Many subjects and topics with great confusing material in my school, but I have initiation step that your site has better correct conclusion. The above discussion in Linguists and educationalists is great. I've used several techniques for my research, for example online comparison. Would you mind if I make citation for my future project? (Of course I will tell you later, once I got the project plan in my hand). Thanks for your attention in reading my comments; you can shot me in my comment details to execute this project, so you can to be as a great part in my project.

Zana Wiltsey United States | Reply

7/23/2010 9:24:15 PM #

Hi, Thanks for the fine blog. I think it is really a great topic to write about on my Website. Also here is some great blog: <A href="http://www.hostsg.com">Web Hosting</A>

Graig Lasiter Germany | Reply

7/25/2010 8:38:13 AM #

Great article. I just located your site and wished to tell you that I have certainly appreciated reading through your blogs. Anyhow I shall be subscribing to your feed and I hope you write more soon.

Colby Klamert United States | Reply

7/25/2010 9:14:52 AM #

Migraine is een regelmatig terugkerend hoofdpijnsyndroom met aanvallen van minimaal 4 en maximaal 72 uur. Kenmerkend voor migrainehoofdpijn is de kloppende

migraine United States | Reply

7/25/2010 11:11:16 AM #

Many people don’t understand the effects misleading effects. I knew what you meant. It can be more effective to give people little opinion with facts here. I recognize this problem but with little clue, so we must face the topic together. With new assurance, we hope that the skill of other categories can be found together. Nice people can work with great people, and great resource is important for us in analyzing the case. I’ve studied for more than 4 years to analyze them, but the fact still can’t be discovered. In order to do that, we must deal with the changes of management resources. With so many resources here, we can develop new ideas for solving to problem. Let’s give opinion, let’s discuss them, we can take charge with our resources. The last one but not least, I support your analyzes to face new one. With many resources that we can collect, all of them can be handled well.

beauty and skincare United States | Reply

7/27/2010 10:24:13 AM #

Appreciate what we have, that's what my mother always said to me.

timber sash windows United States | Reply

7/28/2010 12:29:43 AM #

I was wondering if you ever considered changing the layout of your blog?  Its very well written; I love what youve got to say.  But maybe you could a little more in the way of content so people could connect with it better.  Youve got an awful lot of text for only having one or two images.  Maybe you could space it out better?

best auto insurance companies United States | Reply

7/28/2010 8:02:31 PM #

Cheers for this post, It's fantastic to see another BlogEngine user. Most users these days tend to use Wordpress, but I think BlogEngine.NET is the best system to use.

PowWeb Review United States | Reply

7/30/2010 4:13:57 AM #

OMG! I must say, your blog has to be one of the most well written blogs that I have seen in a long time.What I wouldn't give to be able to create posts that are as fascinating| as yours. I think I'll have to keep reading your articles and hope that one day I can write on a topic with as much knowledge as you have! Bravo!!

computerhelp55 United States | Reply

7/30/2010 7:11:45 PM #

Your current article always have many of really up to date info.  Where do you come up with this?  Just saying you are very creative.  Thanks again

family health insurance United States | Reply

7/31/2010 10:25:43 AM #

This blog has definitely changed my perspective on this subject.  Theres no way I wouldve thought about it this way if I hadnt come across your blog.  All I was doing was cruising the web and I found your blog and all of a sudden my views have changed.  Good on you, man!

4story gold Germany | Reply

7/31/2010 12:53:02 PM #

Thank you for this blog.  Thats all I can say.  You most definitely have made this blog into something thats eye opening and important.  You clearly know so much about the subject, youve covered so many bases.  Great stuff from this part of the internet.  Again, thank you for this blog.

Atlantica gold Germany | Reply

Add comment




  Country flag

biuquote
  • Comment
  • Preview
Loading