Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Westi on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

PHP Newbie 1

Status
Not open for further replies.

drtom

Technical User
Feb 26, 2004
369
CA
I am right new to PHP (just started studying it) so correct me if I'm wrong. I understand that it's very similar to asp or it is to serve similar purposes?
Could I create an HTML document and a PHP form and have the contents flow from the form into various fields within the HTML doc? I'm thinking of making up a "See an example of your web page here". Have the potential customer fill in a form, click and have those details flow into a page that shows his name etc. in the appropriate places on the page.
Am I making sense here?
Is there an example of the PHP code or a template that would demonstrate how to do this?
Thanks in advance
 
ASP and PHP are both server side scripting languages and are both used to accomplish similar tasks.

Your form would be a standard HTML form, but you would send it to a page containing your PHP script.

for example:
Code:
<form method='post' action='display_webpage.php'>
 <input type='text' name='whatever'></input>
</form>

in display_webpage.php you then need:
Code:
<?php
$whatever = $_POST['whatever'];
?>

And then wherever in the html code you want to print out the text that has been posted you would use:
Code:
<?php echo $whatever ?>
 
OK let me try to interpret...
I create a form with a field called "whatever". When a user enters something (a name) into field "whatever" and clicks submit the php script on the html page processes the information in the "whatever" field and opens a webpage.php and inserts the info in the “whatever” field into a location on the html page defined by the echo command?
How's that?
 
Almost. The page with the form on can just be a bog standard HTML page with no PHP on it whatsoever. When you click submit it will pass the values that have been entered to whatever you specify in the 'action' attribute of the form.

It is here, at wherever you've pointed your action to that the PHP comes into play. All of the text entered in your form fields will be available to you in your PHP script through $_POST['fieldname_goes_here'];

If you named one of the form fields 'title' for instance, and you wanted this to be the title of the users custom webpage when he clicks submit. You would set the action of the form to 'display_webpage.php' and then in display_webpage.php do:

Code:
<?php

$title = $_POST['title'];
?>

<html>
<head>
<title><?php echo $title ?></title>
</head>
<body>

etc.......

The page that you send the form info forward to will open when you click submit and you will see your custom title at the top of the browser window.
 
OK, I think I have the flow. I'll give it a go and post back when I have it ready (or more likely when I can't get it to work).
Many thanks.
Off to pick up the grandkids now...
 
Dweezel: When you refer to the title in your last post are you referring to the title in the browser's title bar? If so, I have missed something as there is no title in the browser's title bar when I submit the form.
Thanks

The html page is and the php code is:

<?php
$title = $_POST['title'];
?>
<html>
<head>
<title><?php echo $title ?></title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>
<body>
<?php
$Name = $_POST['Name'];
?>
<?php echo $Name ?>
</body>
</html>
 
that's because your form has no field in it called "title".

add
Code:
<input type="text" name="title" />

to your form and check it works.
 
Oh, YES...!!! This is so much fun. Now I'm going to start on the real thing. I'll let you know.
Thanks.
 
Can a form have two actions?
i.e. "preview.php" as well as "formail.php
 
fraid not. but there is almost always a way to achieve the send end. what are you trying to achieve?
 
I want to have the person's details that he fills out on my preview form sent to me so I send a follow up contact e-mail.
Also while I have you, is there any reason why "preview.php" cannot draw its setup / layout from "style.css"?
Thanks
 
no problem to either of those.

just include the mail script within the receiving page (see the mail() function on
for the css declaration either include it normally in the html header of the receiving page or you can "echo" the declaration into the html using php. however you want.

hth
Justin
 
Thanks, I found my error with the css and I will look at the mail script this afternoon.
 
I have another one for you. This may or may not be php related. I have a database of what I will refer to as "Qick Facts" which are one page articles that I would like to offer to a client base in the form of a .pdf document with their banner or logo. Using php can I insert the content from the database into a blank .pdf file (which has their letterhead, banner, logo) for my client to download and print?
 
you certainly can. make sure that you enable the php extension in your php.ini file (you may have to download the extension from php.net).

the exact functions for creating pdf files dynamically are not complex but you do need to get your head around them. they are in the php manual (online at
post if you have any specific problems with the functions. For one, I find them far from obvious!
 
Thanks, I'll work on it over the weekend and re-post
 
Well the real thing is working to the point where I can refine the content. The .php page that opens represents what a potential website would look like to the client. I now want to be able to have a few other pages within this preview website open with the customers details. I have created ficticious pages however, when I click on one a link to another page the client details are obviously missing as the original html form action is only to the first preview page I created. What's the trick in accessing the details from the other pages?
Thanks,

I also see that PHP.net is a vast site so it is now my winter project.
 
hmmm

it seems you're being gradually bitten by the php bug. the best thing about it is that it is an accessible language and that once you are over the initial conceptual hurdles the speed of acquisition accellerates.

so: by way of concept (i think this will help), php is a server-side language. although it started off just as a hypertext pre-processor it has become, for all purposes, a complete language. Its main use is still to render web-sites etc but there is no reason why it need do this. you could use it instead of vb or whatever for coding cross-platform local applications. Why is this an important conceptual step? becuase you should now see that rendering a page is just one of many possible outputs for your php program. by opening the receiving page you are instructing apache (or IIS etc) to run the php scripts in that page via the php interpreter. this php script could delete your entire hard drive with no difficulty. it could also open your front door for you (i have done this for a client of mine) or print Hello World to the screen or any combination of the above!

so: to your more general question: i think you need to start picking up database skills at the same time as php skills. most people start with mysql as it is (more or less) standards compliant and completely free for non-commercial use. You could use other databases like SQLITE etc with no problems either. I would recommend using the PEAR DB class as starting your database+php life by using abstraction layers is a good thing (abstraction layers means that your database code, in this instance, can be used on many different database types by changing only very minor things - often just one line).

construct-wise your problem is very easily solved with a database integration:

1. the user fills in his name and presses submit
2. the receiving script saves the user's name into a database along with some other relevant characteristics and a session id.
3. on each page you "include" a little script that looks up the user's name based on the session id and then use this to display relevant details.

however at this level of simplicity there are even easier ways to solve the problem using just sessions:

a very basic code snip :

Code:
insert this into the top of each page
<?
session_start();
$name = isset($_SESSION['name'])
        ? $_SESSION['name']
        : "Anonymous";
 
?>
//insert this into your form receiving script below the session_start() function.

<?
$_SESSION['name'] = isset($_POST['name'])
                    ? $_POST['name']
                    : "Anonymous";
?>

the "criteria ? if true : if false" construct is called the ternary operator and can be thought of as a quick if-then-else mechanism.

sessions don't really survive closing and reopening the browsers (although this can be worked around with cookies) and thus the concept of login scripts is born!

sorry this is a bit of a ramble...
 
NO, thats great. I'm off for the day but will be tackling this one in the morning. Again, thanks, I'll be back
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top