My first PHP

By DangerOfFate on Dec 12, 2008

 <html>
<body>

<form action="welcome.php" method="post">
Age: <input type="text" name="age" />
<input type="submit" />
</form> 

That's the HTML of it, I guess...I'm new to this so please go easy on me...That goes before the actual code.

<? if($age >= "18");  {
 echo "Hello, you are $age "; }
else{
 echo "Sorry, you are not the correct age."; }
?>
</body>
</html>

Comments

Sign in to comment.
Jonesy44   -  Jun 30, 2009

Wrong place to ask dude. Try Forums :)

 Respond  
gilbertsavier   -  Jun 30, 2009

Hi,

I need a code that lets my customers upload a image file from their computer and then that file does 2 things

  1. gets uploaded to upload folder on my server
  2. a copy of that file gets e-mailed to me

this is what I have so far, any improvements/additions on this code would really be appreciated...

1.//GET IMAGE INFO
2.

  1. $dir = 'images/';
  2. $file_name = $_FILES['file']['name'];
  3. $file_tmp = $_FILES['file']['tmp_name'];
  4. //CHECK FOR UPLOADED FILE
  5. if(is_uploaded_file($file_tmp)){
  6. //MOVE TO DIR
  7. move_uploaded_file($file_tmp, $dir.$file_name);
  8. }

Thanks & regards,
Lokananth

 Respond  
F*U*R*B*Y*   -  Dec 20, 2008

or have it as a dropdown/select box for the equation? :P

 Respond  
jcbones   -  Dec 19, 2008

Nah, PHP won't parse symbols like that, it will just print it back to the page.

Try this instead...

<?php
$form = "<form action=\"test.php\" method=\"post\"><input type=\"text\" value=\"\" name=\"one\"/><br/><input type=\"text\" value=\"\" name=\"two\"/><br/><input type=\"text\" value=\"\" name=\"three\"/><br/><input type=\"submit\" value=\"submit\" name=\"submit\"/></form>";

if($_POST["submit"])
{
  if($_POST["two"] == "+") {$value = $_POST["one"] + $_POST["three"]; echo $_POST["one"] ." + " . $_POST["three"] . " = " . $value;}
elseif($_POST["two"] == "-") {$value = $_POST["one"] - $_POST["three"]; echo $_POST["one"] . " - " . $_POST["three"] . " = " . $value;}
elseif($_POST["two"] == "*") {$value = $_POST["one"] * $_POST["three"]; echo $_POST["one"] . " * " . $_POST["three"] . " = " . $value;}
elseif($_POST["two"] == "/") {$value = $_POST["one"] / $_POST["three"]; echo $_POST["one"] . " / " . $_POST["three"] . " = " . $value;}
else echo "You have entered an unknown operator!";

}

else echo stripslashes($form);

?>
 Respond  
Jonesy44   -  Dec 13, 2008

Set a variable with the post, it should parse all symbols.. i think, never really had the need to try it out before..

 Respond  
DangerOfFate   -  Dec 13, 2008

D:....I'm -trying- to code a PHP calculator of a sort, just an input field for someone to type like...2+2 and it'll return 4 when the action is submitted.

 Respond  
Jonesy44   -  Dec 13, 2008

Nice to see another PHP scripter out there :L lacks a few on this site hehe.

I must be bored or something.. but this would incorporate all your HTML & PHP together;

<?php
echo '<html>
      <body>';

if (!$_POST) {
  echo '<form action="' .$_SERVER["PHP_SELF"]. '" method="post">
        Age: <input type="text" name="age" />
        <input type="submit" />
        </form>';
}
else {
  $age = $_POST["age"];
  if ($age < 18) {
    echo 'You are too young';
  }
  else {
    echo 'Welcome';
  }
}
?>
 Respond  
Hawkee   -  Dec 13, 2008

Danger, why don't you edit the snippet to reflect our suggestions?

 Respond  
DangerOfFate   -  Dec 12, 2008

heh..Thanks..I'm just a pessimist, I guess.

 Respond  
F*U*R*B*Y*   -  Dec 12, 2008

And also, i don't think you should put your own work down....

lol failure....

and

Tags lamer

Sure its not the best script out there, but its your first PHP script. I know my first php script was

<?php echo "Hello World"; ?>

you wouldn't believe how much problems i had, firstly i had <?php hello world ?> i spent hours trying to get it to work, i ended up asking RussellReal and he told me i was missing the " and the ; but he wasn't going to tell me where.

We all start off at the same place, it doesn't matter what the quality of the script is, its worth the knowledge of what you gain from creating this script.

 Respond  
F*U*R*B*Y*   -  Dec 12, 2008

Also not all servers have.... uhmmm can't think of it of the top of my head, but they won't allow $age they require you to do $age = $_POST['age'];.

Not bad for a first script :) Hope to see more, and if your stuck on a problem, yell out on the forums and we'll all help out =)

 Respond  
DangerOfFate   -  Dec 12, 2008

Thanks Hawkee.

 Respond  
Hawkee   -  Dec 12, 2008

You should just put all the code together into the snippet. There's no reason to separate it like this. Also, always use <?php instead of <? to avoid compatibility problems with certain servers.

 Respond  
DangerOfFate   -  Dec 12, 2008

lol failure....

 Respond  
Are you sure you want to unfollow this person?
Are you sure you want to delete this?
Click "Unsubscribe" to stop receiving notices pertaining to this post.
Click "Subscribe" to resume notices pertaining to this post.