Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Mar 7th, 2005, 6:05 AM   #1
stakeknife
Newbie
 
Join Date: Mar 2005
Posts: 16
Rep Power: 0 stakeknife is on a distinguished road
Functions, Forms and PHP

hi all,

beginner at PHP, doing an installation script for PHP project. at the moment im just throwing information from one page to another, which means the installation script is four pages long

1) enter mysql information
2) check mysql info, start session, connect to db, enter user information
3) check user info, create user account, enter site config information
4) check config info and enter into database

these are all functions in one page being called in the next

what i wanted to do for my own mental health was (if possible)

throw values from one function to another, allowing them to act like pages so i only have one install.php instead of install1.php, install2.php and so forth...



here is some example code

install.php


function step1()
{

if (!isset($_POST['mysql_server'])) $_POST['mysql_server'] = ' ';
if (!isset($_POST['mysql_user'])) $_POST['mysql_user'] = ' ';
if (!isset($_POST['mysql_pass'])) $_POST['mysql_pass'] = ' ';

echo '<p>All fields are required.</p>';
echo '<form action="testfunction4.php" method="post">';
echo '<table border="0" cellspacing="4" cellpadding="0">';
echo '<tr><td>mySQL Server:::: </td><td><input type="text" name="mysql_server" value="'.htmlspecialchars($_POST['mysql_server']).'"></td></tr>';
echo '<tr><td>mySQL Username:: </td><td><input type="text" name="mysql_user" value="'.htmlspecialchars($_POST['mysql_user']).'"></td></tr>';
echo '<tr><td>mySQL Password:: </td><td><input type="password" name="mysql_pass" value="'.htmlspecialchars($_POST['mysql_pass']).'"></td></tr>';
echo '<tr><td colspan="2"><input type="submit" value="Submit" name="submit"></td></tr>';
echo '</table>';
echo '</form>';
}



error check on next page

install2.php

<?php


require_once 'install.php';


if (isset($_POST['submit'])) {

$error_str = ''; // initialise $error_str as empty
if (empty($_POST['mysql_server'])) $error_str .= '<li>You did not spcify a server.</li>';
if (empty($_POST['mysql_user'])) $error_str .= '<li>You did not specify a username.</li>';
if (empty($_POST['mysql_pass'])) $error_str .= '<li>You did not specify a password.</li>';

if (strlen($_POST['mysql_server']) < 2) $error_str .= '<li>Server name must be longer than 1 letter.</li>';
if (strlen($_POST['mysql_user']) < 4) $error_str .= '<li>Username must be longer than 3 letters.</li>';
if (strlen($_POST['mysql_pass']) < 4) $error_str .= '<li>Password must be longer than 3 letters.</li>';

if (!empty($error_str)) {

echo '<p>There were errors in the information you entered, they are listed below:</p>';
echo '<ul>'.$error_str.'</ul>';

step1();
exit; // die
}

} else {

step1();
}

?>

followed by a connection to db and what not

any suggestions???
stakeknife is offline   Reply With Quote
Old Mar 7th, 2005, 9:52 AM   #2
Dizzutch
Professional Programmer
 
Dizzutch's Avatar
 
Join Date: Dec 2004
Location: Worcester, MA
Posts: 441
Rep Power: 4 Dizzutch is on a distinguished road
Send a message via ICQ to Dizzutch Send a message via AIM to Dizzutch Send a message via MSN to Dizzutch Send a message via Yahoo to Dizzutch
looks good, it takes a lot of time to make php efficient and to get everything done as quickly as possible, but for a first project this looks great. If you're using a form you can send it to the same page
ex.
<?
if($_POST['submitted1'] != true){
?>
<form action="index.php" method="post">
...
<input type=hidden name=submitted1 value=true>
</form>
<?
} else {
  process_information();
}
?>
you an do this multiple times in the same page.

good luck
Dizz
__________________
naked pictures of you | PFO F@H stats

