Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

STROUPPER not working

Status
Not open for further replies.

tekpr00

IS-IT--Management
Jan 22, 2008
186
CA
Hello All,

Can anyone tell me why line 40 (//this is line 40) will not return upper case when passed to a variable and printed in line 50? thanks.

Code:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
"[URL unfurl="true"]http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">[/URL]
<html xmlns="[URL unfurl="true"]http://www.w3.org/1999/xhtml"[/URL] xml:lang="en" lang="en">
 <head>
  <meta http-equiv="Content-type" content="text/html; charset=utf-8" />
  
  <title> Test form </title>
  <style type="text/css" media="screen">.number {font-weight: bold;} </style>
 </head>
 <body>

 <form action="textform.php" method="post">

 <p> First Name <input type="text"
 name="firstname" size="20" /></p>

<p> Last Name <input type="text"
 name="lastname" size="20" /></p>

<p> Job Title Code (GM, MGR, PGM, or PM) <input type="text"
 name="jobtitle" size="4" /></p>

 <p> Department <input type="text"
 name="department" size="6" /></p>

<input type="submit" name="submit"
value="Submit" />
<h1> Test Form </h1>

<?php 
/* This script takes values from above and use them in the function below. */
 //
$firstname=$_POST['firstname'];
$lastname=$_POST['lastname'];
$department=$_POST['department'];
//$department=strtoupper($_POST['department']);
//format_dept
//function format_dept(strtoupper$department) {
function format_dept($department) {
return strtoupper($department)	; //this is line 40
}

//print result

print "<div><p>
First Name :<span class=\"number\">$firstname</span><br />

Last Name :<span class=\"number\">$lastname</span><br />

Department :  <span class=\"number\">$department</span></p></div>" //this is line 50
?>
</body>
</html>
 
I have re-written this code to as follows with the same result. The $deptn is not showing in CAPS. Not showing at all.

Code:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
"[URL unfurl="true"]http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">[/URL]
<html xmlns="[URL unfurl="true"]http://www.w3.org/1999/xhtml"[/URL] xml:lang="en" lang="en">
 <head>
  <meta http-equiv="Content-type" content="text/html; charset=utf-8" />
  
  <title> Test form </title>
  <style type="text/css" media="screen">.number {font-weight: bold;} </style>
 </head>
 <body>

 <form action="textform.php" method="post">

 <p> First Name <input type="text"
 name="firstname" size="20" /></p>

<p> Last Name <input type="text"
 name="lastname" size="20" /></p>

<p> Job Title Code (GM, MGR, PGM, or PM) <input type="text"
 name="jobtitle" size="4" /></p>

 <p> Department <input type="text"
 name="department" size="6" /></p>

<input type="submit" name="submit"
value="Submit" />
<h1> Test Form </h1>

<?php 
/* This script takes values from above and use them in the function below. */
 //
$firstname=$_POST['firstname'];
$lastname=$_POST['lastname'];
$department=$_POST['department'];

function format_dept($department){
$deptn = strtoupper($department);
return $deptn;
}

//print result

print "<div><p>
First Name :<span class=\"text\">$firstname</span><br />

Last Name :<span class=\"text\">$lastname</span><br />

Department :  <span class=\"text\">$deptn</span></p></div>"
?>
</form>
</body>
</html>
 
How would it? You never call the function. Functions don't magically run, and alter variables. They need to be called and their returned values need to be "retrieved" and then used.

Also variables have something called scope. That is they exist in certain areas of your code but not in others. Variabes inside functions exist inside functions only, variables outside functions exist outside the functions. So a variable defined outside a function cannot be directly used inside a function. and viceversa.

Code:
[blue]$department[/blue]t=$_POST['department'];

function format_dept($department){
$deptn = strtoupper([green]$department[/green]);
return [red]$deptn[/red];
}
...
Department :  <span class=\"text\">[COLOR=#464646 #EDED00]$deptn[/color]</span></p></div>"

So your [blue]blue[/blue] and [green]green[/green] $department variables are completely different. As well as your [red]red[/red] and [COLOR=#464646 #EDED00]yellow [/color] $deptn variables.

Also since you never call your function, nothing is ever done to your variable.

What you want to do is call your function, passing the value you want to it, and then retrieving the result from it.

Code:
<?php 
/* This script takes values from above and use them in the function below. */
 //
$firstname=$_POST['firstname'];
$lastname=$_POST['lastname'];
$department=$_POST['department'];

function format_dept($department){
$deptn = strtoupper($department);
return $deptn;
}


[blue]$deptn=format_dept($department);[/blue]
//print result

print "<div><p>
First Name :<span class=\"text\">$firstname</span><br />

Last Name :<span class=\"text\">$lastname</span><br />

Department :  <span class=\"text\">$deptn</span></p></div>"
?>


----------------------------------
Phil AKA Vacunita
----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.

Web & Tech
 
Thanks....

Would you know why the switch is printing "GM" instead of General Manager when "GM is entered?
Test Form

Full Name :T Mail
Title :GM
Department : HELLO



Code:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
"[URL unfurl="true"]http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">[/URL]
<html xmlns="[URL unfurl="true"]http://www.w3.org/1999/xhtml"[/URL] xml:lang="en" lang="en">
 <head>
  <meta http-equiv="Content-type" content="text/html; charset=utf-8" />
  
  <title> Test form </title>
  <style type="text/css" media="screen">.number {font-weight: bold;} </style>
 </head>
 <body>

 <form action="textform7.1.php" method="post">

 <p> First Name <input type="text"
 name="firstname" size="20" /></p>

<p> Last Name <input type="text"
 name="lastname" size="20" /></p>

<p> Job Title Code (GM, MGR, PGM, or PM) <input type="text"
 name="jobtitle" size="4" /></p>

 <p> Department <input type="text"
 name="department" size="6" /></p>

<input type="submit" name="submit"
value="Submit" />
<h1> Test Form </h1>
</form>
<?php 
/* This script takes values from above and use them in the function below. */
 //

//Here
$firstname=$_POST['firstname'];
$lastname=$_POST['lastname'];
$department=$_POST['department'];
$jobtitle=$_POST['jobtitle'];

//Dept function
function format_dept($department){
$deptn = strtoupper($department);
return $deptn;
}
//Full name function
function abbrev_name($firstname,$lastname){
$upperfirst =strtoupper($firstname); //upprcase first tname
$firstinit= $upperfirst[0];  //get first innitial
return $fullname=$firstinit . " " . $lastname;
}

//job title function
function job_title($jobtitle){
return $jb_tle;
switch($jobtitle){
	case 'GM':
	$jb_tle='General Manager';
	break;
	case 'MGR':
	$job_title='Manager';
	break;
	case 'PGM':
	$job_title='Program Manager';
	break;
	case 'PM':
	$job_title='Project Manager';
	break;
	default:
	$job_title='Unknown';
	}//end of switch
}//end of title function

//Call functions
$deptn=format_dept($department);
$fullname=abbrev_name($firstname,$lastname);
$jb_tle=job_title($jobtitle);
//print result
print "<p>
Full Name :<span class=\"text\">$fullname</span><br />

Title  :<span class=\"text\">$job_tle</span><br />

Department :  <span class=\"text\">$deptn</span></p>"
?>

</body>
</html>
 
i modofoed this again and I am still getting error for the GM

Code:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
"[URL unfurl="true"]http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">[/URL]
<html xmlns="[URL unfurl="true"]http://www.w3.org/1999/xhtml"[/URL] xml:lang="en" lang="en">
 <head>
  <meta http-equiv="Content-type" content="text/html; charset=utf-8" />
  
  <title> Test form </title>
  <style type="text/css" media="screen">.number {font-weight: bold;} </style>
 </head>
 <body>

 <form action="textform7.1.php" method="post">

 <p> First Name <input type="text"
 name="firstname" size="20" /></p>

<p> Last Name <input type="text"
 name="lastname" size="20" /></p>

<p> Job Title Code (GM, MGR, PGM, or PM) <input type="text"
 name="jobtitle" size="4" /></p>

 <p> Department <input type="text"
 name="department" size="6" /></p>

<input type="submit" name="submit"
value="Submit" />
<h1> Test Form </h1>
</form>
<?php 
/* This script takes values from above and use them in the function below. */
 //

//Here
$firstname=$_POST['firstname'];
$lastname=$_POST['lastname'];
$department=$_POST['department'];
$jobtitle=$_POST['jobtitle'];

//Dept function
function format_dept($department){
$deptn = strtoupper($department);
return $deptn;
}
//Full name function
function abbrev_name($firstname,$lastname){
$upperfirst =strtoupper($firstname); //upprcase first tname
$firstinit= $upperfirst[0];  //get first innitial
return $fullname=$firstinit . " " . $lastname;
}

switch($jobtitle){
	case 'GM':
	$job_title='General Manager';
	break;
	case 'MGR':
	$job_title='Manager';
	break;
	case 'PGM':
	$job_title='Program Manager';
	break;
	case 'PM':
	$job_title='Project Manager';
	break;
	default:
	$job_title='Unknown';
	}//end of switch

//job title function
function job_title_func($job_title){
return $jbtitle;
}//end of title function

//Call functions
$deptn=format_dept($department);
$fullname=abbrev_name($firstname,$lastname);
$jbtitle=job_title_func($job_title);
//print result
print "<p>
Full Name :<span class=\"text\">$fullname</span><br />

Title  :<span class=\"text\">$jbtitle</span><br />

Department :  <span class=\"text\">$deptn</span></p>"
?>

</body>
</html>
 
Again, variables have scope. Re-read what I posted above.

Your job_title_func() function is attempting to return a value it hasn't set.

Code:
function job_title_func($job_title){
return[red] $jbtitle;[/red]
}


You haven't set that variable anywhere.

Try this instead:

Code:
//job title function
function job_title_func([red]$jtitle[/red]){

switch([red]$jtitle[/red]){
    case 'GM':
    $job_title='General Manager';
    break;
    case 'MGR':
    $job_title='Manager';
    break;
    case 'PGM':
    $job_title='Program Manager';
    break;
    case 'PM':
    $job_title='Project Manager';
    break;
    default:
    $job_title='Unknown';
    }//end of switch

return $job_title;
}//end of title function

...

$jbtitle=job_title_func($jobtitle);











----------------------------------
Phil AKA Vacunita
----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.

Web & Tech
 
I called all my functions correctly like you suggested. Still no correct answer. Here is my latest code.
Code:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
"[URL unfurl="true"]http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">[/URL]
<html xmlns="[URL unfurl="true"]http://www.w3.org/1999/xhtml"[/URL] xml:lang="en" lang="en">
 <head>
  <meta http-equiv="Content-type" content="text/html; charset=utf-8" />
  
  <title> Test form </title>
  <style type="text/css" media="screen">.number {font-weight: bold;} </style>
 </head>
 <body>

 <form action="textform7.1.php" method="post">

 <p> First Name <input type="text"
 name="firstname" size="20" /></p>

<p> Last Name <input type="text"
 name="lastname" size="20" /></p>

<p> Job Title Code (GM, MGR, PGM, or PM) <input type="text"
 name="jobtitle" size="4" /></p>

 <p> Department <input type="text"
 name="department" size="6" /></p>

<input type="submit" name="submit"
value="Submit" />
<h1> Test Form </h1>
</form>
<?php 
/* This script takes values from above and use them in the function below. */
 //

//Here
$firstname=$_POST['firstname'];
$lastname=$_POST['lastname'];
$department=$_POST['department'];
$jobtitle=$_POST['jobtitle'];

//Dept function
function format_dept($department){
$deptn = strtoupper($department);
return $deptn;
}
//Full name function
function abbrev_name($firstname,$lastname){
$lastnameuc=ucfirst($lastname);
$upperfirst =strtoupper($firstname); //upprcase first tname
$firstinit= $upperfirst[0];  //get first innitial
return $fullname=$firstinit . " " . $lastnameuc;
}
//job title function
function job_title_func($jobtitle){
if ($jobtitle= 'GM'){
$job_title='General Manager';
}
elseif ($jobtitle = 'MGR'){
$job_title='Manager';
}
elseif ($jobtitle = 'PGM'){
$job_title='Program Manager';
}
elseif ($jobtitle = 'PM'){
$job_title='Project Manager';
}
return $job_title;
}//end of title function

//Call functions
$deptn=format_dept($department);
$fullname=abbrev_name($firstname,$lastname);
$jobtitle=job_title_func($jobtitle);

//print result
print "<p><div>
Full Name :<span class=\"text\">$fullname</span><br />

Title  :<span class=\"text\">$jobtitle</span><br />

Department :  <span class=\"text\">$deptn</span></p></div>"
?>
</body>
</html>
 
I have even modified further, but my job title function is not working
Code:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
"[URL unfurl="true"]http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">[/URL]
<html xmlns="[URL unfurl="true"]http://www.w3.org/1999/xhtml"[/URL] xml:lang="en" lang="en">
 <head>
  <meta http-equiv="Content-type" content="text/html; charset=utf-8" />
  
  <title> Test form </title>
  <style type="text/css" media="screen">.number {font-weight: bold;} </style>
 </head>
 <body>

 <form action="textform7.1.php" method="post">

 <p> First Name <input type="text"
 name="firstname" size="20" /></p>

<p> Last Name <input type="text"
 name="lastname" size="20" /></p>

<p> Job Title Code (GM, MGR, PGM, or PM) <input type="text"
 name="jobtitle" size="4" /></p>

 <p> Department <input type="text"
 name="department" size="6" /></p>

<input type="submit" name="submit"
value="Submit" />
<h1> Test Form </h1>
</form>
<?php 
/* This script takes values from above and use them in the function below. */
 //

//Here
$firstname=$_POST['firstname'];
$lastname=$_POST['lastname'];
$department=$_POST['department'];
$jobtitle=$_POST['jobtitle'];

//Dept function
function format_dept($department){
$deptn = strtoupper($department);
return $deptn;
}
//Full name function
function abbrev_name($firstname,$lastname){
$lastnameuc=ucfirst($lastname);
$upperfirst =strtoupper($firstname); //upprcase first tname
$firstinit= $upperfirst[0];  //get first innitial
return $fullname=$firstinit . " " . $lastnameuc;
}
//job title function
function job_title_func($jobtitle){
if ($jobtitle='GM') {
    $job_title='General Manager';
} elseif ($jobtitle='MGR') {
    $job_title='Manager';
} elseif ($jobtitle='PM') {
    $job_title='Project Manager';
}elseif ($jobtitle='PMG') {
    $job_title='Program Manager';
}
else {
   $job_title="There is no Value";
}
return $job_title;
}//end of title function

//Call functions
$deptn=format_dept($department);
$fullname=abbrev_name($firstname,$lastname);
$job_title=job_title_func($jobtitle);

//print result
print "<p><div>
Full Name :<span class=\"text\">$fullname</span><br />

Title  :<span class=\"text\">$job_title</span><br />

Department :  <span class=\"text\">$deptn</span></p></div>"
?>
</body>
</html>
 
I finnaly got this working on my own. However the isset() function is not working. It is suppose to throw error and die if any of the input fields have invalid input like putting a "4" in the firstname field. Can someone help? thanks in advance.

Code:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
"[URL unfurl="true"]http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">[/URL]
<html xmlns="[URL unfurl="true"]http://www.w3.org/1999/xhtml"[/URL] xml:lang="en" lang="en">
 <head>
  <meta http-equiv="Content-type" content="text/html; charset=utf-8" />
  
  <title> Test form </title>
  <style type="text/css" media="screen">.number {font-weight: bold;} </style>
 </head>
 <body>

 <form action="textform7.1.php" method="post">

 <p> First Name <input type="text"
 name="firstname" size="20" /></p>

<p> Last Name <input type="text"
 name="lastname" size="20" /></p>

<p> Job Title Code (GM, MGR, PGM, or PM) <input type="text"
 name="jobtitle" size="4" /></p>

 <p> Department <input type="text"
 name="department" size="6" /></p>

<input type="submit" name="submit"
value="Submit" />
<h1> Test Form </h1>
</form>
<?php 
/* This script takes values from above and use them in the function below. */
 //

//Here
$firstname=$_POST['firstname'];
$lastname=$_POST['lastname'];
$department=$_POST['department'];
$jobtitle=$_POST['jobtitle'];

//check values and die if all are not present
if(isset ($firstname)){
}elseif (isset($lastname)){
}
elseif (isset ($department)){
}
elseif (isset ($jobtitle)){
}
else {
print '<p style="color:red;">Please enter a valid firstname, lastname, department and Jobtitle!</p>'; 
}
//Dept function
function format_dept($department){
$deptn = strtoupper($department);
return $deptn;
}
//Full name function
function abbrev_name($firstname,$lastname){
$lastname=strtolower($lastname);
$lastnameuc=ucfirst($lastname);
$upperfirst =strtoupper($firstname); //upprcase first tname
$firstinit= $upperfirst[0];  //get first innitial
return $fullname=$firstinit . " " . $lastnameuc;
}
//job title function
function job_title_func($jobtitle){
if ($jobtitle=='GM') {
    $job_title='General Manager';
} elseif ($jobtitle=='MGR') {
    $job_title='Manager';
} elseif ($jobtitle=='PM') {
    $job_title='Project Manager';
}elseif ($jobtitle=='PGM') {
    $job_title='Program Manager';
}
else {
   $job_title="This is not a VALID Value";
}
return $job_title;
}//end of title function

//Call functions
$deptn=format_dept($department);
$fullname=abbrev_name($firstname,$lastname);
$job_title=job_title_func($jobtitle);

//print result
print "<p><div>
Full Name :<span class=\"text\">$fullname</span><br />

Title  :<span class=\"text\">$job_title</span><br />

Department :  <span class=\"text\">$deptn</span></p></div>"
?>
</body>
</html>
 
I would advise a different approach, if the job title is something that cannot be changed by the user -- they have to pick one of the suggested options. Then use the select field, rather than input field:
Code:
<p> Job Title Code (GM, MGR, PGM, or PM) 
<select name="jobtitle">
  <option value="There is no value">No Title</option>
  <option value="General Manager">GM</option>
  <option value="Manager">MGR</option>
  <option value="Project Manager">PM</option>
  <option value="Program Manager">PMG</option>
</select>
</p>
And then you can simply get the right answer by doing:
Code:
<?php 
print "<p><div>
Full Name :<span class=\"text\">$fullname</span><br />

Title  :<span class=\"text\">$_POST['jobtitle']</span><br />

Department :  <span class=\"text\">$deptn</span></p></div>"
?>

[small]Do something about world cancer today: Comprehensive cancer control information at PACT[/small]
 
Tell it to die.
Code:
if(isset($firstname)){
}elseif (isset($lastname)){
}
elseif (isset ($department)){
}
elseif (isset ($jobtitle)){
}
else {
[red]die([/red][gray]'<p style="color:red;">Please enter a valid firstname, lastname, department and Jobtitle!</p>'[/gray][red])[/red]; 

}

Also the function is_nan() checks to see whether the value provided is or is not a number. It returns true if its not a number.

Code:
if(isset($firstname) [red]&& is_nan($firstname)[/red]){

----------------------------------
Phil AKA Vacunita
----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.

Web & Tech
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top