/    Sign up×
Community /Pin to ProfileBookmark

setting default option

Hi guys

I would like to set a default value for the drop down menu incase a user forgets to select an option from there. At the moment it is just inserting “choose option/category” if the user forgets to select something. I would like this to be applied to the category field and the loan field

[code=php]<?php session_start(); ?>
<?php // suppress or turn off notice errors or warnings
error_reporting (E_ALL ^ E_NOTICE); ?>
<?php require_once(“includes/functions.php”);
include_once(“includes/functions_two.php”);
if (!isset($_SESSION[‘camnumber’])) {
redirect_to (“index2.php”);
}
?>

<html>
<head>
<title>Listing page</title>

</head>
<body>
<?php include(“includes/header.php”); ?>

<table id= “structure”>
<tr>
<td id=”navigation”>
<br/><a href = “member.php”> Back to Menu Page</a><br/>
<br/><a href= “logout.php”>Log out</a>

</td>
<td id= “page”>

<h2>Adding book details</h2>
<?php

// checking for submit
$submit = $_POST[‘submit’];

// form data and stripping of html tags
$title = strip_tags($_POST[‘title’]);
$author = strip_tags($_POST[‘author’]);
$edition = strip_tags($_POST[‘edition’]);
//$publicationdate = strip_tags($_POST[‘publicationdate’]);
$publicationdate=strip_tags($_POST[‘publicationdate’]);
//$aDate_parts=preg_split(“/[s-]+/”,$publicationdate);
$isbnnumber = strip_tags($_POST [‘isbnnumber’]);
$condition = strip_tags($_POST[‘condition’]);
$price = strip_tags($_POST[‘price’]);
$category = strip_tags($_POST[‘category’]);
$loan = strip_tags($_POST[‘loan’]);

/*function is_date($publicationdate){
$stamp=strtotime($publicationdate);
if(!is_numeric($stamp))
{return FALSE;
}
$month=date(‘m’,$stamp);
$day=date(‘d’,$stamp);
$year=date(‘Y’,$stamp);
$response=”Thanks”;
if(checkdate($month,$day,$year))
{
return $response;
}

return FALSE;
}*/
$camnumber = $_SESSION[‘camnumber’];
if($submit)
{
// user registration by connecting to database
$connect = mysql_connect(“localhost”, “”, “”);
mysql_select_db (“book_trade2”);

$isbncheck = mysql_query (“SELECT isbnnumber FROM books WHERE isbnnumber =’$isbnnumber ‘”);
$count = mysql_num_rows($isbncheck);

if ($count!=0)
{
echo (“<span style=’color:red’ name=’PHPSpan’><b>ISBN Number has to be unique</b></span>”);
}
else{
// check for that all fields have been entered
if ($title&&$author&&$isbnnumber&&$condition&&$price&&$category&&$camnumber&&$loan)
{
//checks that a digit is entered and rounds it off if more than two values are entered after the decimal place.
if (!is_numeric($price))
{
echo “<span style=’color:red’ name=’PHPSpan’><b>Price must be a an interger</b></span>”;
}
else
{
// check for character length of isbn number
if (strlen($isbnnumber)>13||strlen($isbnnumber)<13)
{
echo “<span style=’color:red’ name=’PHPSpan’><b>Length of ISBN Number should not be more or less than 13 characters</b></span>”;
}
else
{
// check title name and author length
if (strlen($title)>50||strlen($author)>60)
{
echo “<span style=’color:red’ name=’PHPSpan’><b>Length of Title and Author is too long</b></span>”;
}
else
{
// check the details submitted if this field is not empty
if (!empty($_POST[‘edition’])&& strlen($edition)<2||strlen($edition)>6)
{
echo “<span style=’color:red’ name=’PHPSpan’><b>Edition must be between 6 and 2 characters</b></span>”;
}
else{
//if the date field is not empty check the details submitted in it
if (!empty($_POST[‘publicationdate’]))
{
$date_val = date_validate($publicationdate);
}
//search for the date within the string to validate it
$check_date = strpos($date_val,”Invalid”);

if($check_date === false) {

// code to connect to database was here
$queryreg = mysql_query(”

INSERT INTO books VALUES(”,’$title’,’$author’,’$edition’, ‘$date_val’,’$isbnnumber’,’$condition’,’$price’,’$category’,’$loan’,’$camnumber’)

“);
die(“You book has been added <a href =’index2.php’> Return to login page</a>”);

//die (“You book has been added <a href =’index2.php’> Return to login page</a>”);
}
else{

echo “$date_val”;

}

}
}
}

}

}

else{
echo “<span style=’color:red’ name=’PHPSpan’><b>Please fill in all fields</b></span><br />”;
}
}

}

?>

