Programming Forums

Programming Forums (http://www.programmingforums.org/forumindex.php)
-   PHP (http://www.programmingforums.org/forum29.html)
-   -   question on implementation (http://www.programmingforums.org/showthread.php?t=10605)

dark_omen Jul 2nd, 2006 5:26 PM

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

Arevos Jul 2nd, 2006 6:06 PM

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);


dark_omen Jul 2nd, 2006 6:15 PM

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

DaWei Jul 2nd, 2006 6:33 PM

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.

Arevos Jul 2nd, 2006 7:07 PM

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.

dark_omen Jul 2nd, 2006 7:35 PM

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?

DaWei Jul 2nd, 2006 7:49 PM

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.

Arevos Jul 2nd, 2006 8:19 PM

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".

dark_omen Jul 2nd, 2006 9:18 PM

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.

DaWei Jul 3rd, 2006 10:16 AM

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


All times are GMT -5. The time now is 8:01 AM.

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