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 bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

2 questions, newbie to PHP/HTML programming.

Status
Not open for further replies.

bdina

Programmer
Jun 17, 2003
47
US
I'll start off with the easier of the two questions:

1) I am not sure that I am really understanding how a form works in conjunction with PHP. Currently I have some PHP scripting that will enter information into a database. The script does work, it will enter information via the website into the database table. Where I am not comfortable is how this is happening exactly. I have a form where I have defined an action, which I am not sure what that is exactly, but am guessing that the action is where you define what happens on submission. Then when the user clicks the button submit or presses enter it executes some PHP that makes the addition... really, I am just not sure how this code is working which makes me a poor programmer... here is the code:

echo &quot;<p><b>Please submit a task</b><br>&quot;;

if ($submit)
{
$connection = mysql_connect($host, $user, $password) or die(&quot;Error! Could not connect to database!!\n&quot;);
$db = mysql_select_db($database, $connection) or die(&quot;Error! Could not select table!!\n&quot;);

$taskStat = &quot;1&quot;;

$query = &quot;INSERT INTO taskTable (task_Name, task_Status, task_Detail, task_Date) VALUES ('$taskName','$taskStat','$taskDetail', '$taskDate')&quot;;
$result = mysql_query($query) or die(&quot;Error! Could not submit data to table!!\n&quot;);
?>
<form method=&quot;post&quot; action=&quot;<?php echo $PHP_SELF?>&quot;>
Task Name:<input type=&quot;text&quot; size=&quot;25&quot; name=&quot;taskName&quot;><br>
Task Detail:<input type=&quot;text&quot; size=&quot;75&quot; name=&quot;taskDetail&quot;><br>
<br><input type=&quot;Submit&quot; name=&quot;submit&quot; value=&quot;Submit Task&quot;>
</form>
<?php
}
else
{
?>
<form method=&quot;post&quot; action=&quot;<?php echo $PHP_SELF?>&quot;>
Task Name:<input type=&quot;text&quot; size=&quot;25&quot; name=&quot;taskName&quot;><br>
Task Detail:<input type=&quot;text&quot; size=&quot;75&quot; name=&quot;taskDetail&quot;><br>
<br><input type=&quot;Submit&quot; name=&quot;submit&quot; value=&quot;Submit Task&quot;>
</form>
<?php
}
?>


that is the whole page minus the actual database connection stuff.

So really, my question is how exactly does that code work, the environment seems odd because I am not sure where and when the code executes, as far as how does the flow control work or whatever... meaning if I were using some other language like C, if I ask for input, the program waits for input, this environment is not the same however.

2) I would like to add logins to a site, I am assuming I can do this by having user names and passwords in the database and then have a page that takes the username and password and checks them against the database, then will forward you along to the pages on that site, however there would be some type of code in each page possibly included using includes or the header() function in PHP that makes sure you are an authenticated user somehow, maybe even using cookies... not sure... anyway, this might be too tough a question to be answered.

--Bryan
 
To your first question:

PHP is a server side technology. That means the code is run on the server and the result (if there is output) sent to the browser.

A form is HTML interpreted by the client, the web browser. The <form> tags action attribute tells the browser where to transmit the values defined in the form by user input.

The receiving PHP code takes the data from the form and runs the script with the ascertained values.

Your example has several features:
The if statement looks if the form was submitted by checking for $submit -> note the submit button in the HTML is named 'submit'. Be aware that your example assumes that register_globals is on. However, since PHP 4.2.0 it is off by default because of security issues. You should use $_POST['submit'] instead (or $HTTP_POST_VARS['submit'] if your PHP is older).

If the form was not submitted it only prints out the form. If the submit button was pressed it executes the first part which adds the values to the database.

A PHP page is not like a program that performs some actions after waiting for input. There is no continuous connection between the server and the client. That's why the form has to have the action attribute to know where to send the data.
The PHP script on the other end of the always one-way conversation decides from the data sent what to do.

PHP means pre-hypertext-processor. The code is executed before the HTML is sent to the client. Once it has been sent to the client (browser) PHP's role is over. When the data is submitted PHP can pick up the values and process them.

Question 2:
PHP allows for sessions. That means you can store a package of variables on the server referenced by a session identifier. It works pretty much by itself, this just ecplains how it works. Sessions allow the server to be aware of certain settings over several accesses. Ther client server connection is like sending letters back and forth. The server knows from an enclosed session ID who sent the request and use the locally stored values in processing the request.
This is helpful for loging people in a site. The session environment can be used to give them access to the scripts without having to prove their identity for every page access.

If you need more info, let me know.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top