Using a class function to determine a forms action

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
JoeAckney
Forum Newbie
Posts: 3
Joined: Sun Jan 25, 2009 11:20 am

Using a class function to determine a forms action

Post by JoeAckney »

Hi, i'm currently trying to learn how to program in PHP using the OO aproach and i'm having trouble with form actions. I'll post the section of code in question and then explain in more detail:

Code: Select all

 
<form action="$NewPost->buildPost()" method="post">
 
All relevant files are included and $NewPost is a valid instance of the class buildPost() function belongs to. Currently buildPost() returns a string variable for testing purposes.

Could anyone tell me how i could i use a member function as part of a forms action? Any help would be greatly appreciated!

Thanks,

Martin.
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: Using a class function to determine a forms action

Post by requinix »

Code: Select all

<form action="$NewPost->buildPost()" method="post">
That's a function, not a variable, so you can't embed it into a string. If you have that as-is right in the HTML, PHP doesn't know it's supposed to do something with it.

Assuming that buildPost() returns a string,

Code: Select all

<form action="<?php echo $NewPost->buildPost(); ?>" method="post">
JoeAckney
Forum Newbie
Posts: 3
Joined: Sun Jan 25, 2009 11:20 am

Re: Using a class function to determine a forms action

Post by JoeAckney »

Sorry, i should have mentioned that ultimately i want it to execute a void function. So rather than run the code in a *.php file (action="buildPost.php") i want it to run the code in a specfic class function ($NewPost->buildPost()) contained within a php file.

I know i could use action="postClass.php?action=buildpost" and have some code outside of the class definition in the same file to call the function, but i was wondering if there was a neater way.
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Re: Using a class function to determine a forms action

Post by John Cartwright »

For many good reasons, that is not possible. The not so neat way which you refered is how the rest of us do it.
JoeAckney
Forum Newbie
Posts: 3
Joined: Sun Jan 25, 2009 11:20 am

Re: Using a class function to determine a forms action

Post by JoeAckney »

Okay, I appreciate the replies. I know I'm being far too fussy, after all, if it works then it works ;)

Thanks anyway guys.
Post Reply