...

View Full Version : PHP/MySQL Dropdown Help Needed



jay7981
11-10-2010, 03:30 AM
Hey all i need some help here.

i have a drop down ..


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

Keleth
11-10-2010, 03:45 AM
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:



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


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

jay7981
11-10-2010, 04:15 AM
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
@$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
// 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) == 0 )
{
$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) == 0 )
{
$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) <=5 )
{
$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) == 0 )
{
$error .= "Phone number is blank! <br />";
}

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

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

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

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

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

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

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

if (strlen($inet) == 0 )
{
$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.

Keleth
11-10-2010, 04:24 AM
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.

jay7981
11-10-2010, 04:38 AM
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.

Keleth
11-10-2010, 04:51 AM
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.

MattF
11-10-2010, 05:06 AM
Put it all into one script. Something along the lines of:



<?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.

jay7981
11-10-2010, 07:12 AM
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

MattF
11-10-2010, 07:29 AM
@mail


The @ symbol is the error suppressant.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum