Wrox Programmer Forums
Go Back   Wrox Programmer Forums > PHP/MySQL > Beginning PHP
|
Beginning PHP Beginning-level PHP discussions. More advanced coders should post to the Pro PHP forum.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Beginning PHP section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old July 31st, 2006, 11:58 AM
Registered User
 
Join Date: Jul 2006
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to PeterJB
Default submit data to db with html form....(help)

Hi there,

I have the book beginning php 5 and i'm trying to make a site where i can collect data from customers using an html form, i made a few changes to the script in the book to accomodate my own layout but when i press submit nothing happens, it just clears the form and no data is submitted. Can you tell me if you will, what could be wrong with this code?

<?php

include_once "../common_db.inc";

function in_use($username)
{
    global $user_tablename;

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

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

    $link_id = db_connect();
    mysql_select_db("newsite");
    $position_array = enum_options('plan', $link_id);
    mysql_close($link_id);

?>



<style type="text/css">
<!--
.style2 {font-style: italic}
.style3 {color: #FF0000}
.style5 {color: #FF0000; font-style: italic; }
-->
</style>



<CENTER><H3>Create a New Account!</H3></CENTER>

<FORM METHOD="POST" ACTION="<?php echo $PHP_SELF ?>">
<INPUT TYPE="HIDDEN" NAME="action">
  <DIV ALIGN="CENTER"><TABLE WIDTH="60%" BORDER="1" bordercolor="#0000FF" bgcolor="#99FFFF">
    <TR>
      <TH WIDTH="30%" NOWRAP>Username</TH>
      <TD WIDTH="70%"><INPUT TYPE="TEXT" NAME="username" SIZE="8" MAXLENGTH="8"></TD>
    </TR>
    <TR>
      <TH WIDTH="30%" NOWRAP>Userpassword</TH>
      <TD WIDTH="70%"><INPUT TYPE="PASSWORD" NAME="userpassword" SIZE="8" MAXLENGTH="8"></TD>
    </TR>
    <TR>
      <TH WIDTH="30%" NOWRAP>Retype password</TH>
      <TD WIDTH="70%"><INPUT TYPE="PASSWORD" NAME="userpassword2" SIZE="8" MAXLENGTH="8"></TD>
    </TR>
    <TR>
      <TH WIDTH="30%" NOWRAP>Full Name</TH>
      <TD WIDTH="70%"><INPUT TYPE="TEXT" NAME="fullname" SIZE="30"></TD>
    </TR>
    <TR>
      <TH WIDTH="30%" NOWRAP>Email</TH>
      <TD WIDTH="70%"><INPUT TYPE="text" NAME="email" SIZE="30">
      </TD>
    </TR>
    <TR>
      <TH WIDTH="30%" NOWRAP>Street / suburb </TH>
      <TD WIDTH="70%"><INPUT TYPE="text" NAME="street" SIZE="30">
      </TD>
    </TR>
        <TR>
      <TH WIDTH="30%" NOWRAP>City</TH>
      <TD WIDTH="70%"><INPUT TYPE="text" NAME="city" SIZE="30">
      </TD>
    </TR>
        <TR>
      <TH WIDTH="30%" NOWRAP>State / Province </TH>
      <TD WIDTH="70%"><INPUT TYPE="text" NAME="state" SIZE="30">
      </TD>
    </TR>
        <TR>
      <TH WIDTH="30%" NOWRAP>Zip / Postal code </TH>
      <TD WIDTH="70%"><INPUT TYPE="text" NAME="zip" SIZE="6">
      </TD>
    </TR>
        <TR>
      <TH WIDTH="30%" NOWRAP>Phone / Mobile </TH>
      <TD WIDTH="70%"><INPUT TYPE="text" NAME="phone" SIZE="20">
      </TD>
    </TR>
        <TR>
      <TH WIDTH="30%" NOWRAP>Chose a Plan</TH>
      <TD WIDTH="70%"><SELECT NAME="plan"><OPTION VALUE="Growth">growth</OPTION>
                                              <OPTION VALUE="Vehicle">vehicle</OPTION>
                                                <OPTION VALUE="Bond">bond</OPTION>
                    </SELECT> &nbsp;&nbsp;&nbsp;
<?php
    for($i=0; $i < count($plan_array); $i++) {
    if(!isset($plan) && $i == 0) {
      echo "<OPTION SELECTED VALUE=\"". $plan_array[$i] .
           "\">" . $plan_array[$i] . "</OPTION>\n";
    }
    else if($plan == $cplan_array[$i]) {
      echo "<OPTION SELECTED VALUE=\"". $plan_array[$i] . "\">" .
                                        $plan_array[$i] . "</OPTION>\n";
    }
    else {
      echo "<OPTION VALUE=\"". $plan_array[$i] . "\">" .
                               $plan_array[$i] . "</OPTION>\n";
    }
  }
?>
      </select></TD>
    </TR>
    <TR>
      <TH WIDTH="30%" NOWRAP><span class="style2">Bank details</span></TH>
      <TD WIDTH="70%"><span class="style3"><em>Where you would like to be paid</em></span>
      </TD>
    </TR>
            <TR>
      <TH WIDTH="30%" NOWRAP>Name of bank</TH>
      <TD WIDTH="70%"><INPUT TYPE="text" NAME="bankname" SIZE="50">
      </TD>
    </TR>
    <TR>
      <TH WIDTH="30%" NOWRAP>Name of account</TH>
      <TD WIDTH="70%"><INPUT TYPE="text" NAME="accountname" SIZE="50">
      </TD>
    </TR>
        <TR>
      <TH WIDTH="30%" NOWRAP>Account number</TH>
      <TD WIDTH="70%"><INPUT TYPE="text" NAME="accountnumber" SIZE="50">
      </TD>
        <TR>
      <TH WIDTH="30%" NOWRAP>Sort code</TH>
      <TD WIDTH="70%"><INPUT TYPE="text" NAME="sortcode" SIZE="6">
      </TD>
    </TR>
        <TR>
      <TH WIDTH="30%" NOWRAP>Swift code</TH>
      <TD WIDTH="70%"><INPUT TYPE="text" NAME="swiftcode" SIZE="11">
      </TD>
    </TR>
        <TR>
      <TH WIDTH="30%" NOWRAP>ABA Route</TH>
      <TD WIDTH="70%"><INPUT TYPE="text" NAME="abaroute" SIZE="9">
      </TD>
    </TR>
        <TR>
      <TH WIDTH="30%" NOWRAP><span class="style2">Bank address</span></TH>
      <TD WIDTH="70%"><span class="style5">Your banks physical address</span>
      </TD>
    </TR>
        <TR>
      <TH WIDTH="30%" NOWRAP>Street</TH>
      <TD WIDTH="70%"><INPUT TYPE="text" NAME="bankstreet" SIZE="30">
      </TD>
    </TR>
        <TR>
      <TH WIDTH="30%" NOWRAP>Suburb</TH>
      <TD WIDTH="70%"><input type="text" name="banksuburb" size="30"></TD>
    </TR>
        <TR>
      <TH WIDTH="30%" NOWRAP>zip / postal code</TH>
      <TD WIDTH="70%"><INPUT TYPE="text" NAME="bankzip" SIZE="30">
      </TD>
    </TR>
    <TR>
      <TH WIDTH="30%" NOWRAP>State / Province</TH>
      <TD WIDTH="70%"><INPUT TYPE="text" NAME="bankstate" SIZE="30">
      </TD>
    </TR>
    <TR>
      <TH WIDTH="30%" NOWRAP>Country</TH>
      <TD WIDTH="70%"><INPUT TYPE="text" NAME="bankcountry" SIZE="30">
      </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()
{
$username = $_POST['username'];
$fullname = $_POST['fullname'];
$userpassword = $_POST['userpassword'];
$userpassword2 = $_POST['userpassword2'];
$email = $_POST['email'];
$street = $_POST['street'];
$city = $_POST['city'];
$state = $_POST['state'];
$zip = $_POST['zip'];
$phone = $_POST['phone'];
$plan = $_POST['plan'];
$bankname = $_POST['bankname'];
$accountname = $_POST['accountname'];
$accountnumber = $_POST['accountnumber'];
$sortcode = $_POST['sortcode'];
$swiftcode = $_POST['swiftcode'];
$abaroute = $_POST['abaroute'];
$bankstreet = $_POST['bankstreet'];
$banksuburb = $_POST['banksuburb'];
$bankzip = $_POST['bankzip'];
$bankstate = $_POST['bankstate'];
$bankcountry = $_POST['bankcountry'];

    global $default_dbname, $user_tablename;

   if(empty($username)) {
   error_message("Enter your desired username!");
   }
   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($email)) {
   error_message("Enter your email address!");
   }
   if($userpassword != $userpassword2){
   error_message("Your desired password and retyped password mismatch!");
   }

   $link_id = db_connect($default_dbname);

   if(in_use($username)){
   error_message("$username is in use. Please choose a different one.");
}

// Insert information into the table "user"
$query = "INSERT INTO user VALUES
(NULL, '$username', '$fullname', password('$userpassword'), '$email', '$street', '$city', '$state',
'$zip', '$phone', '$plan', '$bankname', '$accountname', '$accountnumber', '$sortcode',
'$swiftcode', '$abaroute', '$bankstreet', '$banksuburb', '$bankzip', '$bankstate', '$bankcountry')";

   $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>

<DIV ALIGN="CENTER"><CENTER><TABLE BORDER="1" WIDTH="90%">
  <TR>
    <TH WIDTH="30%" NOWRAP>username</TH>
    <TD WIDTH="70%"><?php echo $usernname ?></TD>
  </TR>
  <TR>
    <TH WIDTH="30%" NOWRAP>fullname</TH>
    <TD WIDTH="70%"><?php echo $fullname ?></TD>
  </TR>
  <TR>
    <TH WIDTH="30%" NOWRAP>email</TH>
    <TD WIDTH="70%"><?php echo $email ?></TD>
  </TR>
  <TR>
    <TH WIDTH="30%" NOWRAP>state</TH>
    <TD WIDTH="70%"><?php echo $state ?></TD>
  </TR>
  <TR>
    <TH WIDTH="30%" NOWRAP>phone</TH>
    <TD WIDTH="70%"><?php echo $phone ?></TD>
  </TR>
  <TR>
    <TH WIDTH="30%" NOWRAP>bankname</TH>
    <TD WIDTH="70%"><?php echo $bankname ?></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;
}
?>

I would appreciate any help...

Thankyou.

Dislexics have more fnu
 
Old August 5th, 2006, 01:09 PM
Registered User
 
Join Date: Jul 2006
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default

hi peter,
instead of using function create account.write the code in two files
one is "form.php" file and another "save_form.php"
///////////form.php//////////////
<script>
function validate()
{
if(document.form1.username.value=="")
{
alert("Please enter your name !";
document.form1.username.focus;
return false;
}

return true;
}
</script>

<form name="form1" method="post" action="save_form.php"
onsubmit ="return validate();">
<input type="text" name="username">
<input type="hidden" name="submit_form">
<input type="submit" value="Submit">
<form>

////////////////save_form.php///////////
$username=$_POST['username'];
if(isset($_POST['submit_form']))
{
///connect with database//////////////////
$query="insert into user values('$username')";
mysql_query($query);
}
///////////////That's all, u can extend it//////////////

 
Old August 6th, 2006, 12:07 AM
Registered User
 
Join Date: Jul 2006
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to PeterJB
Default

Hi jets,

Thank you for your input, im beginning to see that there are lots of ways to do the same thing... (being new at this)

I'll give it a try and see how it goes, Thanks again.



Dislexics have more fnu





Similar Threads
Thread Thread Starter Forum Replies Last Post
Submit Form with QueryString Data Phil06 Javascript 4 June 15th, 2007 06:11 PM
Refresh Data when Submit Form ruso77 Infopath 0 April 13th, 2007 02:30 PM
submit form using javascript with html:button 752004 Struts 2 November 23rd, 2006 12:39 AM
How to send form data without pressing SUBMIT eapsokha Classic ASP Professional 2 March 1st, 2004 09:58 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.