![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Newbie
Join Date: Mar 2005
Posts: 16
Rep Power: 0
![]() |
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??? |
|
|
|
|
|
#2 |
|
Professional Programmer
|
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();
}
?>good luck Dizz Last edited by Dizzutch; Mar 7th, 2005 at 10:55 AM. |
|
|
|
|
|
#3 |
|
Newbie
Join Date: Mar 2005
Posts: 16
Rep Power: 0
![]() |
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? |
|
|
|
|
|
#4 |
|
Professional Programmer
|
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 |
|
|
|
|
|
#5 |
|
I eat cake for breakfast.
![]() ![]() ![]() ![]() Join Date: Jul 2004
Location: In my box.
Posts: 4,434
Rep Power: 9
![]() |
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] |
|
|
|
|
|
#6 |
|
Newbie
Join Date: Mar 2005
Posts: 16
Rep Power: 0
![]() |
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 |
|
|
|
|
|
#7 |
|
Professional Programmer
|
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] |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|