Wrox Programmer Forums

Need to download code?

View our list of code downloads.

| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
BOOK: Beginning PHP4/PHP 5 ISBN: 978-0-7645-4364-7; v5 ISBN: 978-0-7645-5783-5
This is the forum to discuss the Wrox book Beginning PHP4 by Wankyu Choi, Allan Kent, Chris Lea, Ganesh Prasad, Chris Ullman; ISBN: 9780764543647
Welcome to the p2p.wrox.com Forums.

You are currently viewing the BOOK: Beginning PHP4/PHP 5 ISBN: 978-0-7645-4364-7; v5 ISBN: 978-0-7645-5783-5 section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old September 13th, 2007, 10:48 AM
Registered User
 
Join Date: Sep 2007
Location: Ikeja, Lagos, Nigeria.
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default Call to function

The code below can't call the create_account function when submit button is clicked. What is actually wrong with the form?


<?php
//register.php
include_once "./common_db.inc";

function in_use($userid) {
   global $user_tablename;

   $query = "SELECT userid FROM $user_tablename WHERE userid = '$userid'";
   $result = mysql_query($query);
   if(!mysql_num_rows($result)) return 0;
   else return 1;
}

function register_form() {
global $userposition;
global $PHP_SELF;

$link_id = db_connect();
mysql_select_db("sample_db");
$position_array = enum_options('userposition', $link_id);
mysql_close($link_id);

?>


<CENTER><H3>Create your account!</H3></CENTER>
<FORM METHOD="POST" ACTION="<?php echo $PHP_SELF ?>">
<INPUT TYPE="HIDDEN" NAME="action">
  <DIV ALIGN="CENTER"><CENTER><TABLE BORDER="1" WIDTH="90%">
    <TR>
      <TH WIDTH="30%" NOWRAP>Desired ID</TH>
      <TD WIDTH="70%"><INPUT TYPE="TEXT" NAME="userid"
                             SIZE="8" MAXLENGTH="8"></TD>
    </TR>
    <TR>
      <TH WIDTH="30%" NOWRAP>Desired Password</TH>
      <TD WIDTH="70%"><INPUT TYPE="PASSWORD"
                             NAME="userpassword" SIZE="15"></TD>
    </TR>
    <TR>
      <TH WIDTH="30%" NOWRAP>Retype Password</TH>
      <TD WIDTH="70%"><INPUT TYPE="PASSWORD"
                             NAME="userpassword2" SIZE="15"></TD>
    </TR>
    <TR>
      <TH WIDTH="30%" NOWRAP>Full Name</TH>
      <TD WIDTH="70%"><INPUT TYPE="TEXT" NAME="username" SIZE="20"></TD>
    </TR>
    <TR>
      <TH WIDTH="30%" NOWRAP>Position</TH>
      <TD WIDTH="70%"><SELECT NAME="userposition" SIZE="1">
<?php


   for($i=0; $i < count($position_array); $i++) {
    if(!isset($userposition) && $i == 0) {
      echo "<OPTION SELECTED VALUE=\"". $position_array[$i] .
           "\">" . $position_array[$i] . "</OPTION>\n";
    }
    else if($userposition == $cposition_array[$i]) {
      echo "<OPTION SELECTED VALUE=\"". $position_array[$i] . "\">" .
                                        $position_array[$i] . "</OPTION>\n";
    }
    else {
      echo "<OPTION VALUE=\"". $position_array[$i] . "\">" .
                               $position_array[$i] . "</OPTION>\n";
    }
  }
?>


      </SELECT></TD>
    </TR>
    <TR>
      <TH WIDTH="30%" NOWRAP>Email</TH>
      <TD WIDTH="70%"><INPUT TYPE="TEXT" NAME="useremail" SIZE="20"
      </TD>
    </TR>
    <TR>
      <TH WIDTH="30%" NOWRAP>Profile</TH>
      <TD WIDTH="70%"><TEXTAREA ROWS="5" COLS="40"
                                NAME="userprofile"></TEXTAREA></TD>
    </TR>
    <TR>
      <TH WIDTH="30%" COLSPAN="2" NOWRAP>
        <INPUT TYPE="SUBMIT" VALUE="Submit">
        <INPUT TYPE="RESET" VALUE="Reset"></TH>
    </TR>
  </TABLE>
  </CENTER></DIV>
</FORM>
<?php
}

function create_account() {
   $userid = $_POST['userid'];
   $username = $_POST['username'];
   $userpassword = $_POST['userpassword'];
   $userpassword2 = $_POST['userpassword2'];
   $userposition = $_POST['userposition'];
   $useremail = $_POST['useremail'];
   $userprofile = $_POST['userprofile'];

   global $default_dbname, $user_tablename;


   if(empty($userid)) error_message("Enter your desired ID!");
   if(empty($userpassword)) error_message("Enter your desired password!");
   if(strlen($userpassword) < 4 ) error_message("Password too short!");
   if(empty($userpassword2))
                  error_message("Retype your password for verification!");
   if(empty($username)) error_message("Enter your full name!");
   if(empty($useremail)) error_message("Enter your email address!");
   if(empty($userprofile)) $userprofile = "No Comment.";

   if($userpassword != $userpassword2)
      error_message("Your desired password and retyped password mismatch!");



   $link_id = db_connect($default_dbname);

   if(in_use($userid))
         error_message("$userid is in use. Please choose a different ID.");


   $query = "INSERT INTO user VALUES(NULL, '$userid', password('$userpassword'),
                     '$username', '$userposition', '$useremail', '$userprofile')";
   $result = mysql_query($query);
   if(!$result) error_message(sql_error());


   $usernumber = mysql_insert_id($link_id);
   html_header();
