Jump to content


Photo

POST Logic Error


  • Please log in to reply
2 replies to this topic

#1 scott532

scott532
  • Members
  • PipPip
  • Member
  • 14 posts
  • LocationSanta Barbara, CA. (USA)

Posted 01 September 2006 - 04:02 PM

Hi, I'm trying to get the POST info to mysql, and there should be a confirmation message... specified in the create_account function. But once I POST my info, I'm only getting the register_form function. I'm thinking there's a problem with my switch statement at the end. Can someone tell me what's wrong with it.

<html>
<head>

<style type="text/css">
body {
font-family: 'Lucida Grande', Arial; font-size: 11px; color: #333;
background: url(img/bg_texture.jpg);
}
th {  text-align: right ; font-size: 12px; border-bottom: 1px solid #999; }
input, select, textarea {
background: #EEE;
}
</style></head><?php
  include_once "./common_db.inc";

  function register_form()
  {
    global $PHP_SELF;

    $link_id = db_connect();
    mysql_select_db("sample_db");
    mysql_close($link_id);

    ?>


<body>
  <center>
    <h3>Sign Up For A Free PRAVANA DVD!</h3>
  </center>

  <form method="post" action="<?php echo $PHP_SELF; ?>">
    <input type="hidden" name="action">

    <div align="center">
      <center>
        <table width="90%" cellpadding="6" border="1" bordercolor="purple" cellspacing="0" align="center">
          <tr>
            <th width="30%" nowrap>First Name:</th>

            <td width="70%"><input type="text" name="fname" size="8" maxlength="20"></td>
          </tr>

          <tr>
            <th width="30%" nowrap>Middle Initial:</th>

            <td width="70%"><input type="text" name="minitial" size="1" maxlength="1"></td>
          </tr>

          <tr>
            <th width="30%" nowrap>Last Name:</th>

            <td width="70%"><input type="text" name="lname" size="10" maxlength="30"></td>
          </tr>

          <tr>
            <th width="30%" nowrap>Salon Name:</th>

            <td width="70%"><input type="text" name="salonname" size="20" maxlength="30"></td>
          </tr>

          <tr>
            <th width="30%" nowrap>Salon Address: (<i>Example: 456 Main ST.</i>)</th>

            <td width="70%"><input type="text" name="minitial" size="20" maxlength="60"></td>
          </tr>

          <tr>
            <th width="30%" nowrap>State:</th>

            <td width="70%"><select name="state">
              <option value="">
                Select State
              </option>

              <option value="AK">
                Alaska(AK)
              </option>

              <option value="AL">
                Alabama(AL)
              </option>

              <option value="AR">
                Arkansas(AR)
              </option>

              <option value="AZ">
                Arizona(AZ)
              </option>

              <option value="CA">
                California(CA)
              </option>

              <option value="CO">
                Colorado(CO)
              </option>

              <option value="CT">
                Connecticut(CT)
              </option>

              <option value="DC">
                District of Columbia(DC)
              </option>

              <option value="DE">
                Delaware(DE)
              </option>

              <option value="FL">
                Florida(FL)
              </option>

              <option value="GA">
                Georgia(GA)
              </option>

              <option value="HI">
                Hawaii(HI)
              </option>

              <option value="IA">
                Iowa(IA)
              </option>

              <option value="ID">
                Idaho(ID)
              </option>

              <option value="IL">
                Illinois(IL)
              </option>

              <option value="IN">
                Indiana(IN)
              </option>

              <option value="KS">
                Kansas(KS)
              </option>

              <option value="KY">
                Kentucky(KY)
              </option>

              <option value="LA">
                Louisiana(LA)
              </option>

              <option value="MA">
                Massachusetts(MA)
              </option>

              <option value="MD">
                Maryland(MD)
              </option>

              <option value="ME">
                Maine(ME)
              </option>

              <option value="MI">
                Michigan(MI)
              </option>

              <option value="MN">
                Minnesota(MN)
              </option>

              <option value="MO">
                Missouri(MO)
              </option>

              <option value="MS">
                Mississippi(MS)
              </option>

              <option value="MT">
                Montana(MT)
              </option>

              <option value="NC">
                North Carolina(NC)
              </option>

              <option value="ND">
                North Dakota(ND)
              </option>

              <option value="NE">
                Nebraska(NE)
              </option>

              <option value="NH">
                New Hampshire(NH)
              </option>

              <option value="NJ">
                New Jersey(NJ)
              </option>

              <option value="NM">
                New Mexico(NM)
              </option>

              <option value="NV">
                Nevada(NV)
              </option>

              <option value="NY">
                New York(NY)
              </option>

              <option value="OH">
                Ohio(OH)
              </option>

              <option value="OK">
                Oklahoma(OK)
              </option>

              <option value="OR">
                Oregon(OR)
              </option>

              <option value="PA">
                Pennsylvania(PA)
              </option>

              <option value="RI">
                Rhode Island(RI)
              </option>

              <option value="SC">
                South Carolina(SC)
              </option>

              <option value="SD">
                South Dakota(SD)
              </option>

              <option value="TN">
                Tennessee(TN)
              </option>

              <option value="TX">
                Texas(TX)
              </option>

              <option value="UT">
                Utah(UT)
              </option>

              <option value="VA">
                Virginia(VA)
              </option>

              <option value="VT">
                Vermont(VT)
              </option>

              <option value="WA">
                Washington(WA)
              </option>

              <option value="WI">
                Wisconsin(WI)
              </option>

              <option value="WV">
                West Virginia(WV)
              </option>

              <option value="WY">
                Wyoming(WY)
              </option>

              <option value="">
                -----
              </option>

              <option value="AS">
                American Samoa(AS)
              </option>

              <option value="FM">
                Federated States of Micronesia(FM)
              </option>

              <option value="GU">
                Guam(GU)
              </option>

              <option value="MH">
                Marshall Islands(MH)
              </option>

              <option value="MP">
                Northern Mariana Islands(MP)
              </option>

              <option value="PW">
                Palau(PW)
              </option>

              <option value="PR">
                Puerto Rico(PR)
              </option>

              <option value="VI">
                Virgin Islands(VI)
              </option>
            </select></td>
          </tr>

          <tr>
            <th width="30%" nowrap>City:</th>

            <td width="70%"><input type="text" name="city" size="20" maxlength="40"></td>
          </tr>

          <tr>
            <th width="30%" nowrap>Zipcode:</th>

            <td width="70%"><input type="text" name="zip" size="12" maxlength="15"></td>
          </tr>

          <tr>
            <th width="30%" nowrap>Phone Number:</th>

            <td width="70%">( <input type="text" name="areacode" size="3" maxlength="3"> )-<input type="text" name="phonep1" size="3" maxlength="3">-<input type="text" name="phonep2" size="4" maxlength="4"></td>
          </tr>

          <tr>
            <th width="30%" nowrap>Email Address:</th>

            <td width="70%"><input type="text" name="email" size="20" maxlength="45"></td>
          </tr>

          <tr>
            <th width="30%" nowrap>Number of Stylists/Colorists:</th>

            <td width="70%"><input type="text" name="numstylists" size="4" maxlength="5"></td>
          </tr>

          <tr>
            <th width="30%" nowrap>Average Total Number of Hair Colors Done Each Day in The Salon:</th>

            <td width="70%"><input type="text" name="avgcolors" size="4" maxlength="10"></td>
          </tr>

          <tr>
            <th width="30%" nowrap>Hair Colors Currently Being Used in The Salon:</th>

            <td width="70%">
            <textarea name="haircolors" cols="25" rows="5">
</textarea></td>
          </tr>

          <tr>
            <th width="30%" nowrap>Which Pravana products are you using in the salon?:</th>

            <td width="70%">
            <textarea name="products" cols="25" rows="5">
</textarea></td>
          </tr>

          <tr>
            <th colspan="2" nowrap align="center"><input type="submit" value="Submit"> <input type="reset" value="Reset"></th>
          </tr>
        </table>
      </center>
    </div>
  </form><?php
  }

  function create_account()
  {
    $fname = $_POST['fname'];
    $minitial = $_POST['minitial'];
    $lname = $_POST['lname'];
    $salonname = $_POST['salonname'];
    $address = $_POST['address'];
    $state = $_POST['state'];
    $city = $_POST['city'];
    $zip = $_POST['zip'];
    $areacode = $_POST['areacode'];
    $phonep1 = $_POST['phonep1'];
    $phonep2 = $_POST['phonep2'];
    $email = $_POST['email'];
    $numstylists = $_POST['numstylists'];
    $avgcolors = $_POST['avgcolors'];
    $haircolors = $_POST['haircolors'];

    global $default_dbname;

    if(empty($fname)) {
      error_message("Please Enter Your First Name");
    }
    if(empty($lname)) {
      error_message("Please Enter Your Last Name");
    }
    if(empty($salonname)) {
      error_message("Please Enter The Name of Your Salon");
    }
    if(empty($address)) {
      error_message("Please Enter Your Salon's Address");
    }
    if(empty($state)) {
      error_message("Please Select a State");
    }
    if(empty($city)) {
      error_message("Please Enter a City");
    }
    if(empty($zip)) {
      error_message("Please Enter Your Zip Code");
    }
    if(empty($areacode)) {
      error_message("Please include an Area Code with Your Phone Number");
    }
    if(empty($phonep1)) {
      error_message("Please Enter a telephone number");
    }
    if(empty($phonep2)) {
      error_message("Please Enter a telephone number");
    }
    if(empty($email)) {
      error_message("Please Enter an email address");
    }
    if(empty($numstylists)) {
      error_message("Please tell us how many stylists/colorists you have");
    }
    if(empty($avgcolors)) {
      error_message("Please tell us the average total number of hair colors done each day in your salon");
    }
    if(empty($haircolors)) {
      error_message("Please tell us what hair colors are currently being used in your salon");
    }

      $link_id = db_connect($default_dbname);
      $query = "INSERT INTO freedvd VALUES('$fname', '$minitial', '$lname', '$salonname',
              '$address', '$state', '$city', '$zip', '$areacode', '$phonep1', '$phonep2',
              '$email', '$numstylists', '$avgcolors', '$haircolors')";
      $result = mysql_query($query);
      if(!$result){
        error_message(sql_error());
      }
  ?>

  <center>
    <h3>Thank You <?php echo $fname ?>. We will be mailing out our DVD to you, free of charge!</h3>
  </center><?php
    }
  if (empty($_POST)){
    $_POST['action'] = "";
  }
  switch($_POST['action']) {
    case "register":
      create_account();
    break;
    default:
      register_form();
    break;
  }
  ?>
</body>
</html>


#2 GingerRobot

GingerRobot
  • Staff Alumni
  • Advanced Member
  • 4,086 posts
  • LocationUK

Posted 01 September 2006 - 06:58 PM

I think it might just be a problem with your HTML. You have a hidden field called action, but it has no value. So change:
<input type="hidden" name="action">
to:
<input type="hidden" name="action" value="register">

Hopefully its something as simple as that. Cant see anything wrong with your switch statement.


#3 HeyRay2

HeyRay2
  • Members
  • PipPipPip
  • Advanced Member
  • 223 posts

Posted 01 September 2006 - 07:13 PM

Why even have a hidden action form element when you can just check if the form was submitted (if the user submitted the form, it can be assumed that they want to "register").

Take out this line in your form:

<input type="hidden" name="action">

And change these lines of code:

  if (empty($_POST)){
    $_POST['action'] = "";
  }
  switch($_POST['action']) {
    case "register":
      create_account();
    break;
    default:
      register_form();
    break;
  }

to this:

  if ($_POST){
      create_account();
  } else {
      register_form();
  }






0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users