Jump to content

PHP Issues

- - - - -

  • Please log in to reply
2 replies to this topic

#1
gjcluttrell

gjcluttrell

    Newbie

  • Members
  • Pip
  • 1 posts
The attached code pulls data from a mysql database & displays it in a form so I can edit it. I need to be able, when a user clicks the submit button in this code, the data is updated in the mysql database, and sends that data to an email address based off of the location. At this time I just have first name, lastname, and location in there. So if a user chose office1 the data would go to office1@mail.com.





<?php # edit_dqa.php


$page_title = 'Edit a Record';


$con = mysql_connect("localhost","uname","password");

if (!$con)

  {

  die('Could not connect: ' . mysql_error());

  }


mysql_select_db("psrflow", $con);


if ( (isset($_GET['id'])) && (is_numeric($_GET['id'])) ) { // Accessed through view_users.php

	$id = $_GET['id'];

} elseif ( (isset($_POST['id'])) && (is_numeric($_POST['id'])) ) { // Form has been submitted.

	$id = $_POST['id'];

} else { // No valid ID, kill the script.

	echo '<h1 id="mainhead">Page Error</h1>

	<p class="error">This page has been accessed in error.</p><p><br /><br /></p>';

	include ('./includes/footer.html'); 

	exit();

}

		

		$query = "UPDATE psrinfo SET fname='$fn', lname='$ln', location='$loc' WHERE fid=$id";

		$result = @mysql_query ($query); // Run the query.

				

		// Send mail according to location

switch($_REQUEST['location']) { 

case "office1":

$email = "myemail@yahoo.com";

break;

default:

echo "Error, no location selected!!!";

break; 

}

$Message = "<p>An employee has edited the information below.</p><br> Location : {$_REQUEST['location']}<br>First Name : {$_REQUEST['fname']}<br>Last Name : {$_REQUEST['lname']}";	

$Headers  = "MIME-Version: 1.0\n"; 

$Headers .= "Content-type: text/html; charset=iso-8859-1\n"; 

$Subject  = "Please Review";

$Headers .= "To: DQA <$EmailAddress>\n"; 

$Headers .= "From: The Database <sender email>\n"; 

$SendMail = mail($EmailAddress, $Subject, $Message, $Headers);

		

if (mail($EmailAddress, $Subject, $Message, $Headers)) {

	

echo "<center>An email has been sent for editing"; 

} else { 

 

echo "This system is not working properly. Please contact a tech."; 

 

}

						

$url = 'http://' . $_SERVER['HTTP_HOST'] . dirname($_SERVER['PHP_SELF']);

if ((substr($url, -1) == '/') OR (substr($url, -1) == '//')) {

$url = substr ($url, 0, -1);

}

$url ='/flow/index.html'; 


header("Location: $url");


exit();

		

	} else { // Report the errors.

	

		echo '<h1 id="mainhead">Error!</h1>

		<p class="error">The following error(s) occurred:<br />';

		foreach ($errors as $msg) { // Print each error.

			echo " - $msg<br />\n";

		}

		echo '</p><p>Please try again.</p><p><br /></p>';

		

	} // End of if (empty($errors)) IF.


} // End of submit conditional.


// Retrieve the user's information.

$query = "SELECT pacts, fname, lname, location WHERE fid = " . $_REQUEST['id'];

$result = @mysql_query ($query); // Run the query.


if (mysql_num_rows($result) == 1) { // Valid user ID, show the form.


	$row = mysql_fetch_array ($result, MYSQL_NUM);

		

?>	


<?php



echo '<form action="edit_dqa.php" method="post">

<fieldset><legend><h1> You are editing a record!</h1></legend>


<b>First Name:</b> <br><input type="text" name="fname" size="15" maxlength="30" value="'.$row[1].'" /><br />

<b>Last Name:</b> <br><input type="text" name="lname" size="15" maxlength="30" value="'.$row[2].'" /><br />

<b>Location: </b><br><input type="text" name="location" size="15" maxlength="30" value="'.$row[5].'" /><br>

br>

<br>

</fieldset>


<div align="left"><input type="submit" name="submit" value="Submit" /></div>


<input type="hidden" name="submitted" value="TRUE" />

<input type="hidden" name="id" value="' . $id . '" />

</form>';


} else { // Not a valid user ID.

	echo '<h1 id="mainhead">Page Error</h1>

	<p class="error">This page has been accessed in error.</p><p><br /><br /></p>';

}


mysql_close(); // Close the database connection.


?>




#2
RHochstenbach

RHochstenbach

    Learning Programmer

  • Members
  • PipPipPip
  • 56 posts
Do you mean that the user needs to select the location in the form? If so you can combine it with a drop-down menu:

<form method="post">

	<select name="location">

		<option name="1">office 1</option>

		<option name="2">office 1</option>

		<option name="3">office 1</option>

		<option name="4">office 1</option>

	</select>

	

	<input type="submit" name="submit" value="send" />

</form>

<?php 

if (isset($_POST['submit'])) { // if the submit button is pressed

		

		if($_POST['location'] == '1') {

		$address = "office1@mail.com";

		}

		

		if($_POST['location'] == '2') {

		$address = "office2@mail.com";

		}

		

		if($_POST['location'] == '3') {

		$address = "office3@mail.com";

		}

		

		if($_POST['location'] == '4') {

		$address = "office4@mail.com";

		}

		


mail($address);

}

?>


What you basically do is assign a unique ID to each location, and then have PHP set the email address based on the ID that has been chosen.

#3
BlaineSch

BlaineSch

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,448 posts

RHochstenbach said:

Do you mean that the user needs to select the location in the form? If so you can combine it with a drop-down menu:
<form method="post">
    <select name="location">
        <option name="1">office 1</option>
        <option name="2">office 1</option>
        <option name="3">office 1</option>
        <option name="4">office 1</option>
    </select>
    
    <input type="submit" name="submit" value="send" />
</form>
<?php 
if (isset($_POST['submit'])) { // if the submit button is pressed
        
        if($_POST['location'] == '1') {
        $address = "office1@mail.com";
        }
        
        if($_POST['location'] == '2') {
        $address = "office2@mail.com";
        }
        
        if($_POST['location'] == '3') {
        $address = "office3@mail.com";
        }
        
        if($_POST['location'] == '4') {
        $address = "office4@mail.com";
        }
        

mail($address);
}
?>
What you basically do is assign a unique ID to each location, and then have PHP set the email address based on the ID that has been chosen.
A bunch of IF's probably won't do well, you should make them if, else if, else so you can also have a default. If the HTML messes up or somebody is trying to play with your form on their local machine its possible they could not have any value 1-4.
// If / else if / else
if($_POST['location'] == '1') {
    $address = "office1@mail.com";
} else if ($_POST['location'] == '2') {
    $address = "office2@mail.com";
} else if ($_POST['location'] == '3') {
    $address = "office3@mail.com";
} else if ($_POST['location'] == '4') {
    $address = "office4@mail.com";
} else {
    $address = "office1@mail.com";
}

// switch / case
switch ($_POST['location']) {
    case '2':
        $email = "office2@mail.com";
        break;
    case '3':
        $email = "office3@mail.com";
        break;
    case '4':
        $email = "office4@mail.com";
        break;
    case '1':
    default:
        $email = "office1@mail.com";
}





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users