![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Hobbyist Programmer
|
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)Thanks |
|
|
|
|
|
#2 | ||
|
Programming Guru
![]() Join Date: Aug 2005
Location: England
Posts: 1,499
Rep Power: 5
![]() |
Quote:
<?php echo "Your name was " . $_POST['name']; ?> Quote:
fwrite($fp, $text1 . "\n"); fwrite($fp, $text2); |
||
|
|
|
|
|
#3 |
|
Hobbyist Programmer
|
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 |
|
|
|
|
|
#4 |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
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 |
|
|
|
|
|
#5 | |
|
Programming Guru
![]() Join Date: Aug 2005
Location: England
Posts: 1,499
Rep Power: 5
![]() |
Quote:
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. |
|
|
|
|
|
|
#6 |
|
Hobbyist Programmer
|
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? |
|
|
|
|
|
#7 |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
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 |
|
|
|
|
|
#8 | |
|
Programming Guru
![]() Join Date: Aug 2005
Location: England
Posts: 1,499
Rep Power: 5
![]() |
Quote:
[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". |
|
|
|
|
|
|
#9 |
|
Hobbyist Programmer
|
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.
|
|
|
|
|
|
#10 |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
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 |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|