Link to home
Start Free TrialLog in
Avatar of wantabe2
wantabe2Flag for United States of America

asked on

PHP Code Help Requested

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

Avatar of Julian Hansen
Julian Hansen
Flag of South Africa image

What errors are you getting or is the mail just not coming through?

Have a look at phpMailer to do the mailing for you.
Avatar of wantabe2

ASKER

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.
ASKER CERTIFIED SOLUTION
Avatar of Member_2_248744
Member_2_248744
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
thanks, I'll edit the code when i return to work monday. thanks for your assistance.
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: