PDA

View Full Version : Ahhhhhhhh Help Me Php Code



izwar
05-09-2006, 07:56 PM
wow ive alrady started learning this and i feel like shooting myself finding the errors in my code this is crazy. im trying to learn how to do form validations, and it jus shows a white screen

http://www.hotclubber.com/testing/form.html
that url is supposd to if u dont put certain feilds its supposd to day sorry you must enter the rite date etc, and if you do put everything rite its supposd to display thank you message. this is the code can someone see wahts wrong cuz im gonna shoot my self already i cant imagine how hard this will get later.


<?php

if (!empty($_REQUEST['name'])){
$name = stripslashes($_REQUEST['name']);
} else {
$name = NULL;
echo '<p><font color="red">You forgot to enter your name!</font></p>';
}

if (!empty($_REQUEST['email'])) {
$email = $_REQUEST['email'];
} else {
$email = NULL;
echo '<p><font color="red">You forgot to enter your email address!</p></font>';
}

if (!empty($_REQUEST['comments'])) {
$comments = stripslashes($_REQUEST['comments']);
} else {
$comments = NULL;
echo '<p><font color="red">You forgot to enter your comments!</font></p>';
}


if (isset($_REQUEST['gender'])) {

$gender = $_REQUEST['gender'];

if ($gender == 'M') {
$message = '<p><b>Good Day, Sir</b></p>';
} else if ($gender = 'F') {
$message = '<p><b>Good Day, Madam</b></p>';
} else {
$message = NULL;
echo '<p><font color="red">You forgot to select your gender</font></p>';
}


if ($name && $email && $gender && $comments) {

echo "<p>Thank you, <b>$name</b>, for the following comments: <br />
<tt>$comments</tt></p>
<p>We will reply to you at <i>$email</i>.</p>\n";
echo $message;
} else {
echo '<p><font color="red">Please go back and fill out the form again.</font></p>';
}





?>

Emancipator
05-09-2006, 08:09 PM
I am a PHP programmer and I wouldnt use php to validate a form. using jscript prompts BEFORE the data is submitted is a way nicer way to do it for the user and visually.

izwar
05-09-2006, 08:44 PM
i would agree, i jus realized that lol as my current site uses that. But its just the exercises in the book started me out on this well atleast im gettin a bit more familiar with conditional statements, im starting arrays tommorow but i looked at it and looks very confusing.

r2d2
05-09-2006, 11:44 PM
I'm guessing you sorted this now as it seems to work fine for me.

Kings
05-10-2006, 12:50 AM
I am a PHP programmer and I wouldnt use php to validate a form. using jscript prompts BEFORE the data is submitted is a way nicer way to do it for the user and visually.

JS validation isn't enough, you have to do PHP validation, or else you'll open a huge security problem in your scripts.

Masetek
05-10-2006, 01:16 AM
Yeah because ppl can turn JS off. I use jscript on all my forms, but they also have a php version backing it up

AndyH
05-10-2006, 01:45 AM
I am a PHP programmer and I wouldnt use php to validate a form. using jscript prompts BEFORE the data is submitted is a way nicer way to do it for the user and visually.
If you are saying don't do PHP validation then you are giving out bad advice.

Bad bad bad.

shadowbox
05-10-2006, 02:22 AM
I am a PHP programmer and I wouldnt use php to validate a form. using jscript prompts BEFORE the data is submitted is a way nicer way to do it for the user and visually.

What strange advice!

You should use server side code for primary validation every time - javascript is a nice addition for the user as it immediately picks up little errors, but only server side code can catch XSS, SQL injection and all the other nasties, and as someone points out, even for the mildest of validation, if JS is turned off, what then?

The New Guy
05-10-2006, 06:46 AM
Also, izwar don't use $_REQUEST. If your using a form us $_POST, if your getting it from a URI (URL) use $_GET.

Emancipator
05-10-2006, 08:53 AM
Sorry guys I should have worded my super quick response ALOT better. Everyone has caught my major faux pas. I was only referring to checking the email, etc. Without using a final PHP check you are absolutely open for injections. I know because I had a problem with it about a year ago.

Thanks for keeping me straight guys :) I will try next time to spend a bit more time on my reply to cover all the bases not just half of em.