Last edited by Dizzutch; Mar 7th, 2005 at 9:55 AM.
Dizzutch is offline   Reply With Quote
Old Mar 7th, 2005, 11:31 AM   #3
stakeknife
Newbie
 
Join Date: Mar 2005
Posts: 16
Rep Power: 0 stakeknife is on a distinguished road
thanks dizzutch, just trying to get everything spot on as i work on it...

just a few questions regarding this code

<?
if($_POST['submitted1'] != true){

// i presume this means that all the forms below must be filled in before submission

?>
<form action="index.php" method="post">

// do i put all of my html for the forms in here, this would be the mysql settings, user settings and site settings forms

...


<input type=hidden name=submitted1 value=true>
</form>
<?
} else {
process_information();

// i presume this has to be a function that will do things like check the integrity of the data etc etc

}
?>


will sessions cover this entire length of code?
stakeknife is offline   Reply With Quote
Old Mar 7th, 2005, 12:40 PM   #4
Dizzutch
Professional Programmer
 
Dizzutch's Avatar
 
Join Date: Dec 2004
Location: Worcester, MA
Posts: 441
Rep Power: 4 Dizzutch is on a distinguished road
Send a message via ICQ to Dizzutch Send a message via AIM to Dizzutch Send a message via MSN to Dizzutch Send a message via Yahoo to Dizzutch
right, the ... just represented the form code you already had, and the process_information() could be anything. Just giving you an example of the otherall structure.
$_POST['submitted'] != true, will just check whether the hidden form field has been received by the browser, so if the form has not been submitted, the code will not print the form and assume the form has been submitted and proceed to process to the next step.
good luck
Dizz
__________________
naked pictures of you | PFO F@H stats
Dizzutch is offline   Reply With Quote
Old Mar 7th, 2005, 4:21 PM   #5
Ooble
I eat cake for breakfast.
 
Ooble's Avatar
 
Join Date: Jul 2004
Location: In my box.
Posts: 4,434
Rep Power: 9 Ooble is on a distinguished road
Why not just give the submit button a value?
[php]<?
if ($_POST['submit']) {
process_info();
}
else {
?>
<form method="post" action="me.php">
... // insert inputs here
<input type="submit" id="submit" name="submit" value="Submit" />
</form>
<?
}
?>[/php]
__________________
Me :: You :: Them
Ooble is offline   Reply With Quote
Old Mar 8th, 2005, 5:28 AM   #6
stakeknife
Newbie
 
Join Date: Mar 2005
Posts: 16
Rep Power: 0 stakeknife is on a distinguished road
okay from my understanding of PHP at the moment the code given thus far will just show all of the forms on the one page, no?

What i need is each form to held in a function, i intend to use a switch statement to call each function and display that step as the user installs the software.

inside each of these switch statements (or functions) connections to the DB and modification of the application will be done based on the information given by the user.

im not really sure if this is possible, or am i barking up the right tree?

thanks again
stakeknife is offline   Reply With Quote
Old Mar 8th, 2005, 6:56 AM   #7
Dizzutch
Professional Programmer
 
Dizzutch's Avatar
 
Join Date: Dec 2004
Location: Worcester, MA
Posts: 441
Rep Power: 4 Dizzutch is on a distinguished road
Send a message via ICQ to Dizzutch Send a message via AIM to Dizzutch Send a message via MSN to Dizzutch Send a message via Yahoo to Dizzutch
sure, any code written in php you can stick in a function

[php]
<?
if ($_POST['submit']) {
process_info();
}
else {
print_form();
}

function print_form()
{
echo "<form method=\"post\" action=\"me.php\"><br>\n";
... // insert inputs here
echo "<input type=\"submit\" id=\"submit\" name=\"submit\" value=\"Submit\" /><br>\n";
echo "</form><br>\n";
}

function process_info()
{
/* connect to DB server */
/* run query */
/* do other stuff */
/* disconnect from database */
}
?>
[/php]
__________________
naked pictures of you | PFO F@H stats
Dizzutch is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 6:32 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC