[PHP] My first PHP-script using $_POST! :)
I have recently started learning php and i just wanted to show of a part of what i have learned so far. :P
You can see the script in action here.
Check it out:
The full sourcecode for name.php is:
Code:
<html>
<head>
<title>My first php-script width the GET and POST included!</title>
</head>
<body bgcolor=#000000>
<br><br><br><br>
<center>
<form action=nameform.php method=post>
<font size=2 color=white face=verdana>
First name: <input type=text name=fname><br>
Last name: <input type=text name=lname><br>
Your favorite movie: <input type=text name=favmov><br>
Your age: <input type=text name=age><br>
<input type=reset value=Start over>
<input type=submit value=OK></form>
</body>
</html>
and the full source code for nameform.php is:
PHP Code:
<?php
echo "
<font face=verdana size=4 color=white><b>Muhaha, i know all about you!<br><br></b>";
// Just a little cool greeting "I know all about you!"
echo "
<font face=verdana size=4 color=white>
Your name is <font face=verdana size=4 color=red>".$_POST['fname']." ".$_POST['lname']."!</font><br><br>";
// The ".$_POST['fname']." will take the input wit the name "fname" from the form and write it in here.
// The ".$_POST['lname']." will take the input wit the name "lname" from the form and write it in here.
echo "
<font face=verdana size=4 color=white>
You'r <font face=verdana size=4 color=red>".$_POST['age']."</font> years old and your favorite movie is <font face=verdana size=4 color=red>".$_POST['favmov'].".</font><br>";
// The ".$_POST['age']." will take the input wit the name "age" from the form and write it in here.
// The ".$_POST['favmov']." will take the input wit the name "favmov" from the form and write it in here.
?>
So whatta you think? This ain't bad efter like 3 days (1 hour/day) learning? :P
Re: [PHP] My first PHP-script using $_POST! :)
Pretty good for roughly 3 hours. Try making it XHTML compatible and tabbing everything out for readability.
Oh, you may want to get this moved to the Showcase.
Re: [PHP] My first PHP-script using $_POST! :)
Nicely done sir, now you just need to work on doing something with the inputted data (e.g. transforming it, storing it, etc).
Re: [PHP] My first PHP-script using $_POST! :)
And try to keep html outof the php ;),
Will look alot better.
Just use <? echo $_POST['varible']; ?>
Re: [PHP] My first PHP-script using $_POST! :)
you spelled you're wrong. haha.
also, you should make it more advanced. work on using php or javascript to check if they typed in numbers in the age. good work though. =]
Re: [PHP] My first PHP-script using $_POST! :)
Nice, but your HTML code is deprecated.
The <center> and <font> tag are not supported by the current version of HTML, therefore browser *may* not display them properly.
Also, try using quotes...makes it more clear IMO: bgcolor="#000000"
Once you're going to work with MySQL, you'll want to make sure some values are correct (and don't have hidden JS code or SQL code), don't do this with JS...always do it with PHP.
And, wrong section. Moved to Showcase...it's a showcase eh?! Not a code problem / question...
Re: [PHP] My first PHP-script using $_POST! :)
Thanks guys for the great feadback :P
Re: [PHP] My first PHP-script using $_POST! :)
I've tried fixin' it up a little, added some stuff.
This will save the sent info in a text file within the folder 'test' (if that folder isnt made, it'll create it!):
PHP Code:
<?php
if ($_POST['send']) {
$message = "<div id='main'><p><b>Muhaha, i know all about you!</b></p>
<p> </p>
<p>Your name is <span class='input'>". $_POST['fname'] ." ". $_POST['lname'] ."!</span></p>
<p> </p>
<p>You're <span class='input'>". $_POST['age'] ."</span> years old and your favorite movie is <span class='input'>". $_POST['favmov'] .".</span>
<p> </p></div>";
echo $message;
$messagetodo = str_replace("<p> </p>", "
", $message);
$messagetodo = str_replace(" ", " ", $message);
$messagetodo = strip_tags($messagetodo);
if (!is_dir("test")) {
mkdir("test");
}
$name = "test/test". rand(1, 100) .".txt";
$open = fopen($name, "w+");
if ($open) {
if (fwrite($open, $messagetodo)) {
$msg = "Successfully Created file: ". $name;
} else {
$msg = "Error in fwrite('". $open ."', '$message');";
}
} else {
$msg = "Error in fopen('". $name ."', 'w+');";
}
}
?>
<html>
<head>
<title>My first php-script width the GET and POST included!</title>
<style type="text/css">
p {
font-family: Verdana;
font-size: 18px;
color: #FFFFFF;
}
span.input {
font-family: Verdana;
font-size: 18px;
color: #FF0000;
}
#main {
margin: auto;
text-align: center;
}
</style>
<script type="text/javascript">
<!--
var c = 0;
function check()
{
frm = document.form1;
if (frm.fname.value == '') {
alert('Please fill in your name!');
c = 1;
}
if (frm.lname.value == '') {
alert('Please fill in your last name!');
c = 1;
}
if (frm.favmov.value == '') {
alert('Please fill in your favourite movie!');
c = 1;
}
if (frm.age.value == '') {
alert('Please fill in your age!');
c = 1;
}
if (c != 1) {
frm.submit();
}
}
-->
</script>
</head>
<body bgcolor="#000000" text="#FFFFFF">
<div id="main">
<h1>Fill out the form!</h1>
<p><?=$msg;?></p>
<form action="<?=$_SERVER['PHP_SELF'];?>" method="post" name="form1">
<p>First name: <input type="text" name="fname"></p>
<p>Last name: <input type="text" name="lname"></p>
<p>Your favourite movie: <input type="text" name="favmov"></p>
<p>Your age: <input type="text" name="age"></p>
<p><input type="reset" value="Start over"> <input type="button" value="OK" name="send" onClick="check();" /></p>
</form>
</div>
</body>
</html>
http://thamob.no-ip.org/thamob/test.php <---- script in action!
Re: [PHP] My first PHP-script using $_POST! :)
Re: [PHP] My first PHP-script using $_POST! :)
EliteGM, don't really understand the functions in your script, could you please add comments or anything?
Really appreciated :)
Re: [PHP] My first PHP-script using $_POST! :)
PHP Code:
<?php
if ($_POST['send']) {
// variable telling the server what to echo
$message = "<div id='main'><p><b>Muhaha, i know all about you!</b></p>
<p> </p>
<p>Your name is <span class='input'>". $_POST['fname'] ." ". $_POST['lname'] ."!</span></p>
<p> </p>
<p>You're <span class='input'>". $_POST['age'] ."</span> years old and your favorite movie is <span class='input'>". $_POST['favmov'] .".</span>
<p> </p></div>";
// echoing the above message
echo $message;
// replaces certain charaters with other characters
$messagetodo = str_replace("<p> </p>", "
", $message);
$messagetodo = str_replace(" ", " ", $message);
$messagetodo = strip_tags($messagetodo);
// checks if there exists a dir named 'test'
// if not it creates it
if (!is_dir("test")) {
mkdir("test");
}
// variable telling the server what to call the created file
// here making a random number name
$name = "test/test". rand(1, 100) .".txt";
// opens the created file
$open = fopen($name, "w+");
if ($open) {
if (fwrite($open, $messagetodo)) {
// message that the server created file
$msg = "Successfully Created file: ". $name;
} else {
// error message that the server could not write the specified file
$msg = "Error in fwrite('". $open ."', '$message');";
}
} else {
// error message that the server could not open the specified file
$msg = "Error in fopen('". $name ."', 'w+');";
}
}
?>
<html>
<head>
<title>My first php-script width the GET and POST included!</title>
<style type="text/css">
p {
font-family: Verdana;
font-size: 18px;
color: #FFFFFF;
}
span.input {
font-family: Verdana;
font-size: 18px;
color: #FF0000;
}
#main {
margin: auto;
text-align: center;
}
</style>
<script type="text/javascript">
<!--
var c = 0;
function check()
{
frm = document.form1;
if (frm.fname.value == '') {
alert('Please fill in your name!');
c = 1;
}
if (frm.lname.value == '') {
alert('Please fill in your last name!');
c = 1;
}
if (frm.favmov.value == '') {
alert('Please fill in your favourite movie!');
c = 1;
}
if (frm.age.value == '') {
alert('Please fill in your age!');
c = 1;
}
if (c != 1) {
frm.submit();
}
}
-->
</script>
</head>
<body bgcolor="#000000" text="#FFFFFF">
<div id="main">
<h1>Fill out the form!</h1>
<p><?=$msg;?></p>
<form action="<?=$_SERVER['PHP_SELF'];?>" method="post" name="form1">
<p>First name: <input type="text" name="fname"></p>
<p>Last name: <input type="text" name="lname"></p>
<p>Your favourite movie: <input type="text" name="favmov"></p>
<p>Your age: <input type="text" name="age"></p>
<p><input type="reset" value="Start over"> <input type="button" value="OK" name="send" onClick="check();" /></p>
</form>
</div>
</body>
</html>
Then Illusion ;)
Re: [PHP] My first PHP-script using $_POST! :)
Thanks angrycat & EliteGM, that helped allot :P
One more thing, how do i tell th server to check that it is not more than 2 numbers in the "age"? Nothing except numbers.
Edit: No, the script didn't work.
Re: [PHP] My first PHP-script using $_POST! :)
PHP Code:
$age = $_POST['age'];
$age2 = intval($age); //String to Integer.
if ($age2 < 0) { //If it's under 0 (a.k.a not a number)
echo "Incorrect Age!";
} else if (substr($age, 2)) { //The only way I thought of it..
echo "You can't be over 99!";
}
//But maybe the best is to set the form field 'age' to maxlength="2"
EDITED:
Added intval, because the age is submitted as string.
Re: [PHP] My first PHP-script using $_POST! :)
How 'bout ;)
PHP Code:
elseif(strlen($age) < 3)
{
echo "blablabla over 99";
}
Re: [PHP] My first PHP-script using $_POST! :)
Yeah I know but my expirience with strlen is kinda bad xD
And I edited out a flaw in substr, if you put in '$var, 3' it'll to to nr4, it starts at zero.