Why use php? because it allows things to be generated very quickly - without lots of html pages. It enables users you choose to allow, to input their own content to the pages you want them to.
1) save these files as anyfilename.php - pick your names, upload them to a php enabled server and go to the url they are at...
2) understand <?, ?>, .php, //////, /* */, and ;
CODE
<?
?>
THis tells the web browser when to expect php coding. So... when writing php, this needs to encase all php code - or it will be formatted as html.
Naming a filename .php allows for <? and ?> to be expected - or it will show as html.
CODE
<?
//// hello - this line will be overlooked
/* hello
these lines will be overlooked*/
?>
both of these 'hello's' will not be formatted as php - these 2 bits of code exclude the contained text.
CODE
<?
print "hello"
?>
print tells the php to print the contained text - if a variable is used then ""'s are not usually needed, if "" are used then the content will be shown as html. The above WILL NOT WORK. Each line of php code needs to be ended with a ;.
3) printing basic html
CODE
<?
print 'hello world';
?>
open the file - it will show the words hello world with no formatting.
4) printing and setting variables
CODE
<?
$hello = 'hello world';
///setting variables
print $hello;
///printing variables
?>
5) if else with variables
CODE
<?
$a = $_GET['a'];
///setting variables (3)
if (isset($a)) {
print '<a href="?a=0">Click here</a><br>';
///printing html (2)
print "a is set to:".$a;
} else {
print '<a href="?a=1">Click here</a><br>';
///printing html (2)
print "a is empty";
}
?>
for a form... adjusting my last script from above...
6) create the form html - save as form.php.
CODE
<?
$form = <<<HTML
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>Form!</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>
<body>
<form action="?a=1" method="post" name="" id="">
<table width="50%" border="0" align="center" cellpadding="4" cellspacing="0">
<tr>
<td width="22%">Subject</td>
<td width="78%"><input name="value" type="text" id="value"></td>
</tr>
</table>
</form>
</body>
</html>
<br>
HTML;
?>
that basically mates a text box to enter something into - which posts to ?a=1
then save the following as anythingyouwant.php .... load it...and should work as a simple form.
CODE
<?
include 'form.php';
///include the form we just made - so that you can print it when you need to.
$a = $_GET['a'];
///setting variables - has the script been submitted yet? - get the value from the url
if (isset($a)) {
///the script has been submitted
$value = $_POST['value'];
///retrieve the posted data.
print '<a href="/?">Click here</a><br>';
///printing html - return to script not submitted
print "value is set to:".$value;
} else {
////form isnt submitted - so print it.
print "a is empty<br>";
print "therefore include the form<br><br>";
///printing html (2)
print $form;
}
?>
should work... unless i missed off a ; or something silly
if you want to check the field to make it required - and show a form with a message if its not filled in, then this would be the edited version.....
CODE
<?
include 'form.php';
///include the form - so that you can print it when you need to.
$a = $_GET['a'];
///setting variables - has the script been submitted yet?
if (isset($a)) {
///the script has been submitted
$value = $_POST['value'];
///retrieve the posted data.
if(isset($value)) {
/////////FIELD SET?////////////
print '<a href="/?">Click here</a><br>';
///printing html - return to script not submitted
print "value is set to:".$value;
} else {
//////FEILD NOT SET - show form again, with message to fill it out///////
print "The required fields are empty<br>";
print "therefore include the form<br><br>";
///printing html (2)
print $form;
}
} else {
////form isnt submitted - so print it.
print "a is empty<br>";
print "therefore include the form<br><br>";
///printing html (2)
print $form;
}
?>
then learn mysql and your sorted
-----------------------
see database thread