jacob
05-10-2006, 09:40 AM
Rest assured you will never write code without a bug being there. None of us are that perfect - even those of us who live and breathe coding. you'll spend about 60-70% of your time debugging code, it just goes with the territory unfortunately.

The one thing I was taught early on was to NEVER delete code, ALWAYs comment it out. When you make big revisions, backup the file you're working on. This has saved me a ton of grief since then! You'll get used to recognizing the error codes and the debugging will come much quicker as you gain experience with the language. I type over 125wpm and have over 7 years programming experience. my co-workers heads spin when they watch me debugging code and flipping from one application back to the other until the bugs gone and they have no idea what just happened. Another piece of advice is to ensure you indent code properly and do it as you type. I taught a friend how to program who refused to do this. Indenting code properly will also save you a ton of grief down the road. And "save" you from those who might try to debug your code ;)

izwar
05-10-2006, 09:45 AM
yea i solved it, but indenting the code as you say, that woudl just help me visually right? to keep things clean. thank you guys for advice im gettin back to it right now starting on arrays talk to you guys later.

Nico
05-10-2006, 10:12 AM
Yes, indenting your code will just help you visually. It'll make your code look cleaner and easier to read, so it'll be quicker and easier to find errors,etc.
It's not an option...it's a must :)

Nicolas

jacob
05-10-2006, 10:33 AM
yes, indent your code. every place i've worked at i've wanted to lay the beats to the previous programmer because of their poor coding habits. indenting code will help you have less bald patches from pulling out your hair trying to figure out where the problem in your code is because you can't make heads or tails of the code.

the only other tidbit i learned from a great programming instructor i had is to do this:
if (condition) {
} // end of if (condition) {

so placing a comment after the closing brace so you know what the closing brace belongs to. i found this very useful when there are a few hundred lines of code and the block you're in scrolls off the page. you should do whatever you need to to make your code easier to read an understand. Any whitespace within the code is ignored by the interpreter so don't be afraid to put some in if that makes it easier to read and unerstand.

izwar
05-10-2006, 12:08 PM
thank you,but im alrady getting so frustrated with PHP its insane, i havent been able to get one freaking program to work, always displays a white screen then i go back and compare against the script that comes with the book and i see no dam changes. Then i test the books out and it works. ive had this for 3 scripts already im gettin pissed off.

r2d2
05-10-2006, 12:10 PM
Have you checked the source code of the page? maybe its a problem with your HTML? Post the script(s) that have the problems.

jacob
05-10-2006, 12:14 PM
when you get a blank screen, view the source code. sometimes the php error messages don't display but you can see them in the source code.

i've never found a piece of code that i can just plunk in and it works. part of the reason is everyone has a different server setup and the code behaves differently in different environments and even more so, between different versions. I worked on an application for months on a local development server to have to fix the whole bloody thing once i placed it on my hosting server because i work on windows at home and they have a unix server.

izwar
05-10-2006, 01:22 PM
i see, but let me tell you who ever says php is fast and easy to learn is full of **** because this is the hardest **** ive ever done, im pulling my hair out i mean all im doing is copying the examples out of the book which im supposd to be understanding butits like im understanding the basic concepts but theres so much i dont get that im copying, and whats funny is even copying it word for word i still cant get the dam scripts tow ork unless i use the ones i downloaded from the book. Im getting so pissed and discouraged i dont see myself learnign this sht.im pissed off to cuz ive been at this about 6 hours today and i feel like im going no where.

something so simple like this i cnat even get working
http://hotclubber.com/testing/calculator.php
( it only works cuz i used their script)
i cant even imagine how im gonna struggle wen real stuff starts coming.