?>


<CENTER><H3>
<?php echo $username ?>, thank you for registering with us!
</H3></CENTER>
<FORM METHOD="GET" ACTION="<?php echo $PHP_SELF ?>">
<INPUT TYPE="HIDDEN" NAME="action" VALUE="register">
<INPUT TYPE="HIDDEN" NAME="userid" VALUE="<? echo $userid ?>">
<DIV ALIGN="CENTER"><CENTER><TABLE BORDER="1" WIDTH="90%">
  <TR>
    <TH WIDTH="30%" NOWRAP>User Number</TH>
    <TD WIDTH="70%"><?php echo $usernumber ?></TD>
  </TR>
  <TR>
    <TH WIDTH="30%" NOWRAP>Desired ID</TH>
    <TD WIDTH="70%"><?php echo $userid ?></TD>
  </TR>
  <TR>
    <TH WIDTH="30%" NOWRAP>Desired Password</TH>
    <TD WIDTH="70%"><?php echo $userpassword ?></TD>
  </TR>
  <TR>
    <TH WIDTH="30%" NOWRAP>Full Name</TH>
    <TD WIDTH="70%"><?php echo $username ?></TD>
  </TR>
  <TR>
    <TH WIDTH="30%" NOWRAP>Position</TH>
    <TD WIDTH="70%"><?php echo $userposition ?></TD>
  </TR>
  <TR>
    <TH WIDTH="30%" NOWRAP>Email</TH>
    <TD WIDTH="70%"><?php echo $useremail ?></TD>
  </TR>
  <TR>
    <TH WIDTH="30%" NOWRAP>Profile</TH>
    <TD WIDTH="70%"><?php echo htmlspecialchars($userprofile) ?></TD>
  </TR>
</TABLE>
</CENTER></DIV>
<?php
    html_footer();
}

if (empty($_POST)) $_POST['action'] = "";

switch($_POST['action']) {
   case "register":
      create_account();
   break;
   default:
      html_header();
      register_form();
      html_footer();
   break;
}
?>


Here is code for the included "common_db.inc" file:


<?php
$dbhost = 'localhost';
$dbusername = 'root';

$default_dbname = 'sample_db';

$records_per_page = 5;

$user_tablename = 'user';
$access_log_tablename = 'access_log';

$MYSQL_ERRNO = '';
$MYSQL_ERROR = '';

$new_win_width = 600;
$new_win_height = 400;

function html_header(){

   global $new_win_width, $new_win_height;
   ?>
   <HTML>
   <HEAD>
   <SCRIPT LANGUAGE="JavaScript" TYPE="text/javascript">
   <!--
   function open_window(url) {
      var NEW_WIN = null;
      NEW_WIN = window.open ("", "RecordViewer");
      NEW_WIN.location.href = url;
   }
   //-->
   </SCRIPT>
   <TITLE>User Record Viewer</TITLE>
   </HEAD>
   <BODY>
   <?php
}


function html_footer() {
?>
</BODY>
</HTML>
<?php
}

function db_connect(){
   global $dbhost, $dbusername, $dbuserpassword, $default_dbname;
   global $MYSQL_ERRNO, $MYSQL_ERROR;

   $link_id = mysql_connect($dbhost, $dbusername, $dbuserpassword);
   if(!$link_id) {
      $MYSQL_ERRNO = 0;
      $MYSQL_ERROR = "Connection failed to the host $dbhost.";
      return 0;
   }
   else if(empty($dbname) && !mysql_select_db($default_dbname)) {
      $MYSQL_ERRNO = mysql_errno();
      $MYSQL_ERROR = mysql_error();
      return 0;
   }
   else return $link_id;
}

function sql_error() {
   global $MYSQL_ERRNO, $MYSQL_ERROR;

   if(empty($MYSQL_ERROR)) {
      $MYSQL_ERRNO = mysql_errno();
      $MYSQL_ERROR = mysql_error();
   }
   return "$MYSQL_ERRNO: $MYSQL_ERROR";
}

function error_message($msg) {
  // html_header();
   echo "<SCRIPT>alert(\"Error: $msg\");history.go(-1)</SCRIPT>";
   html_footer();
   exit;
}
function enum_options($field, $link_id) {
   $query = "SHOW COLUMNS FROM user LIKE '$field'";
   $result = mysql_query($query, $link_id);
   $query_data = mysql_fetch_array($result);

   if(eregi("('.*')", $query_data["Type"], $match)) {
      $enum_str = ereg_replace("'", "", $match[1]);
      $enum_options = explode(',', $enum_str);
      return $enum_options;
   } else return 0;
}

?>
Reply With Quote
  #2 (permalink)  
Old September 19th, 2007, 06:13 PM
Authorized User
 
Join Date: Sep 2007
Location: , , .
Posts: 56
Thanks: 0
Thanked 1 Time in 1 Post
Default

It could be that you do not have a value set for the hidden form field. I don't know what line # that is looking at your code but the following line needs a value specified.

<INPUT TYPE="HIDDEN" NAME="action">



http://mynameissteve.com
Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
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 Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Call a Function stealthdevil Access VBA 2 November 2nd, 2006 10:38 AM
Unable to call function... rupen Javascript How-To 1 July 1st, 2006 04:53 AM
How to call javascript function from VB function vinod_yadav1919 VB How-To 0 February 13th, 2006 06:03 AM
Help with Javascript function call fakeeyed Javascript 1 January 15th, 2006 04:27 AM



All times are GMT -4. The time now is 04:10 AM.


Powered by vBulletin®
Copyright ©2000 - 2019, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.