Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Jul 2nd, 2006, 4:26 PM   #1
dark_omen
Hobbyist Programmer
 
Join Date: Dec 2004
Posts: 125
Rep Power: 4 dark_omen is on a distinguished road
Send a message via AIM to dark_omen
question on implementation

I'm new to PHP, but I know how to program, and it doesn't seem that hard to do, but I don't know how to layout classes or use them, etc.. and I can't seem to find any straight forward response on this topic (OOP), even in books it doesn't seem to touch up on this very well. Basically, let's say I have a html form (name.php, where you just put your name in), and then I have a php class that would process the info in the form (process.php, let's just say it saves to a text file). First off would I set the action of the form to process.php, and could I just use the post way ($_POST['name']) in process.php to retrieve the info, or do I need to create a process.php constructor in name.php to pass in the information to be used?

And just a side question, the fwrite() function, if I have more then one line of text that I want to add to a file do I just keep calling it so like:
$text1 = "Hello"
$text2 = "Good Bye"

$fp = fopen("/location/hello.txt", "w")

fwrite($fp, $text1)
fwrite($fp, $text2)
or if I do it like that would it overwrite the file?

Thanks
dark_omen is offline   Reply With Quote
Old Jul 2nd, 2006, 5:06 PM   #2
Arevos
Programming Guru
 
Arevos's Avatar
 
Join Date: Aug 2005
Location: England
Posts: 1,499
Rep Power: 5 Arevos is on a distinguished road
Quote:
Originally Posted by dark_omen
Basically, let's say I have a html form (name.php, where you just put your name in), and then I have a php class that would process the info in the form (process.php, let's just say it saves to a text file). First off would I set the action of the form to process.php, and could I just use the post way ($_POST['name']) in process.php to retrieve the info, or do I need to create a process.php constructor in name.php to pass in the information to be used?
I'm not sure I understand the question. Unlike, say, Java, object orientation in PHP is optional, rather than the core value the language hinges upon. Is there a specific reason you wish to create a class to handle the form data? Why not do it procedually?
<?php echo "Your name was " . $_POST['name']; ?>
Quote:
Originally Posted by dark_omen
or if I do it like that would it overwrite the file?
No, it wouldn't. But you'd probably have to put a newline character in:
fwrite($fp, $text1 . "\n");
fwrite($fp, $text2);
Arevos is offline   Reply With Quote
Old Jul 2nd, 2006, 5:15 PM   #3
dark_omen
Hobbyist Programmer
 
Join Date: Dec 2004
Posts: 125
Rep Power: 4 dark_omen is on a distinguished road
Send a message via AIM to dark_omen
No i just wanted to do it on the backend so people couldn't see what was going on with the information. If I had a class, could i just make a function (or even the constructor) to take in the post data and then process it, and how would I go about doing that, would it be similar to java?
Thanks
dark_omen is offline   Reply With Quote
Old Jul 2nd, 2006, 5:33 PM   #4
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
For classes, have you consulted the php manual? It's quite good. PHP is 'backend', if you're implying server-side, versus client-side. It is not intimately interactive with the client, even though it might appear to be because you've interleaved PHP and HTML. That's just a misperception. PHP code does not appear when you 'view source', only its output, the result of your code.
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code.
Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers
DaWei is offline   Reply With Quote
Old Jul 2nd, 2006, 6:07 PM   #5
Arevos
Programming Guru
 
Arevos's Avatar
 
Join Date: Aug 2005
Location: England
Posts: 1,499
Rep Power: 5 Arevos is on a distinguished road
Quote:
Originally Posted by dark_omen
No i just wanted to do it on the backend so people couldn't see what was going on with the information.
As DaWei says, PHP code is interpreted server-side, and never seen by the client.

For example, the following PHP code:
[php]<b><?php echo "Hello World";?></b>[/php]Will be turned into this before it leaves the server:
[php]<b>Hello World</b>[/php]The browser will never receive any PHP code. The client will have no way of accessing the PHP code on your server. PHP is a purely server-side language.
Arevos is offline   Reply With Quote
Old Jul 2nd, 2006, 6:35 PM   #6
dark_omen
Hobbyist Programmer
 
Join Date: Dec 2004
Posts: 125
Rep Power: 4 dark_omen is on a distinguished road
Send a message via AIM to dark_omen
OK so if I put it on the same page that the form is on it wouldn't be seen by the client?
How would that look though, just have the $_POST['']'s in a <?php ... ?> tags right after the form, or would it be done differently?
dark_omen is offline   Reply With Quote
Old Jul 2nd, 2006, 6:49 PM   #7
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
You still haven't quite gotten the difference between client-size code and server-side code. The client side code is prepared on the server, either directly by writing HTML, or indirectly by emitting HTML with another language. All this is put together, then the page is sent as a response to a request. The page is rendered according to the HTML. This may present a form. When the page is submitted as part of a new request, the form contents arrive at the server in accordance with http protocol. For PHP, they are available in an array. You deal with them, and provide some new and appropriate response. You should study some more about the client-server paradigm so that you'll have a firmer idea of how these things relate and come together. We're happy to answer your questions, but the forum is not a good substitute for pre-study. I would suggest that you write some code, examine the results, hone the questions that are raised by reality, and ask about them.
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code.
Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers
DaWei is offline   Reply With Quote
Old Jul 2nd, 2006, 7:19 PM   #8
Arevos
Programming Guru
 
Arevos's Avatar
 
Join Date: Aug 2005
Location: England
Posts: 1,499
Rep Power: 5 Arevos is on a distinguished road
Quote:
Originally Posted by dark_omen
How would that look though, just have the $_POST['']'s in a <?php ... ?> tags right after the form, or would it be done differently?
Maybe an example would be appropriate here:
[php]
<form action="" method="post">
<p>Name: <input type="text" name="name" /></p>
<p><input type="submit" /></p>
</form>

<?php
if ($_POST['submit']) {
echo "Your name is " . $_POST['name'];
}
?>[/php]The above code places the form, and the PHP code, on the same page. The empty action attribute on the form tag tells the browser to send the information on the form to the current page.

You can also split it:

index.php
[php]
<form action="formsubmit.php" method="post">
<p>Name: <input type="text" name="name" /></p>
<p><input type="submit" /></p>
</form>[/php]
formsubmit.php
[php]<?php echo "Your name is " . $_POST['name']; ?>[/php]The "if submit" statement isn't needed in this case, as we can reasonably assume that formsubmit.php will only be accessed through the form in index.php.

However, as DaWei says, learning the basics of PHP is best learnt elsewhere. Try googling "PHP tutorial".
Arevos is offline   Reply With Quote
Old Jul 2nd, 2006, 8:18 PM   #9
dark_omen
Hobbyist Programmer
 
Join Date: Dec 2004
Posts: 125
Rep Power: 4 dark_omen is on a distinguished road
Send a message via AIM to dark_omen
Ok, I'll try to find a good tutorial online and probably look around the PHP manual as it looks pretty extensive. Thanks for the help.
dark_omen is offline   Reply With Quote
Old Jul 3rd, 2006, 9:16 AM   #10
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
Please note that "send the form to the current page" doesn't mean the page currently being rendered. It means send the form to the server and have the server bring up the same script that generated the page previously. This is quite important; without effort the transaction is stateless. The second time the script is brought up, it has no idea about the previous times. The effort could be using 'sessions', or some state could be carried in, for instance, the fields
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code.
Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers
DaWei is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 4:30 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC