Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 9 of 9
  1. #1
    New to the CF scene
    Join Date
    Nov 2010
    Posts
    4
    Thanks
    3
    Thanked 0 Times in 0 Posts

    PHP/MySQL Dropdown Help Needed

    Hey all i need some help here.

    i have a drop down ..
    Code:
    <td class="fieldcell"><select  name="os" id="os">
              <option selected="selected"><?php echo($os); ?></option>
              <option value="Windows 98">Windows 98</option>
              <option value="Windows ME">Windows ME</option>
              <option value="Windows 2000">Windows 2000</option>
              <option value="Windows XP">Windows XP</option>
              <option value="Windows Vista">Windows Vista</option>
              <option value="Windows Seven">Windows Seven</option>
            </select></td>
    where i have <option selected="selected"><?php echo($os); ?></option>
    i would like to have the seleced option match what is in the mysql table.

    i have a novice handle on php and mysql i just can't seem to make it work. Any help please and thank you.

  • #2
    Senior Coder
    Join Date
    Jun 2008
    Location
    New Jersey
    Posts
    2,536
    Thanks
    45
    Thanked 259 Times in 256 Posts
    So you mean select the correct OS rather create a new option, right? If the value matches the values given exactly, the simple looping method I go with is:

    PHP Code:
    <td class="fieldcell"><select  name="os" id="os">
    <?php
        
    foreach(array('Windows 98''Windows ME''Windows 2000''Windows XP''Windows Vista''Windows Seven') as $osOption)
        echo 
    "    <option value=\"{$osOption}\"".($os == $osOption?' selected="selected"':'').">{$osOption}</option>";
    ?>
    </select></td>
    Otherwise go longform like you have there:

    PHP Code:
    <td class="fieldcell"><select  name="os" id="os">
        <option value="Windows 98"<?php if ($os == 'Windows 98') echo ' selected="selected"'?>>Windows 98</option>
        <option value="Windows ME"<?php if ($os == 'Windows ME') echo ' selected="selected"'?>>Windows ME</option>
        <option value="Windows 2000"<?php if ($os == 'Windows 2000') echo ' selected="selected"'?>>Windows 2000</option>
        <option value="Windows XP"<?php if ($os == 'Windows XP') echo ' selected="selected"'?>>Windows XP</option>
        <option value="Windows Vista"<?php if ($os == 'Windows Vista') echo ' selected="selected"'?>>Windows Vista</option>
        <option value="Windows Seven"<?php if ($os == 'Windows Seven') echo ' selected="selected"'?>>Windows Seven</option>
    </select></td>

  • Users who have thanked Keleth for this post:

    jay7981 (11-10-2010)

  • #3
    New to the CF scene
    Join Date
    Nov 2010
    Posts
    4
    Thanks
    3
    Thanked 0 Times in 0 Posts
    OK Great that works wonders ... now for the hard part, how can i do the same thing with $_POST data,

    example i have a registration form that when submitted it validates fields and builds an error report if there are errors... in the form itself i have the $_POST data echoing as the default value.... so if errors are present i have the processor show the form with the list of errors and the form fields presented with the values they submitted.

    here is the full form (so far im not done yet)
    PHP Code:
    <?php 
    @$name addslashes($_POST['name']);
    @
    $email addslashes($_POST['email']);
    @
    $pass1 addslashes($_POST['password']);
    @
    $pass2 addslashes($_POST['password2']);
    @
    $pass md5($pass1);
    @
    $phone addslashes($_POST['phone']);
    @
    $os addslashes($_POST['os']);
    @
    $manu addslashes($_POST['manu']);
    @
    $model addslashes($_POST['model']);
    @
    $sn addslashes($_POST['sn']);
    @
    $inet addslashes($_POST['inet']);
    @
    $date addslashes($_POST['date']);
    @
    $time addslashes($_POST['time']);
    ?>

    <table width="100%" border="0" cellspacing="3" cellpadding="2">
      <form action="../formprocess.php" method="post" enctype="multipart/form-data" name="remotereg">
        <tr>
          <td colspan="2"><h2>Remote Services Registration Form</h2></td>
        </tr>
        <tr>
          <td colspan="2" align="center" valign="middle">&nbsp;</td>
        </tr>
        <tr>
          <td class="labelcell">*Name:</td>
          <td class="fieldcell"><input name="name" type="text"  id="name" value="<?php echo($name); ?>" /></td>
        </tr>
        <tr>
          <td class="labelcell">*Email:</td>
          <td class="fieldcell"><input name="email" type="text"  id="email" value="<?php echo($email); ?>" /></td>
        </tr>
        <tr>
          <td class="labelcell">*Password:</td>
          <td class="fieldcell"><input type="text" name="password" id="password" /></td>
        </tr>
        <tr>
          <td class="labelcell">*Confirm Password:</td>
          <td class="fieldcell"><input type="text" name="password2" id="password2" /></td>
        </tr>
        <tr>
          <td class="labelcell">*Phone:</td>
          <td class="fieldcell"><input name="phone" type="text"  id="phone" value="<?php echo($phone); ?>" /></td>
        </tr>
        <tr>
          <td class="labelcell">*Select Operating System:</td>
          <td class="fieldcell"><select  name="os" id="os">
    <?php
        
    foreach(array('Windows 98''Windows ME''Windows 2000''Windows XP''Windows Vista''Windows Seven') as $osOption)
        echo 
    "    <option value=\"{$osOption}\"".($os == $osOption?' selected="selected"':'').">{$osOption}</option>";
    ?>
    </select></td>
        </tr>
        <tr>
          <td class="labelcell">*Select Manufacturer:</td>
          <td class="fieldcell"><select  name="manu" id="manu">
              <option value="" selected="selected">- Select -</option>
              <option value="Acer">Acer</option>
              <option value="Alienware">Alienware</option>
              <option value="Asus">Asus</option>
              <option value="Averatec">Averatec</option>
              <option value="Compaq">Compaq</option>
              <option value="Dell">Dell</option>
              <option value="eMachines">eMachines</option>
              <option value="Hewlett-Packard">Hewlett-Packard</option>
              <option value="IBM">IBM</option>
              <option value="Lenovo">Lenovo</option>
              <option value="Sony">Sony</option>
              <option value="Toshiba">Toshiba</option>
              <option value="Velocity Micro">Velocity Micro</option>
            </select></td>
        </tr>
        <tr>
          <td class="labelcell">*Model Number:</td>
          <td class="fieldcell"><input  type="text" name="model" id="model" /></td>
        </tr>
        <tr>
          <td class="labelcell">*Serial Number:</td>
          <td class="fieldcell"><input  type="text" name="sn" id="sn" /></td>
        </tr>
        <tr>
          <td class="labelcell">*Select Internet Type:</td>
          <td class="fieldcell"><select  name="inet" id="inet">
              <option selected="selected">- Select -</option>
              <option value="Cable">Cable</option>
              <option value="Dial-Up">Dial-Up</option>
              <option value="DSL">DSL</option>
              <option value="Fiber Optic">Fiber Optic</option>
              <option value="ISDN">ISDN</option>
              <option value="T1">T1</option>
            </select></td>
        </tr>
        <tr>
          <td><span class="article_content">Fields marked with &quot;*&quot; are required.</span></td>
          <td>&nbsp;</td>
        </tr>
        <tr>
          <td colspan="2" class="article_content"><div align="center"><span class="fieldcell">
            <?php 
          
    require_once(dirname(__FILE__) . "/scripts/recaptchalib.php");
          
    $publickey "BLEEP";
          echo 
    recaptcha_get_html($publickey);
          
    ?>
          </span></div></td>
        </tr>
        <tr>
          <td class="article_content">&nbsp;</td>
          <td>&nbsp;</td>
        </tr>
        <tr>
          <td class="article_content">By clicking &quot;Submit&quot; you are agreeing to the <a href="#" target="_blank">Terms of Service</a> for Remote Desktop Services.</td>
          <td><input type="hidden" name="date" value="<?php echo(date("Y/m/d")); ?>">
            <input type="hidden" name="time" value="<?php echo(date("h:i A")); ?>">
            <input  name="submit[signup]" type="submit" class="button" id="submit" value="Submit"></td>
        </tr>
      </form>
    </table>
    and here is the processor
    PHP Code:
    <?php
    // Receiving variables
    @$ip$_SERVER['REMOTE_ADDR'];
    @
    $name addslashes($_POST['name']);
    @
    $email addslashes($_POST['email']);
    @
    $pass1 addslashes($_POST['password']);
    @
    $pass2 addslashes($_POST['password2']);
    @
    $pass md5($pass1);
    @
    $phone addslashes($_POST['phone']);
    @
    $os addslashes($_POST['os']);
    @
    $manu addslashes($_POST['manu']);
    @
    $model addslashes($_POST['model']);
    @
    $sn addslashes($_POST['sn']);
    @
    $inet addslashes($_POST['inet']);
    @
    $date addslashes($_POST['date']);
    @
    $time addslashes($_POST['time']);

    // Validation
    if (strlen($name) <=3)
    {
        
    $error .= "Name is too short! <br />";
    }

    if (
    strlen($name) == )
    {
        
    $error .= "Name is too blank! <br />";
    }

    if (! 
    ereg('[A-Za-z0-9_-]+\@[A-Za-z0-9_-]+\.[A-Za-z0-9_-]+'$email))
    {
        
    $error .= "Not a valid Email address! <br />";
    }

    if (
    strlen($email) == )
    {
        
    $error .= "Your Email address is blank! <br />";
    }

    if (
    $pass1==$pass2)
    {
        
    //passwords match so continue with code
    }else{
        
    $error .= "Password and Confirm password do not match! <br />";
    }

    if( 
    strlen($pass1) <=
    {
        
    $error .= "Password too short! <br />";
    }

    if( 
    strlen($pass1) >=13 
    {
        
    $error .= "Password too long! <br />";
    }

    if( !
    preg_match("#[0-9]+#"$pass1) ) 
    {
        
    $error .= "Password must include at least one number! <br />";
    }


    if( !
    preg_match("#[a-z]+#"$pass1) ) 
    {
        
    $error .= "Password must include at least one letter! <br />";
    }


    if( !
    preg_match("#[A-Z]+#"$pass1) ) 
    {
        
    $error .= "Password must include at least one capitol letter <br />";
    }

    if (
    strlen($phone) <7)
    {
        
    $error .= "Phone number is too short! <br />";
    }

    if (
    strlen($phone) >12)
    {
        
    $error .= "Phone number is too long! <br />";
    }

    if (
    strlen($phone) == )
    {
        
    $error .= "Phone number is blank! <br />";
    }

    if ( 
    strcasecmp($os,"win98") != && strcasecmp($os,"winme") != && strcasecmp($os,"win2000") != && strcasecmp($os,"winxp") != && strcasecmp($os,"vista") != && strcasecmp($os,"seven") != )
    {
        
    $error .= "Operating System is invalid! <br />";
    }

    if (
    strlen($os) == )
    {
        
    $error .= "Operating system is blank! <br />";
    }

    if ( 
    strcasecmp($manu,"Acer") != && strcasecmp($manu,"Alienware") != && strcasecmp($manu,"Asus") != && strcasecmp($manu,"Averatec") != && strcasecmp($manu,"Compaq") != && strcasecmp($manu,"Dell") != && strcasecmp($manu,"eMachines") != && strcasecmp($manu,"Hewlett-Packard") != && strcasecmp($manu,"IBM") != && strcasecmp($manu,"Lenovo") != && strcasecmp($manu,"Sony") != && strcasecmp($manu,"Toshiba") != && strcasecmp($manu,"Velocity Micro") != )
    {
        
    $error .= "Manufactuer is invalid! <br />";
    }

    if (
    strlen($manu) == )
    {
        
    $error .= "Manufactuer is blank! <br />";
    }

    if (
    strlen($model) == )
    {
        
    $error .= "Model is blank! <br />";
    }

    if (
    strlen($sn) == )
    {
        
    $error .= "Serial number is blank! <br />";
    }

    if ( 
    strcasecmp($inet,"Cable") != && strcasecmp($inet,"Dial-Up") != && strcasecmp($inet,"DSL") != && strcasecmp($inet,"Fiber Optic") != && strcasecmp($inet,"ISDN") != && strcasecmp($inet,"T1") != )
    {
        
    $error .= "Internet type is invalid! <br />";
    }

    if (
    strlen($inet) == )
    {
        
    $error .= "Internet type is blank! <br />";
    }

    if(
    $error){
        echo 
    "<h2>Account Creation Failure:</h2> $error";
        include(
    "./includes/remoteregform.php");
    die();
    } else {
    //Sending Email to form owner
    $VCRheader "From: $email\n"
      
    "Reply-To: $email\n";
    $VCRsubject "Remote Services Registration From $name";
    $VCRemail_to "BLeep";
    $VCRmessage "Visitor's IP: $ip\n"
    "name: $name\n"
    "email: $email\n"
    "phone: $phone\n"
    "os: $os\n"
    "manu: $manu\n"
    "model: $model\n"
    "sn: $sn\n"
    "inet: $inet\n"
    "date: $date\n"
    "time: $time\n";
    @
    mail($VCRemail_to$VCRsubject ,$VCRmessage ,$VCRheader ) ;

    //Sending auto respond Email to visitor
    $VCRheader "From: BLEEP\n"
      
    "Reply-To: BLEEP\n";
    $VCRsubject "Thank you for registering...";
    $VCRemail_to "$email";
    $VCRmessage "$name, \n"
    "Thank you for registering for remote desktop services from BLEEP. \n"
    "\n"
    "Thank You for signing up with BLEEP. \n"
    "Your remote service account has been created so that you can login and request a remote session.\n"
    "\n"
    "To Request a remote session: \n"
    "1. Goto BLEEP \n"
    "2. Click Login\n"
    "3. Enter the above UserName and Password\n"
    "4. Place the mouse over Remote Services\n"
    "5. Click on Request Remote Session\n"
    "6. Fill out the Form\n"
    "7. Click submit\n"
    "\n"
    "After I recieve the request I will contact you via phone and start the remote session. \n"
    "\n"
    "Thank you; \n"
    "BLEEP \n"
    "BLEEP";
    @
    mail($VCRemail_to$VCRsubject ,$VCRmessage ,$VCRheader ) ;

    //saving record to MySQL database
    //changing date formats
    if (strstr$date,"/"))
    {
    $VCRSeparator "/";
    }
    if (
    strstr$date,"-"))
    {
    $VCRSeparator "-";
    }
    @
    $VCRarr explode($VCRSeparator$date);
    @
    $date  $VCRarr[2]."-".$VCRarr[0]."-".$VCRarr[1];


    @
    $VCRstrQuery "INSERT INTO `remote_accounts`(`name`,`email`,`password`,`username`,`phone`,`os`,`manu`,`model`,`serial`,`inet`,`date`,`time`)VALUES (\"$name\",\"$email\",\"$pass\",\"$email\",\"$phone\",\"$os\",\"$manu\",\"$model\",\"$sn\",\"$inet\",\"$date\",\"$time\")" ;
    @
    $VCRhost "BLEEP";
    @
    $VCRuser "BLEEP";
    @
    $VCRpw "BLEEP";
    @
    $VCRdb "BLEEP";
    $VCRlink mysql_connect($VCRhost$VCRuser$VCRpw);
    if (!
    $VCRlink) {
     die(
    'Could not connect: ' mysql_error());
    }
    $VCRdb_selected mysql_select_db($VCRdb$VCRlink);
    if (!
    $VCRdb_selected) {
    die (
    'Can not use $VCRdb : ' mysql_error());
    }

    //insert new record
    $VCRresult mysql_query($VCRstrQuery);
    if (!
    $VCRresult) {
     die(
    'Invalid query: ' mysql_error());
    }
    mysql_close($VCRlink);

     echo(
    "<p align='center'><font face='Arial' size='3' color='#000000'>Thank You,<br /> You will recieve an email containing next step information.</font></p>");
    }
    ?>
    If there is an easier way to do this please i am all ears.

  • #4
    Senior Coder
    Join Date
    Jun 2008
    Location
    New Jersey
    Posts
    2,536
    Thanks
    45
    Thanked 259 Times in 256 Posts
    Is there an easier way then what? Checking each step? No, if there was, there'd be no need to program... you have to put in what you're looking for, and that means each step of validation, be it checking if something is an integer, or if its a string, longer then 4 characters, shorter then 25, only made of alphanumeric characters, underscores, or dots, and not containing any illicit words.

  • Users who have thanked Keleth for this post:

    jay7981 (11-10-2010)

  • #5
    New to the CF scene
    Join Date
    Nov 2010
    Posts
    4
    Thanks
    3
    Thanked 0 Times in 0 Posts
    no, i wasn't wanting an easier way to validate... i was wanting an easier way to retain the $_POST data for when there is an error and the form is reloaded that the form will have the previously typed info.

  • #6
    Senior Coder
    Join Date
    Jun 2008
    Location
    New Jersey
    Posts
    2,536
    Thanks
    45
    Thanked 259 Times in 256 Posts
    Oh, I apologize for the misconstrution. Why not, in the form validation, if there's an error, store the POST into a SESSION var and restore from the session? That's how I do it.

  • #7
    Senior Coder
    Join Date
    Jul 2009
    Location
    South Yorkshire, England
    Posts
    2,318
    Thanks
    6
    Thanked 304 Times in 303 Posts
    Put it all into one script. Something along the lines of:

    Code:
    <?php
    
    if (isset($_POST['name']))
    {
        [processing code here]
    }
    
    if (!isset($_POST['name']) || (isset($error) && $error))
    {
        [form code here]
    }
    
    ?>
    You have already assigned the vars, so just put the vars in the value fields of the form. Btw, ditch all of the error suppression and do things properly. You should be sorting any possible errors, not suppressing them. Rarely is there good reason for suppressing errors.

  • Users who have thanked MattF for this post:

    jay7981 (11-10-2010)

  • #8
    New to the CF scene
    Join Date
    Nov 2010
    Posts
    4
    Thanks
    3
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by MattF View Post
    You have already assigned the vars, so just put the vars in the value fields of the form. Btw, ditch all of the error suppression and do things properly. You should be sorting any possible errors, not suppressing them. Rarely is there good reason for suppressing errors.
    What do you mean about the error suppresion im not suppressing any errors just building an array and displaying all at once

  • #9
    Senior Coder
    Join Date
    Jul 2009
    Location
    South Yorkshire, England
    Posts
    2,318
    Thanks
    6
    Thanked 304 Times in 303 Posts
    Code:
    @mail
    The @ symbol is the error suppressant.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •