Solved

PHP Code Help Requested

Posted on 2013-11-01
5
511 Views
Last Modified: 2013-11-07
The following code allows th euser to enter data & it is dumped into my MySL database. All of that works fine. The probloem I'm having is, how can I make the data also be sent to the employee that is selected & the clerk that is selected via email? The code I have does not work....can someone help me out please? Thanks

<html
<head>
<title>New Assignment</title>
</head>
</html>

<b> <i> These are assigned on this page with automatic assignment </b> </i>

<?php
ini_set('display_errors',1);
error_reporting(E_ALL);
date_default_timezone_set('America/New_York');

if( isset($_POST) && !empty($_POST) )
{
     $host	= "localhost";
     $user	= "uname";
     $pw	= "pword";
     $db	= "mydb";
	         
     $conn = mysql_connect( $host, $user, $pw )
     or die( "Error! Unable to connect to database server: <br/>" . mysql_error() );

     $rs = mysql_select_db( $db, $conn )
     or die( "Error! Unable to connect to database:  <br/>" . mysql_error() );
          
	foreach($_POST as $key=>$value)
	{
		${$key}=mysql_real_escape_string($value);
	}
	
     $strSQL = "INSERT INTO assignments
     	(no, name, clerk, employee, task, date_submitted, time_submitted, additional_notes, your_name)
     	VALUES
     	( '" . $no . "', '" . $name . "', '".$clerk."', '".$employee."', '".$task."', '".$date_submitted."', '".$time_submitted."' , '".$additional_notes."', '".$your_name."')";
	 	 	 
     if (!mysql_query( $strSQL, $conn )){
     	echo( "Unable to save data to database: <br/>" . mysql_error() . "<br/>" . $strSQL . "</span><br/>" );
     }
     else{
     	header( "Location: http://anIP/district/" );
		exit;
     }
			 
switch($_REQUEST['employee']) { 
case "employee1":
$Email = 'employee1@domain.com' . ',';
break;
default:
echo "Error, no employee selected!!!";
break; 
}
switch($_REQUEST['clerk']) { 
case "clerk1":
$Email .= 'clerk1@domain.com';
break;
default:
echo "Error, no clerk selected!!!";
break;
}

$Message = "Something Has Been Assigned to You Below.\n\nemployee : ${_REQUEST['employee']} \nClerk : ${_REQUEST['clerk']}";
$Subject  = "Assignment";
$Headers = "From: system@domain.com \n"; 

 	if (mail($Email, $Subject, $Message, $Headers)) 
						
exit();
	 
	 	 
}

?>

<html>

<head>
<title> SYSTEM </title>

<script type="text/javascript">
function doValidation(){

    if(document.forms[0].your_name.value==""){
    alert("PLEASE CHOOSE YOUR NAME!");
    document.forms[0].your_name.focus();
    return false;
  }

    if(document.forms[0].employee.value==""){
    alert("PLEASE CHOOSE AN employee TO ASSIGN THIS TO!");
    document.forms[0].employee.focus();
    return false;
  }
	
	if(document.forms[0].no.value==""){
    alert("PLEASE ENTER A no NUMBER!");
    document.forms[0].no.focus();
    return false;
  }
  
    if(document.forms[0].name.value==""){
    alert("PLEASE ENTER A NAME!");
    document.forms[0].name.focus();
    return false;
  }
 
 return true;
}
</script>


</head>
</html>
<body>
<body style="background-image:url(FadedBG.png); background-repeat:no-repeat; background-attachment:fixed; background-position:center;">
<form method="post" action="new_psr1.php" onsubmit="return confirmation(this)">

<span style="float:left;">
    <b>Date Submitted:</b>
    </b> <br /><input type="text" name="date_submitted" size="15" value="<?php echo date('Y-m-d',time()); ?>"readonly="readonly"/>  &nbsp&nbsp&nbsp&nbsp
</span>

<span style="float:left;">
    <b>Time Submitted:</b> <br />
    <input type="text" name="time_submitted" size="5" value="<?php echo date('h:i',time()); ?>"readonly="readonly"/> &nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp
</span>

