FRIHOST FORUMS SEARCH FAQ TOS BLOGS COMPETITIONS
You are invited to Log in or Register a free Frihost Account!


Form handling in php is a pain in the neck





boinsterman
Form handling in php is a pain in the neck. I've found it pain-staking, and kind of mind-numbing. I've been working on this one page for two days now. The code works fine. Placing it properly in the page is what is pain-staking.
zacky
Not being boastful but I think all you need is fundamentals. Go ahead and read some couple of books about PHP fundamentals. It will also be helpful if you will care about how networking comes into play when PHP process your form.

Mainly this are common questions:

- What is header in the POST?
- Page return status
- How browser take care of your data

Something like that..
Peterssidan
Everything comes with practice.

Remember that if you want to set HTTP headers (like when redirecting) you have to do that before you start outputting any of the page content. For that reason I usually handle (or at least validate) all POST data at the top of the page.
boinsterman
The issue, I think, is error handling. Most basic instruction doesn't include error handling. It is usually included in the "advanced" sections. It needs to be a greater part of the coding culture.

I am putting the php code up top, which it is true I had not been doing. My main obstacle mentally was the thinking that interrupting php code for HTML would create different php scripts (or requiring outputing the HTML from within PHP). But I was able to prove otherwise this morning by printing a variable declared in an earlier php segment with HTML between them.
sonam
You can use PHP before, in or after HTML page. Also you can create outstanding scripts and include it in HTML when you need it. Only what you cannot is call headers after is your HTML is start a print (echo).

For errors I am using this three definition (different types for different servers):
Code:
error_reporting(E_ALL);
ini_set("display_errors", "1");
ini_set('error_reporting', E_ALL);


But in some situation if you crate syntax error you will get only blank screen. For these reason it is the best to check script several times on your localhost. After some time the most programmers in this situation know what is going wrong and where they must look. For example, I am in 40% forgot second bracket when I call empty function. Embarassed

Code:

$variable ='';
if(empty($variable) {
 echo "This is wrong"; // will not echo because is really wrong
}

if(empty($variable)) {
 echo "This is correct"; // will echo because is correct
}


Just start from some simple examples and very fast you will build good knowledge.

Sonam
sonam
I didn't answer on one part about variables. Ones defined variable you can use everywhere on your HTML, and you can redefine on the HTML. For example:

Code:
<?php
$aaa = 150;
$bbb = 300;
?>
<html>
<head></head>
<body>
<div id="aaa"><?php echo $aaa; ?></div>
<div id="bbb"><?php echo $bbb; ?></div>
<div id="ab"><?php echo $aaa + $bbb; ?></div>
<div id="redefine"><?php $aaa=1500; ?></div>
<div id="aaa"><?php echo $aaa; // result is 1500 ?></div>
</body>
</html>


Of course you didn't need to use div for redefine (or any php) this is just for example Smile

Even more you can send variable on another page if you are using $_SESSION or $_COOKIE.
I am prefer $_SESSION.

Sonam
Marcuzzo
@boinsterman
if you can provide some code or a link where the code is hosted, or both.
we could all help you out you know
codersfriend
could you provide us a code that proves what you are saying? You seem to be posting based on your current problem while starting up in PHP. anyway PHP has lots of capabilities and probably it's limit is only for creating desktop softwares but for the web it can almost do anything.
For the error handling, you can choose different levels like E_WARNING.
later versions of php, there is also a try-catch and also you can use conditionals to test for errors from functions
Gregoric
Faster error catching will come with time. Also, some IDEs or editors come with syntax checking feature, on Windows I use phpDesigner that notifies me of any error in HTML or PHP that it finds. It is commercial, but I am sure that one can find a open-source alternative as well.

You may also want to check the form validation capabilities of Javascript (AngularJS, jQuery), it is far faster, more user-friendly and does not require calculations on the server. However, if you are planning to practice PHP - do not bother with Javascript yet.
Peterssidan
Gregoric wrote:
You may also want to check the form validation capabilities of Javascript (AngularJS, jQuery), it is far faster, more user-friendly and does not require calculations on the server. However, if you are planning to practice PHP - do not bother with Javascript yet.

This does not mean you can leave out the server-side validation. JavaScript runs on the user's computer which makes it easy to tamper with so you still need the server-side validation that you can trust.
zimmer
pain in the neck on PHP - lol i just relate if i have also problem on my server or my program using PHP.
Marcuzzo
Peterssidan wrote:
Gregoric wrote:
You may also want to check the form validation capabilities of Javascript (AngularJS, jQuery), it is far faster, more user-friendly and does not require calculations on the server. However, if you are planning to practice PHP - do not bother with Javascript yet.

This does not mean you can leave out the server-side validation. JavaScript runs on the user's computer which makes it easy to tamper with so you still need the server-side validation that you can trust.


Exactly, you should always validate the user input on the server.
for all you ( the dev) know javascript could be disabled or even modified ( yes, modified ).
tcarnevale
W3Schools has a nice tutorial on PHP form handling that is pretty thorough. It talks about the pieces of PHP code you need, PHP's global variables, when to use $_Post or $_Get and how to secure your form handling.

See this link to start: [urlhttp://www.w3schools.com/php/php_forms.asp[/url].

Hope this helps. Wink Very Happy
codersfriend
Form handling is too easy that even college freshmen can do it. Not to mention it is easier compare to other server scripting languages. Just hit the books or perhaps go back to college
Marcuzzo
codersfriend wrote:
Form handling is too easy that even college freshmen can do it. Not to mention it is easier compare to other server scripting languages. Just hit the books or perhaps go back to college


true, and there's a lot of stuff a good IDE can do for you, like the pdt plugins in eclipse.
also php.net has a lot of good information.

I basicly run a check with javascript to check that all fields hold valid information before enabling the submit button.
then when the data is sent to the server I double check the data to make sure it is valid and then process it.
codegeek
Why don't you use a framework such as Laravel? It makes the whole process a lot simpler and more enjoyable.
Marcuzzo
codegeek wrote:
Why don't you use a framework such as Laravel? It makes the whole process a lot simpler and more enjoyable.

I haven't tried laravel yet, mostly because the getting started page looks scary, I'm planning on looking into Laravel in a few months but now I'm too busy with System Center Configuration Manager and also getting started with Powershell.

I do want to add that not all websites require a framework. and using a framework just for form handling looks a tad overkill to me.
Related topics
Form validation
creating guestbooks (form) with php & mysql
neep help making email form that accepts html tags
Sending email via PHP
Need a simple Contact form
Anyone know how to make an email form?
How to upload a file via html-form and php?
I am trying to build a contact form using php and flash
email form handling on a windows 2000 server help.
handling the $_POST array
how to make Php process won't stop after user disconnect.
PHP Parse error on form handling - New to PHP
How to build PHP mail form with Spam protection
Form and PHP problem
Reply to topic    Frihost Forum Index -> Scripting -> Php and MySQL

FRIHOST HOME | FAQ | TOS | ABOUT US | CONTACT US | SITE MAP
© 2005-2011 Frihost, forums powered by phpBB.