izwar
05-10-2006, 01:42 PM
do you guys suggset me getting a book like php and mysql for dummies, since i think this book i got mite be to advanced and i feel like im not learning all i can more just copying code and then going crazy cuz it doesnt work.

r2d2
05-10-2006, 02:35 PM
Take it easy, everyone gets frustrated with their programming - take the rest of the day off and start tomorrow - you won't get anything productive done if you are as annoyed as you sound!

Give us an example of the code you can't get to work. Or some you don't understand, and want to ask about.

I found this site has some good examples to just read, and get used to the way PHP works: www.phpfreaks.com

Here are some articles that might help you get the grip of things:

http://www.phpfreaks.com/tutorials/37/0.php
http://www.phpfreaks.com/tutorials/7/0.php
http://www.phpfreaks.com/tutorials/26/0.php
http://www.phpfreaks.com/tutorials/42/0.php

jacob
05-10-2006, 03:24 PM
i think a book on programming concepts would do you better then one specifically on a language. i've never found copying code helps me to understand it and i see far too many programmers who ask questions that show they dont understand what they are working with.

you could try posting questions and we can answer them.

agua
05-10-2006, 05:17 PM
Sometimes the books have printing errors in them - you should check the website related with the book for any known corrections.

Masetek
05-10-2006, 08:58 PM
One thing, error reporting may be turned off by your host. Some hosts see it as a security threat because you can see the server path. Worth a look

Selkirk
05-10-2006, 09:43 PM
The one thing I was taught early on was to NEVER delete code, ALWAYs comment it out. When you make big revisions, backup the file you're working on.
The best way to do this is to use source code control, such as subversion. This is so important that I consider the failure to use source code control to be professional malpractice. others agree (http://www.sitepoint.com/forums/showpost.php?p=2514539&postcount=8). With SCC, you can delete and change without fear and avoid commented out code.



the only other tidbit i learned from a great programming instructor i had is to do this:
if (condition) {
} // end of if (condition) {

Actually, this is a bad practice. There is a principle in programming called DRY, or don't repeat yourself (http://www.artima.com/intv/dry.html). By repeating the condition, you make two places in the code that have to change together instead of one, thus making the code harder to maintain.


i found this very useful when there are a few hundred lines of code and the block you're in scrolls off the page.

Ah, here is the real problem. If you never have more than a page full of code in a block, then its not hard to line up the braces. In fact, if you follow the rule of never allowing a single procedure or method to be larger than a single page, you will find your code turns out much better. Why this is true (http://www.sitepoint.com/forums/showpost.php?p=911699&postcount=18).

Unfortunately, the casual programmer might not have the desire to learn how to setup something like svn and might not have the expertise to break up large chunks of code. (20 years of programming and I am still learning better ways to organize code. Its definitely not a solved problem.)

There is some good advise in this thread. For PHP security Issues, I would recommend Chris Shiflett's book (http://phpsecurity.org/).

izwar, what book are you using?

Put this in your .htaccess file to override your hosts settings and make sure that errors are printed to the screen. Don't leave these in place after you are done editing your code.


php_flag display_errors on
php_value error_reporting "E_ALL"

izwar
05-10-2006, 10:29 PM
thanks everyone i returned the book i had, and now i got one that is more visual and tailors more to a begginer, with more detailed exercises its called
PHP 5 Fast & Easy web development by Julie Meloni.

also the phpfreak links looked good as well. I have to say i did learn a lot of the basics already from the last book but after the first 2 chapters it was getting insane, and all i was doing is copying code and not having it work then going crazy , so i stopped learning. But i am not giving up and not stopping till i can atlesat maintain / rebuild my site from scratch of a member community site, with profiles, messaging, rating blogs etc etc.

if i have any questions ill just post them in this thread as to not crowd the forum, thanks everyone i appreciate help.

izwar
05-12-2006, 10:17 AM
new book i s much better everything going good, gettin to sending emails now.

Emancipator
05-12-2006, 08:46 PM
good to hear your progressing.