To register for an Internet.com membership to receive newsletters and white papers, use the Register button ABOVE.
To participate in the message forums BELOW, click here
PHPBuilder.com  
 

 

Go Back   PHPBuilder.com > PHP Help > Code Critique

Code Critique Having someone critique your code is always a great way to hone the skills. Stop in and post your code to see what your peers may have done differently.

Reply
 
Thread Tools Rate Thread Display Modes
Old 10-20-2003, 01:19 AM   #1
kylesite
Junior Member
 
kylesite's Avatar
 
Join Date: Oct 2003
Location: here
Posts: 17
Virgin

My first script. Please tell me if it would work or not.
PHP Code:
<?php
session_start
();
$user = $_SESSION['nam'];
echo
"
      <form action=
\"$PHP_SELF\" method=\"post\">\n
      <input type=
\"text\" name=\"nam\" value=\"Name\">\n
      <input type=
\"submit\" value=\"Submit\">\n
      <br><br>
        "
;

if(empty(
$user)){
      echo
"<font color=\"red\">Please enter your name!</font>";
} else {
      echo
"<center><h1>Hello <font color=\"red\">$user</font></center></h1>";
}
?>
Thankyou
kylesite is offline   Reply With Quote
Old 10-20-2003, 01:26 AM   #2
LordShryku
kung foo code monkey
 
LordShryku's Avatar
 
Join Date: Aug 2002
Location: Occupational Hypnotherapy
Posts: 7,473
So first...if you want to know if it will work or not....test it.

So here's my two cents....
1) You don't seem to ever set the var $_SESSION['nam'], so that probably won't work.

2) Your coding assumes register_globals is on. You should learn to code assuming it is off.

3) Seperating the variable from the string is common practice.
eg.
PHP Code:
echo "<form action=\"$PHP_SELF\" method=\"post\">\n";

// Should be

echo "<form action=\"".$_SERVER['PHP_SELF']."\" method=\"post\">\n";

//Or

echo "<form action=\"{$_SERVER['PHP_SELF']}\" method=\"post\">\n";
HTH
LordShryku is offline   Reply With Quote
Old 10-20-2003, 01:29 AM   #3
kylesite
Junior Member
 
kylesite's Avatar
 
Join Date: Oct 2003
Location: here
Posts: 17
So how would i set the variable $_SESSION['nam']?
kylesite is offline   Reply With Quote
Old 10-20-2003, 01:32 AM   #4
LordShryku
kung foo code monkey
 
LordShryku's Avatar
 
Join Date: Aug 2002
Location: Occupational Hypnotherapy
Posts: 7,473
Well, try this
PHP Code:
if(isset($_POST['nam'])) {
   
$_SESSION['nam'] = $_POST['nam'];
}
LordShryku is offline   Reply With Quote
Old 10-20-2003, 01:35 AM   #5
kylesite
Junior Member
 
kylesite's Avatar
 
Join Date: Oct 2003
Location: here
Posts: 17
So then what would the whole PHP look like?
kylesite is offline   Reply With Quote
Old 10-20-2003, 01:52 AM   #6
LordShryku
kung foo code monkey
 
LordShryku's Avatar
 
Join Date: Aug 2002
Location: Occupational Hypnotherapy
Posts: 7,473
No, that's not the whole thing. That just sets your session var if the form is submitted, and the name is filled in
LordShryku is offline   Reply With Quote
Old 10-20-2003, 02:02 AM   #7
kylesite
Junior Member
 
kylesite's Avatar
 
Join Date: Oct 2003
Location: here
Posts: 17
Is this better?
PHP Code:
<?php
session_start
();
$user = $_SESSION['nam'];
echo
"
      <form action=\""
.$_SERVER['PHP_SELF']."\" method=\"post\">\n
      <input type=\"text\" name=\"nam\" value=\"Name\">\n
      <input type=\"submit\" value=\"Submit\">\n
      <br><br>
     "
;

if(empty(
$user)){
      echo
"<font color=\"red\">Please enter your name above in the box and click \'Submit\'</font>";
} else {
      echo
"<center><h1>Hello <font color=\"red\">$user</font></center></h1>";
}
if(isset(
$_POST['nam'])) {
   
$_SESSION['nam'] = $_POST['nam'];
}
?>
kylesite is offline   Reply With Quote
Reply

Bookmarks


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

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 Off
HTML code is Off
Forum Jump


All times are GMT -4. The time now is 03:31 PM.






Acceptable Use Policy

internet.comMediabistrojusttechjobs.comGraphics.com

WebMediaBrands Corporate Info


Advertise | Newsletters | Feedback | Submit News

Legal Notices | Licensing | Permissions | Privacy Policy


Powered by vBulletin® Version 3.7.2
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.