<form action=’listing.php’ method= ‘POST’>
<table>
<tr>
<td>
Title
</td>
<td>
<input type=’text’ size=’33’ name=’title’ value= “<?php echo $title; ?>”>
</td>
</tr>
<tr>
<td>
Author
</td>
<td>
<input type=’text’ size=’33’ name=’author’ value= “<?php echo $author; ?>”>
</td>
</tr>
<tr>
<td>
Edition
</td>
<td>
<input type=’text’ size=’33’ name=’edition’value= “<?php echo $edition;?>”>
</td>
</tr>
<tr>
<td>
Publication Date
</td>
<td>
<input type=’txtdate’ size=’33’ name=’publicationdate’ value= “<?php echo $publicationdate; ?>”>
<span class=”date”>(yyyy/mm/dd)
</td>
</tr>
<tr>
<td>
ISBN Number
</td>
<td>
<input type=’text’ size=’33’ name=’isbnnumber’ value= “<?php echo $isbnnumber; ?>”>
</td>
</tr>
<tr>
<td>
Condition
</td>
<td>
<input type=’text’ size=’33’ name=’condition’value= “<?php echo $condition; ?>”>
</td>
</tr>
<tr>
<td>
Price £

</td>
<td>
<input type=’text’ size=’33’ name=’price’value= “<?php echo $price; ?>”>
</td>
</tr>
<tr>
<td>
Category
<td colspan=’2′>
<select name= ‘category’>
<option value= ‘Database’>Database</option>
<option value= ‘Multimedia & design’>Multimedia & design</option>
<option value= ‘Structured programming’>Structured Programming</option>
<option value= ‘Software engineering’>Software engineering</option>
<option value= ‘Projects’>Projects</option>
<option value= ‘Networks’>Networks</option>
<option value= ‘Human computer interface design’>Human Computer Interface Design</option>
<option value= ‘Systems analysis’>Systems Analysis</option>
<option value= ‘Web authoring’>Web Authoring</option>
<option value= ‘3d Graphics & animation’>3d Graphics & Animation</option>
<option value= ‘Web servers’>Web Servers</option>
<option value= ‘Object oriented programming’>Object Oriented Programming</option>
<option value= ‘Unix sytems’>Unix Sytems</option>
<option value= ‘Other’>Other</option>
<option value= ‘Choose’ selected>Choose Category</option>
</select>
</tr>
</td>
<tr>
<td>
Available for loan
<td cols=’2′>
<select name=’loan’>
<option value= ‘Yes’>Yes</option>
<option value= ‘No’>No</option>
<option value= ‘Choose’ selected>Choose Option</option>
</select>
</tr>
</td>

<tr>
<td><input type=’submit’ name=’submit’ value=’Add your book’>
</tr>
</table>
</form>
</td>
</tr>
</table>
<?php include(“includes/footer.php”); ?>[/code]

Thanks

to post a comment
PHP

7 Comments(s)

Copy linkTweet thisAlerts:
@SrWebDeveloperApr 26.2010 — Actually, did you know that setting a default option and using a null or zero value is a common strategy for both client and server side validation? Meaning, add the "selected" attribute to whatever option item you want as the default if not the first one already listed. Set the value to zero for that one or any others you don't want users to be able to select (i.e. title of sub-options that might follow).

In your Javascript, for client side validation (simplified, to show concept only):

[CODE]function dropDownMenu(form) {
var myindex=document.forms[0].menu.selectedIndex;
if (myindex==0) {
alert("nYou must make a selection from the drop-down menu.");
document.forms[0].menu.focus();
}
else {
menu_selection=document.forms[0].menu.options[myindex].value;
return true;
}
}[/CODE]


And server side, something like this (rediculously simplified):

[code=php]if ($_POST['option']=="0")) { print "You must make a selection from the drop down menu." }
[/code]


Or, if you want to get super fancy, look into one of the scripts like this one that dynamically populates the options based on how a user selects other form elements using Ajax so you can retrieve option data server side without page refreshing. You also won't "need" extra options that tell the user to "select from this list" which only complicates the validation.

-jim
Copy linkTweet thisAlerts:
@tirnaApr 26.2010 — Imo the easiest way is to set the default option in the html <select>

[CODE]
<select name= 'category'>
<option value= 'Choose'>Choose A Category</option>
[COLOR=red]<option value= 'Database' selected="selected">Database</option>[/COLOR]
<option value= 'Multimedia & design'>Multimedia & design</option>
<option value= 'Structured programming'>Structured Programming</option>
<option value= 'Software engineering'>Software engineering</option>
<option value= 'Projects'>Projects</option>
<option value= 'Networks'>Networks</option>
<option value= 'Human computer interface design'>Human Computer Interface Design</option>
<option value= 'Systems analysis'>Systems Analysis</option>
<option value= 'Web authoring'>Web Authoring</option>
<option value= '3d Graphics & animation'>3d Graphics & Animation</option>
<option value= 'Web servers'>Web Servers</option>
<option value= 'Object oriented programming'>Object Oriented Programming</option>
<option value= 'Unix sytems'>Unix Sytems</option>
<option value= 'Other'>Other</option>
</select>
[/CODE]


