Linked Questions

Popular Questions

Updating database values using button on form to mail

Asked by At

I created a request form where the admin can approve or decline the request, everything works fine when you click the approve or decline button, here's an example picture:


The requests will be sent through the email as well which looks like this:

Requests (email)

The thing is that I can't make the buttons approve and decline work in the email. When I click approve or decline, it redirects me here:


Here's my code for the normal viewing of requests which works when you click the approve or decline button:

$connect = mysqli_connect("localhost", "root", "", "pcrequest");
$output = '';
$colors = array();
$colors["Approved"] = "#00FF00";
$colors["Declined"] = "red";
$colors["Pending"] = "#FF00FF";
 $search = mysqli_real_escape_string($connect, $_POST["query"]);
 $query = "
 SELECT * FROM request 
 WHERE empname LIKE '%".$search."%'
 OR position LIKE '%".$search."%' 
 OR platform LIKE '%".$search."%'
 OR processor LIKE '%".$search."%'
 OR ram LIKE '%".$search."%'
 OR monitor LIKE '%".$search."%'
 OR phone LIKE '%".$search."%'
 OR phonetype LIKE '%".$search."%'
 OR headset LIKE '%".$search."%'
 $query = "
 SELECT * FROM request ORDER BY status";
$result = mysqli_query($connect, $query);
if(mysqli_num_rows($result) > 0)
 $output .= '<div class="table-responsive">
     <table class="table table bordered">
       <th>Requested by</th>
       <th>Start Date</th>
                <th>Phone Type</th>
                <th>Approval Status</th>
 while($row = mysqli_fetch_array($result))
    $output .= '<tr>
                <td style="color:' . $colors[$row["approval"]] . ';">' .$row["approval"] . '</td>';
       if ($row['status']) :
          $output .= '<td>'.$row["status"].'</td> ';
          $output .= '
                    <form method="post" action="update-request-status.php">
                        <input type="hidden" name="reqnumber" value="'.$row['reqnumber'].'" />
                        <button class="button" type="submit" name="completed" value=""><span>In Progress!</span></button>
                <td><a href="records.php?reqnumber='.$row['reqnumber'] .'"><i class="fa fa-edit" style="color: black; font-size: 25px;"></i></a></td>
                 <form method="post" action="update-approval-status.php">
                  <input type="hidden" name="reqnumber" value="'.$row['reqnumber'].'" />
                  <button class="fa fa-check" style="color: green" type="submit" name="approve" value=""></button><button class="fa fa-close" style="color: red" type="submit" name="decline" value=""></button>
echo $output;
 echo 'Data Not Found';

And here's the form to mail code:


error_reporting(E_ALL); ini_set('display_errors', 1);

require_once ('database.php');

if (isset($_POST['send'])) {

$reqname = $_POST['reqname'];
$day = $_POST['day'];
$month = $_POST['month'];
$year = $_POST['year'];
$empname = $_POST['empname'];
$position = ($_POST['position']);
$account = $_POST['account'];
$platform = $_POST['platform'];
$processor = $_POST['processor'];
$ram = $_POST['ram'];
$monitor = $_POST['monitor'];
$phone = $_POST['phone'];
$phonetype = $_POST['phonetype'];
$headset = $_POST['headset'];
$status = $_POST['status'];
$approval = $_POST['approval'];

$insert_query = "INSERT INTO request (reqname, day, month, year, empname, position, account, platform, processor, ram, monitor, phone, phonetype, headset, status, approval)
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";

$insert = $database->prepare($insert_query);
$insert->execute(array($reqname, $day, $month, $year, $empname, $position, $account, $platform, $processor, $ram, $monitor, $phone, $phonetype, $headset, $status, $approval));

$email_from = "PC Request";//<== update the email address
$email_subject = "PC Request for $account";
$message = '<html><body>';
$message .= '<h1>Hello, World!</h1>';
$message .= '</body></html>';

$message = '<html><body>';
$message .= "<tr><td>Good morning,</td></tr>";
$message .= "<br>";
$message .= "<br>";
$message .= "<tr><td>Here are the specifications:</td></tr>";
$message .= "<br>";
$message .= "<br>";
$message .= '<table rules="all" style="border-color: #666;" cellpadding="5">';
$message .= "<tr style='background: #CDD9FF;'><td><strong>Requested by</strong> </td><td>" . strip_tags($_POST['reqname']) . "</td></tr>";
$message .= "<tr style='background: #FFFFFF;'><td><strong>Start Date</strong> </td><td>" . strip_tags($_POST['month'].'/'.$_POST['day'].'/'.$_POST['year']) . "</td></tr>";
$message .= "<tr style='background: #CDD9FF;'><td><strong>Employee</strong> </td><td>" . strip_tags($_POST['empname']) . "</td></tr>";
$message .= "<tr style='background: #FFFFFF;'><td><strong>Position</strong> </td><td>" . strip_tags($_POST['position']) . "</td></tr>";
$message .= "<tr style='background: #CDD9FF;'><td><strong>Account</strong> </td><td>" . strip_tags($_POST['account']) . "</td></tr>";
$message .= "<tr style='background: #FFFFFF;'><td><strong>Platform</strong> </td><td>" . strip_tags($_POST['platform']) . "</td></tr>";
$message .= "<tr style='background: #CDD9FF;'><td><strong>Processor</strong> </td><td>" . strip_tags($_POST['processor']) . "</td></tr>";
$message .= "<tr style='background: #FFFFFF;'><td><strong>RAM</strong> </td><td>" . strip_tags($_POST['ram']) . "</td></tr>";
$message .= "<tr style='background: #CDD9FF;'><td><strong>Monitor</strong> </td><td>" . strip_tags($_POST['monitor']) . "</td></tr>";
$message .= "<tr style='background: #FFFFFF;'><td><strong>Phone</strong> </td><td>" . strip_tags($_POST['phone']) . "</td></tr>";
$message .= "<tr style='background: #CDD9FF;'><td><strong>Phone Type</strong> </td><td>" . strip_tags($_POST['phonetype']) . "</td></tr>";
$message .= "<tr style='background: #FFFFFF;'><td><strong>Headset</strong> </td><td>" . strip_tags($_POST['headset']) . "</td></tr>";
$message .= "<tr style='background: #CDD9FF;'><td><strong>View Requests</strong> </td><td><form method='post' action='update-approval-status.php'>
                  <input type='hidden' name='reqnumber' value='".$row['reqnumber']."' />
                  <button class='fa fa-check' style='color: green' type='submit' name='approve' value=''>Approve</button><button class='fa fa-close' style='color: red' type='submit' name='decline' value=''>Decline</button>
$message .= "</table>";
$message .= "</body></html>";
$to = "[email protected], [email protected]";//<== update the email address
$headers = "From: $email_from \r\n";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";
//$headers .= "Reply-To: $visitor_email \r\n";
//Send the email!
mail($to,$email_subject,$message, $headers);
//done. redirect to thank-you page.
//header('Location: index.php');

echo "<script>alert('Successfully sent!'); window.location='index.php'</script>";

Related Questions