<b>Your Name:</b> <br />
<select name="your_name">
<?php
$link = mysqli_connect("localhost","uname","pword","mydb") or die("Error " . mysqli_error($link));
$query = "SELECT name FROM employees ORDER BY name ";
$result = mysqli_query($link, $query);
while($row = mysqli_fetch_array($result)) :
?>
<option value="<?php echo $row[0];?>"><?php echo $row[0];?></option>
<?php
endwhile;
?>
</select>

<br>
<br>

<span style="float:left;">
    <b>no#:</b> <br />
	<input type="text" name="no" size="10" value="N/A"/> &nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp
</span> 

<span style="float:left;">
    <b>Assigned to employee:</b> <br />
 <select name="employee">
<?php
$link = mysqli_connect("localhost","uname","pword","mydb") or die("Error " . mysqli_error($link));
$query = "SELECT name FROM employees ORDER BY name ";
$result = mysqli_query($link, $query);
while($row = mysqli_fetch_array($result)) :
?>
<option value="<?php echo $row[0];?>"><?php echo $row[0];?></option>
<?php
endwhile;
?>
</select>
</span>

<br>
<br>
<br>

<b>Offender's Name:</b> <br />
<input type="text" value="" name="name" size="35" />

<select name="task">
<option value="PS">PS</option>
</select>
<br>

<b>Instructions/Notes:</b> <br />
<input type="text" value="" name="additional_notes" size="76" />

<?php
$mysqli = new mysqli('localhost', 'uname', 'pword', 'mydb'); 
$sql = "SELECT name, active, cid FROM clerks WHERE active = '1' ORDER BY RAND() LIMIT 1";
$res = $mysqli->query($sql); 
$row = $res->fetch_row(); 
$randomName = $row[0];  
$res->free(); 
$sql = "UPDATE clerks SET active = 0 WHERE cid = " . $row[2] . " " ;
$res = $mysqli->query($sql); 

{

 $sql = "SELECT count(cid) FROM clerks WHERE active = 0";
 $res = $mysqli->query($sql); 
 $row = $res->fetch_row(); 
 $numzeros = $row[0];  
 $res->free(); 

 if($numzeros == 10){
   $sql = "UPDATE clerks SET active = 1" ;             
   $res = $mysqli->query($sql);

 }
}

//mysql_close(); // Close the database connection.
?> 

<br>
<br>
<input type="submit" value="SUBMIT" onClick="return doValidation()"/>
<td></b> <br><input type="text" name="clerk" size="21" maxlength="40" value="<?php echo $randomName;?>" /><br /> </td>
<?php echo '</form>';?>
 
</form>
</body>
</html>

Open in new window

0
Comment
Question by:wantabe2
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
5 Comments
 
LVL 57

Expert Comment

by:Julian Hansen
ID: 39617863
What errors are you getting or is the mail just not coming through?

Have a look at phpMailer to do the mailing for you.
0
 
LVL 15

Author Comment

by:wantabe2
ID: 39617900
no errors & the email is just not going though. I've used the code above to email the info in the past but think I have it in the incorrect place on the page of code......i'm not sure at this point.
0
 
LVL 34

Accepted Solution

by:
Slick812 earned 500 total points
ID: 39618841
greetings  wantabe2,, I looked at your PHP code work above, and You have this
if (!mysql_query( $strSQL, $conn )){
  echo( "Unable to save data to database: <br/>" . mysql_error() . "<br/>" . $strSQL . "</span><br/>" );
     }
else{
// YOUR PROBLEM IS THE LINE BELOW
  header( "Location: http://anIP/district/" );
  exit; // MISTAKE HERE YOU EXIT, NO FURTHER PHP CODE IS DONE, NO EMAIL
     }

Open in new window

SO, You need to do the Emails BEFORE you END and EXIT the page code, However You have rendering COMPLICATIONS in your email code  you have error output as -

echo "Error, no employee selected!!!";
If you set a php "Header" with     header( "Location: http://anIP/district/" );    you will never see any ERROR echo, because the page goes to    http://anIP/district/     and NOT the current page to show any PHP echo info!

and then later you try to do the EMAIL send as -