but instead of setting a default, which might not be the appropriate choice for every user, I would check server side that $_POST['category'] and $_POST['loan'] have valid values other than "Choose an Option" before processing the form data on the server.
Copy linkTweet thisAlerts:
@boxenauthorApr 27.2010 — I have checking whether a value has been selected for loan with this

[code=php]if ($_POST['loan'] == "0"){
echo "span style='color:red' name='PHPSpan'><b>A selection from the Available for loan drop down menu has not been made</b></span>";
}[/code]


but it does not do the job as it still submits "choose option" in the db if the user did not select anything. any ideas
Copy linkTweet thisAlerts:
@tirnaApr 27.2010 — I have checking whether a value has been selected for loan with this

[code=php]if ($_POST['loan'] == "0"){
echo "span style='color:red' name='PHPSpan'><b>A selection from the Available for loan drop down menu has not been made</b></span>";
}[/code]


but it does not do the job as it still submits "choose option" in the db if the user did not select anything. any ideas[/quote]


I can't see the above code in the code you posted in your first post. If you don't post all the relevent code, then there isn't anymore I can do.
Copy linkTweet thisAlerts:
@boxenauthorApr 27.2010 — [code=php]<?php session_start(); ?>
<?php // suppress or turn off notice errors or warnings
error_reporting (E_ALL ^ E_NOTICE); ?>
<?php require_once("includes/functions.php");
include_once("includes/functions_two.php");
if (!isset($_SESSION['camnumber'])) {
redirect_to ("index2.php");
}
?>

<html>
<head>
<title>Listing page</title>

</head>
<body>
<?php include("includes/header.php"); ?>

<table id= "structure">
<tr>
<td id="navigation">
<br/><a href = "member.php"> Back to Menu Page</a><br/>
<br/><a href= "logout.php">Log out</a>

</td>
<td id= "page">


<h2>Adding book details</h2>
<?php



// checking for submit
$submit = $_POST['submit'];

// form data and stripping of html tags
$title = strip_tags($_POST['title']);
$author = strip_tags($_POST['author']);
$edition = strip_tags($_POST['edition']);
$publicationdate=strip_tags($_POST['publicationdate']);
$isbnnumber = strip_tags($_POST ['isbnnumber']);
$condition = strip_tags($_POST['condition']);
$price = strip_tags($_POST['price']);
$category = strip_tags($_POST['category']);
$loan = strip_tags($_POST['loan']);