if (mail($Email, $Subject, $Message, $Headers))
exit();// YOU TRY TO EXIT a SECOND TIME, Makes NO SENSE! !
but you do not test for a a successful $Email
AND you can not have TWO emails sent to two different addys, as employee and clerk with only ONE  line -
if (mail($Email, $Subject, $Message, $Headers))

I can not do any correction code for you, as I can not see any way to show error output with echo   AND then do a page fetch with -
header( "Location: http://anIP/district/" );
which goes to another page so you can never see any echo on the current page!

you need to change the Emails to some code maybe like -
switch($_POST['employee']) { 
case "employee1":
mail('employee1@domain.com', 'Assignment', 
"Something Has Been Assigned to You Below.\r\nemployee : ${_POST['employee']} ", "From: system@domain.com \r\n");
break;
default:
// YOU NEED TO RE-THINK YOUR ERROR OUTPUT IF YOU RE-LOCATE YOUR PAGE
echo "Error, no employee selected!!!";
break; 
}

Open in new window

But you really need to RE-DO your page code for the new EMAIL and rethink your ERROR output, if you want to use -
header( "Location: http://anIP/district/" );

ask questions if you need more info
0
 
LVL 15

Author Comment

by:wantabe2
ID: 39619810
thanks, I'll edit the code when i return to work monday. thanks for your assistance.
0
 
LVL 34

Expert Comment

by:Slick812
ID: 39620337
Because you responded here, I have looked at your code again, and you really need to learn more about proper HTML page code, you have very many HTML tag mistakes! for instance your FIRST LINE OF CODE is incorrect as -
<html

it needs the closing as  <html>
ALSO You have improper closing tags for </html>, the </html> should ONLY be there ONCE at the very end of the page code.
you have a <title> tag TWICE, which is incorrect, ONLY ONE <title> tag is correct HTML.

ALSO you have HTML page output before the <body> tag , as -
<b> <i> These are assigned on this page with automatic assignment </b> </i>
which is always incorrect.
ALSO you have two <body> tags, which is always incorrect.

you have some <span> tags with a float as -
<span style="float:left;">
which I do not think does anything to help your display output.

in your form tag as -
<form method="post" action="new_psr1.php" onsubmit="return confirmation(this)">
you have an onsubmit that calls a NON-EXISTENT javascript function of confirmation(this), bad code I would think?

one last note, you use the old database connection as -
$conn = mysql_connect( $host, $user, $pw )

and then later you use the newer improved database connection as -
$mysqli = new mysqli('localhost', 'uname', 'pword', 'mydb');

This is bad code work, you should use one or the other but not both (the newer is better), and worse than that! !  - - you connect to the same database THREE TIMES, you only need to connect to a MySQL database ONE TIME!
I hope some of this helps.

as to error checking, you need to do checks for your form data like $_POST["name"] to see if it is empty on the server side (in PHP) because the javascript, may be disabled or not working on some browsers.
First check all of the form data POST including the EMAIL entries like  $_POST['employee'] and if they are not correct do not do any DB INSERT, and display the ERROR with the form, so the user can make corrections, If all $_POST entries are OK then send the emails and do the INSERT, and you can switch to the other page with  header( "Location:
0

Featured Post

Migrating Your Company's PCs

To keep pace with competitors, businesses must keep employees productive, and that means providing them with the latest technology. This document provides the tips and tricks you need to help you migrate an outdated PC fleet to new desktops, laptops, and tablets.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
MySQL to get Limited value from each category. 6 42
MySQL 6 48
SSL unsecure page mystery 17 47
php mail() send emails but the message is lost 4 33
Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
This article shows the steps required to install WordPress on Azure. Web Apps, Mobile Apps, API Apps, or Functions, in Azure all these run in an App Service plan. WordPress is no exception and requires an App Service Plan and Database to install
The viewer will learn how to count occurrences of each item in an array.
The viewer will receive an overview of the basics of CSS showing inline styles. In the head tags set up your style tags: (CODE) Reference the nav tag and set your properties.: (CODE) Set the reference for the UL element and styles for it to ensu…

751 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question