$camnumber = $_SESSION['camnumber'];
if($submit)
{
// user registration by connecting to database
$connect = mysql_connect("localhost", "", "");
mysql_select_db ("book_trade2");

$isbncheck = mysql_query ("SELECT isbnnumber FROM books WHERE isbnnumber ='$isbnnumber '");
$count = mysql_num_rows($isbncheck);

if ($count!=0)
{
echo ("<span style='color:red' name='PHPSpan'><b>ISBN Number has to be unique</b></span>");
}
else{
// check for that all fields have been entered
if ($title&&$author&&$isbnnumber&&$condition&&$price&&$category&&$camnumber&&$loan)
{
//checks that a digit is entered and rounds it off if more than two values are entered after the decimal place.
if (!is_numeric($price))
{
echo "<span style='color:red' name='PHPSpan'><b>Price must be a an interger</b></span>";
}
else
{
// check for character length of isbn number
if (strlen($isbnnumber)>13||strlen($isbnnumber)<13)
{
echo "<span style='color:red' name='PHPSpan'><b>Length of ISBN Number should not be more or less than 13 characters</b></span>";
}
else
{
if ($_POST['loan'] == "0"){
echo "span style='color:red' name='PHPSpan'><b>A selection from the Available for loan drop down menu has not been made</b></span>";
}
else{
// check title name and author length
if (strlen($author)<3||strlen($author)>60)
{
echo "<span style='color:red' name='PHPSpan'><b>Please check the name of Author</b></span>";
}
else
{
// check the details submitted if this field is not empty
if (!empty($_POST['edition'])&& strlen($edition)<2||strlen($edition)>6)
{
echo "<span style='color:red' name='PHPSpan'><b>Edition must be between 6 and 2 characters</b></span>";
}
else{
//if the date field is not empty check the details submitted in it
if (!empty($_POST['publicationdate']))
{
$date_val = date_validate($publicationdate);
}
//search for the date within the string to validate it
$check_date = strpos($date_val,"Invalid");

if($check_date === false) {

// code to connect to database was here
$queryreg = mysql_query("

INSERT INTO books VALUES('','$title','$author','$edition', '$date_val','$isbnnumber','$condition','$price','$category','$loan','$camnumber')

");
die("You book has been added <a href ='index2.php'> Return to login page</a>");


//die ("You book has been added <a href ='index2.php'> Return to login page</a>");
}
else{

echo "$date_val";

}




}
}
}

}
}
}

else{
echo "<span style='color:red' name='PHPSpan'><b>Please fill in all fields</b></span><br />";
}
}

}

?>

<form action='listing.php' method= 'POST'>
<table>
<tr>
<td>
Title
</td>
<td>
<input type='text' size='33' name='title' value= "<?php echo $title; ?>">
</td>
</tr>
<tr>
<td>
Author

</td>
<td>
<input type='text' size='33' name='author' value= "<?php echo $author; ?>">
</td>
</tr>
<tr>
<td>
Edition

</td>
<td>
<input type='text' size='33' name='edition'value= "<?php echo $edition;?>">
</td>
</tr>
<tr>
<td>
Publication Date

</td>
<td>
<input type='txtdate' size='33' name='publicationdate' value= "<?php echo $publicationdate; ?>">
<span class="date">(yyyy/mm/dd)
</td>
</tr>
<tr>
<td>
ISBN Number

</td>
<td>
<input type='text' size='33' name='isbnnumber' value= "<?php echo $isbnnumber; ?>">
</td>
</tr>
<tr>
<td>
Condition

</td>
<td>
<input type='text' size='33' name='condition'value= "<?php echo $condition; ?>">
</td>
</tr>
<tr>
<td>
Price £

</td>
<td>
<input type='text' size='33' name='price'value= "<?php echo $price; ?>">
</td>
</tr>
<tr>
<td>
Category
<td colspan='2'>
<select name= 'category'>
<option value= 'Database'>Database</option>
<option value= 'Multimedia & design'>Multimedia & design</option>
<option value= 'Structured programming'>Structured Programming</option>
<option value= 'Software engineering'>Software engineering</option>
<option value= 'Projects'>Projects</option>
<option value= 'Networks'>Networks</option>
<option value= 'Human computer interface design'>Human Computer Interface Design</option>
<option value= 'Systems analysis'>Systems Analysis</option>
<option value= 'Web authoring'>Web Authoring</option>
<option value= '3d Graphics & animation'>3d Graphics & Animation</option>
<option value= 'Web servers'>Web Servers</option>
<option value= 'Object oriented programming'>Object Oriented Programming</option>
<option value= 'Unix sytems'>Unix Sytems</option>
<option value= 'Other'selected="yes">Other</option>
<option value= 'Choose' selected>Choose Category</option>
</select>
</tr>
</td>
<tr>
<td>
Available for loan
<td cols='2'>
<select name='loan'>
<option value= 'Yes'>Yes</option>
<option value= 'No'>No</option>
<option value= 'Choose' selected>Choose Option</option>
</select>
</tr>
</td>

<tr>
<td><input type='submit' name='submit' value='Add your book'>
</tr>
</table>
</form>
</td>
</tr>
</table>
<?php include("includes/footer.php"); ?>[/code]


Here it is in my code above. it is the solution of srWebdeveloper which i was trying but it didn't work.

Also I tried to set "other" as the default select option in html under the catergory field it didn't. I think this is because I still have "choose category" shown as select default. I wanted this (choose category) to be the default selection shown on the form but not the default value submitted if the user does not select a value. can this be done?
Copy linkTweet thisAlerts:
@SrWebDeveloperApr 27.2010 — It can't be done via HTML - only [B]one[/B] option can be "selected" in a select block.

But, it sure as heck can be done server side. Just add in a simple line like this in your PHP script that processes the form after submission:

if ($_POST['category']=="Choose") {$_POST['category']="your_default";}

Replace "your_default" with the option value you want to use as the default.

You could also specify this value on the front end, especially if the form is created dynamically, just add a hidden field with its value set to that default option value, i.e.:

[code=html]<input type="hidden" name="default_category" value="Other">[/code]

On the back end (simplified):

[code=php]if ($_POST['category']=="Choose") {$_POST['category']=$_POST['default_category'];}[/code]

?

-jim
Copy linkTweet thisAlerts:
@boxenauthorApr 27.2010 — I tried you solution srWebDeveloper and it worked thanks for you help.

have another problem with something else but i guess i will post it in a new thread.
×

Success!

Help @boxen spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.19,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...