Click on button, change user status to active in field and send this user automatically e-mail - email

I have the code to update the user in the application that updates the user in the database.
My problem is that when I click on the button aktivan = active (when the user is registered there status neaktivan = inactive, so I'm as administrators have to approve) to automatically send e-mail to that he was active?
<div class="control-group <?php echo !empty($status_racunaError)?'error':'';?>">
<label class="control-label">Status računa:</label>
<div class="controls">
<input name="status_racuna" id="status_racuna" type="text" placeholder="Status računa" value="<?php echo !empty($status_racuna)?$status_racuna:'';?>"> &nbsp
<button name="btn-upload" type="submit" id="btn-upload" class="btn btn-success" onclick="aktivan()"><i class="glyphicon glyphicon-send"></i> aktivan</button>
&nbsp <button type="button" onClick="neaktivan()" class="btn btn-danger">neaktivan</button>
<?php if (!empty($emailError)): ?>
<span class="help-inline"><?php echo $status_racunaError;?></span>
<?php endif;?>
</div>
</div>
<div class="form-actions">
<button type="submit" class="btn btn-success">Ažuriraj</button>
<a class="btn" href="admin_zona.php">Nazad</a>
</div>
</form>
</div>
The screenshot is below
I wrote this code for sending e-mail, but I do not know whether to call it from a new file or can call within the same (update.php)
echo "<div class=\"poruka_korisnik\"><p>Email je poslan na korisnikovu adresu. <i>$_POST[email]</i>. Molimo Vas da provjerite svoj e-mail.</p></div>";
$to = $_POST['email'];
$subject = "OPG Burza: obavijest o registraciji";
$message = "Dobrodošli!\r\r Hvala Vam što ste se registrirali na OPG Burzu. Vaš zahtjev za registracijom je odobren. Možete pristupiti našoj stranici na poveznici www.opg-burza.com.";
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=UTF-8' . "\r\n";
$headers .= 'From: OPG Burza team <noreply#opg-burza.com>' . "\r\n";
mail($to, $subject, $message, $headers);
}
I tried in every way, the new script, but something did not set well.
Any ideas or how to accurately define a script that sends e-mail just this particular user and can be from the same file update.php send the mail?

What is behind your
neaktivan()
aktivan()
function?
Js, background php code, etc
Try this before the form to handle the post:
<?php
if (isset($_POST['status_racuna']) && isset($_POST['user_id'])) {
#do some validation here
$status_racuna = $_POST['status_racuna'];
$user_id = $_POST['user_id'];
$sql="UPDATE users SET activ= '{$status_racuna}' where id={$user_id}";
$updated = $db->query($sql);
if ($updated) {
#display success message
# send mail
} else {
# display the error message
}
}
?>
I cannot see you opened the form or not.
and add hidden input with name user_id.

Related

Redirection not working after sending Email in CakePHP

I am sending a Email in Contact-Us form in CakePHP 2. Mail is being send properly but the problem is after sending Email it is not redirecting to Home page. I am using SMTP to send email.
My Controller method
public function contactForm()
{
if ($this->request->is(['post','put'])) {
$name = $this->request->data['home']['name'];
$email = $this->request->data['home']['email'];
$phone = $this->request->data['home']['phone'];
$message = $this->request->data['home']['message'];
$emailMessage = "Name - $name\n";
$emailMessage .= "Email - $email\n";
$emailMessage .= "Phone - $phone\n";
$emailMessage .= "Message - $message\n";
$Email = new CakeEmail();
$Email->config('smtp');
$Email->from(array($email =>$name));
$Email->to('info#myserver.com');
$Email->subject('Contact Us - Form');
$Email->send($emailMessage);
$this->Session->setFlash('Thank for contacting us. Our representative will reach you soon.','flash', array('alert'=> 'success'));
$this->redirect(array('action' => 'index'));
}
}
The index method is also there. Here is my HTML form
<?php echo $this->Form->create('home', array( 'controller' => 'homes', 'action' => 'contactForm','name'=>'contact-form','id'=>'contact-form','class'=>'contact-form'));?>
<div class="row flex-box">
<fieldset class="column column-1-2">
<label>YOUR NAME</label>
<?php echo $this->Form->input('name',array('type'=>'text','id'=>'name','value'=>'','label' => false,'class'=>'text-input validate[required]','div'=>false));?>
<label>YOUR EMAIL</label>
<?php echo $this->Form->input('email',array('type'=>'email','id'=>'email','value'=>'','label' => false,'class'=>'text-input validate[required]','div'=>false));?>
<label>YOUR PHONE</label>
<?php echo $this->Form->input('phone',array('type'=>'tel','id'=>'phone','value'=>'','label' => false,'class'=>'text-input validate[required]','div'=>false));?>
</fieldset>
<fieldset class="column column-1-2">
<label>MESSAGE</label>
<?php echo $this->Form->input('message',array('type'=>'textarea','id'=>'content','value'=>'','div'=>false));?>
</fieldset>
</div>
<div class="row margin-top-30">
<div class="column column-1-1">
<div class="row margin-top-15 padding-bottom-16 align-center">
<button type="submit" class="more submit-contact-form" style="display:inline-block;">Send message</button>
</div>
</div>
</div>
</form>
I am receiving mail properly. Not able to distinguish what is the error.
Any help will be appreciated.
Please check if you properly closed your PHP controller file and there should be no spaces after closing ?> PHP tags in controller file.
Also check other controllers for same.

How can we send the table data to mail using php?

i am displaying the daily report submitted by the user now i want to mail the same to email when the send email button is clicked. how can i do that in PHP ?
i used the following code
extract($_REQUEST);
if(isset($send_mail)){
$edata= $_POST['send_msg'];
echo "<script type='text/javascript'>alert('$edata');</script>";
$ka_semail=$_SESSION['user_email'];
$subject = "Report";
$body = $edata;
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$headers .= 'From: Info <'.$ka_semail.'>' . "\r\n";
$mchk= mail($ka_semail, $subject, $body, $headers);
if(isset($mchk)){
echo "<script type='text/javascript'>alert('Your data Sent sucessfully');</script>";
header("location:reports.php");
}
}
?>
<body>
<div id="main">
<header>
<div id="logo">
<div id="logo_text">
<a href="index.html">
<div style="background:#2A0000"><img src="images/kreamsoftlogo.png"/></a></div>
</div>
</div>
<?php include'nav.php'; ?>
</header>
<div id="site_content">
<div id="email-data">
<div id="content">
<div class="content" style="min-height:385px; width: 100%" >
<h1>Daily Report </h1>
<?
$current_date = date("d/m/Y");
$current_user = $_SESSION['user_login'];
$find = mysql_query("select * from k_dailyreport where kr_user='$current_user' AND kr_date='$current_date'");
?>
<div class="content_item">
<ul>
<?php
$numrow=mysql_num_rows($find);
if($numrow>0){
$sino = 0;
echo "<table border='0' width='100%'>";
echo "<tr class='head'>";
echo "<th>S.No</th>";
echo "<th>Project</th>";
echo "<th>Process</th>";
echo "<th>Process Date</th>";
echo "<th>Current Duration</th>";
echo "</tr>";
$sno=0;
while($row = mysql_fetch_array($find)){
$sno=$sno +1;
$sino++;
$cls = ($sino%2==0) ? "even" : "odd";
echo "<tr class='t1'>";
echo "<td>$sno</td>";
echo "<td>".$row['kr_project']."</td>";
echo "<td>".$row['kr_process']."</td>";
echo "<td>".$row['kr_rtime']."</td>";
echo "<td>".$row['kr_ctime']."</td>";
}?></td>
<?php
echo "</tr>";
?><form name="test" method="post"enctype="multipart/form-data" action="<?php echo $_SERVER['PHP_SELF'];?>">
<p style='padding-top: 15px;margin-right: 50px; float:right'><input class='submit' id="sendEmail" type="submit" name='sendmail' value='Send Mail' />
<input type="hidden" name="send_mail" value="send_mail" />
<input type="hidden" name="send_msg" value="<?php
$numrow=mysql_num_rows($find);
if($numrow>0){
$sino = 0;
echo "<table border='0' width='100%'>";
echo "<tr class='head'>";
echo "<th>S.No</th>";
echo "<th>Project</th>";
echo "<th>Process</th>";
echo "<th>Process Date</th>";
echo "<th>Current Duration</th>";
echo "</tr>";
$sno=0;
while($row = mysql_fetch_array($find)){
$sno=$sno +1;
$sino++;
$cls = ($sino%2==0) ? "even" : "odd";
echo "<tr class='t1'>";
echo "<td>$sno</td>";
echo "<td>".$row[1]."</td>";
echo "<td>".$row[2]."</td>";
echo "<td>".$row[3]."</td>";
echo "<td>".$row[3]."</td>";
echo "</tr>";
}echo "</table>";}?>" /></p>
</form>
<?
}else{
?>
<center><img style="text-align:center" src="images/no_record.gif" /></center>
<?
}
echo "</table>";
?>
</li>
</ul>
</div>
</div> </div>
</div>
</div>
i want to mail the entire table with data
Without knowing anything about your code, I can perhaps at least guess as to the structure of what builds this table. Maybe something along the lines of this?:
writeTableHeader();
foreach ($values as $value) {
writeTableRow($value);
}
writeTableFooter();
Maybe even abstracted into its own higher-level function?:
function writeTable($values) {
$result = '';
$result .= buildTableHeader();
foreach ($values as $value) {
$result .= buildTableRow($value);
}
$result .= buildTableFooter();
return $result;
}
Any such function or abstraction can be used to construct the HTML for the email body as well...
// ... previously building the overall email body
$mailBody .= writeTable($values);
// ... maybe some more email body elements as well
mail($to, $subject, $mailBody);
Styling the email will be another story, of course. It's best not to rely on linked resources in an HTML email, those tend not to be requested by mail clients as they're often abused by spammers and the like. You can perhaps include the style sheet(s) as an "embedded resource" in the email, though in most cases I think inline styling is probably best for emails. (Some mail clients might not even load an embedded resource, at least not without first prompting the user, which isn't an ideal user experience.)
Assuming you don't want to duplicate much code, the above method can be abstracted using the Form Template Method refactoring pattern (example here, among others).
I guess the point I'm getting at is that the email doesn't send the web page, but rather the web page and the email are both independent "views" of the underlying data. Common code between them can be abstracted and shared of course, but trying to re-use a webpage itself in an email is over-complicating the situation.

PHP form submits regardless of required/error handling attempts

newbie here
I am currently attempting to build a simple contact form to learn how best to build one. I've tried to make several fields required (name, email, do you like spam radio button). The required html5 element isn't working for me or I am not implementing it correctly based on what i've read here, on google and other places. Likewise the error/require messages don't display if someone attempts to submit a message. Instead the form goes through on submit even if NO content anywhere has been entered. I built this yesterday and have been working on the errors for about 24 hours now and am hoping to find some help here.
I currently am returning no errors, the form sends all information entered as hoped, but it also sends if NO information is entered and that doesn't seem like the best possible form to me. I've tried to build a function to help the required, but that doesn't seem to be working either.
Again, i'm new so if my mistakes are obvious and silly, please help em to understand them, that would be greatly appreciated.
current example can be found here
My code:
<!-- For PoohPot -->
<style TYPE="text/css">
input[type="text"]#website { display: none; }
input#website { display: none; }
.vSmall {font-size: 50%; text-align: center;}
</style>
<script>
$('form').submit(function(){
if ($('input#website').val().length != 0) {
return false;
}
});
</script>
<?php
//template.php
include '_inc/config.php';//holds arrays, et al.
include '_inc/functions.php';//holds functions that call arrays, etc
include '_inc/head.php';//Begin Document
//include '_inc/header.php';//Begin Content
?>
<article class="container box style3">
<header>
<img src="_img/icons/icon_lg-max-o-matic2.png" alt="php icon">
<h2><?=NAME_SITE?></h2>
<p>Spam Survey</p>
</header>
<?php
$to = "chezshire#gmail.com";
$subject="Spam Survey";
//----- end conf ----- //
// define variables and set to empty values
$nameErr = $emailErr = $spamErr = "";
$userName = $userEmail = $userMessage = $website = "";
if ($_SERVER["REQUEST_METHOD"] == "POST")
{
if (empty($_POST["userName"]))
{$nameErr = "* Name is required";}
else
{$name = testInput($_POST["userName"]);}
if (empty($_POST["userEmail"]))
{$emailErr = "* Email is required";}
else
{$email = testInput($_POST["userEmail"]);}
if (empty($_POST["spamErr"]))
{$emailErr = "* You must select an option";}
else
{$email = testInput($_POST["userEmail"]);}
if (empty($_POST["userMessage"]))
{$comment = "";}
else
{$comment = testInput($_POST["userMessage"]);}
}
if(isset($_POST['userName']))
{//data is submitted, show it
//echo $_POST['userName'];
$text = 'From: ' . $_POST['userName'] . PHP_EOL;
//$text .= $_POST['Comments']; //add on to text
$text .= process_post(); // uses proces_post to handle comments and stuff
$from = $_POST['userEmail'];
/*
echo '<pre>';
var_dump($_POST);
echo '</pre>';
*/
//domain spoofing controls
$headers = 'From: noreply#max-o-matic.com' . PHP_EOL . //from where formhandler sits
'Reply-To: ' . $from . PHP_EOL . //where replies are to go to
'X-Mailer: PHP/' . phpversion();
mail($to, $subject, $text, $headers); //from becomes headers
echo '<div class="row">
<div class="12u">
<p>Thanxs!</p>
<p>
<br />RESET
</p>
</div>
</div>
</article>
';
/*
echo '<pre>';
var_dump($_POST);
echo '</pre>';
*/
}else{//no data, show form
echo '
<form action="' . THIS_PAGE . '" method="post">
<div class="row half">
<div class="6u">
<input type="text" class="text" name="userName" placeholder="Name?" required autofocus />
<span class="error"><?php echo $nameErr;?></span>
</div>
<div class="6u">
<input type="text" class="text" name="userEmail" placeholder="Email" required />
<span class="error"><?php echo $emailErr;?></span>
</div>
</div>
<div class="row half">
<div class="6u">
Do you like spam?<br />
<input type="radio" name="Please_Spam_Me" value="YES!" required value="1" />Yes<br />
<input type="radio" name="Please_Spam_Me" value="NO!" />No<br />
<span class="error"><?php echo $spamErr;?>
</div>
<div class="6u">
How much Spam is too much?<br />
<input type="checkbox" name="checkbox-group" class="group-required" id="checkbox-group-id" value="6+" >6 or more<br />
<input type="checkbox" name="checkbox-group" value="2+">2 to 5<br />
<input type="checkbox" name="checkbox-group" value="Any">ANY<br /><br />
</div>
</div>
<!--For Winnie the Pooh -->
<input id="website" name="website" type="text" value"Website" />
<div class="row half">
<div class="12u">
<textarea name="userMessage" placeholder="What is your opinion on Spam?"></textarea>
</div>
</div>
<div class="row">
<div class="12u">
<ul class="actions">
<li>SEND!</li>
<input type="submit" />
</ul>
<p class="vSmall">
* Please note, instead of a reCaptcha I build my own \'honeypot\' from scratch.
</p>
</div>
</div>
</form>
</article>
';
}
function testInput($data)
{
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
return $data;
}
// this is a function
function process_post()
{//loop through POST vars and return a single string
$myReturn = ''; //set to initial empty value
foreach($_POST as $varName=> $value)//$_POST is an array
{#loop POST vars to create JS array on the current page - include email
$strippedVarName = str_replace("_"," ",$varName);#remove underscores
if(is_array($_POST[$varName]))
{#checkboxes are arrays, and we need to collapse the array to comma separated string!
$myReturn .= $strippedVarName . ": " . implode(",",$_POST[$varName]) . PHP_EOL;
}else{//not an array, create line
$myReturn .= $strippedVarName . ": " . $value . PHP_EOL;
}
}
return $myReturn;
}
include "_inc/footer.php";//End Content/Document
?>
If you're trying to use HTML5 stuffs then you need the HTML5 doctype. Place this at the start of your output and see what happens:
<!DOCTYPE html>
Also, typo value"Website" should be value="Website"

How do I send a multiple field form to email with multiple variables?

I've spent most of today and all of yesterday just trying to get a simple, basic send-to-email PHP code to work. Here is the code I've tried putting together myself after extensively researching, checking, and editing based on a lot of other threads I've read on this website and on other sites.
<?php
$to='dustedpages#gmail.com';
$subject='Commission Inquiry';
$email = $_REQUEST['Email'] ;
$headers = "From: $email";
$sent = mail($to, $subject, $message, $headers) ;
$message="Name: ".$name. "\r\n" . "Email: " .$email . "\r\n" . "Username: " .$user . "\r\n" . "Type: ".$position. "\r\n" . "Add-Ons: ".$addons. "\r\n" . "Price: ".$price. "\r\n" . "Brief Description: ".$briefdesc;
$name=$_POST['FirstName'],['LastName'];
$email=$_POST['Email'];
$user=$_POST['User'];
$position=$_POST['position'];
$addons=$_POST['cat'],['dog'];
$price=$_POST['price'];
$desc=$_POST['briefdesc'];
if($sent)
{print "Your mail was sent successfully. Please be patient and the artist will contact you soon. Thanks!"; }
else
{print "We encountered an error sending your mail"; }
?>
There is actually a LOT more form data than just that basic information, but I cannot even get the basic information to show up. For the portion of html codes it is pulling from, here is some of what it looks like.
<!--NAME-->
</li> <li id="li_2" >
<label class="description" for="element_2">Name </label>
<span>
<input onkeypress="return handleEnter(this, event)" id="element_2_1" name="FirstName" class="element text" maxlength="255" size="14" value=""/>
<label>First</label>
</span>
<span>
<input onkeypress="return handleEnter(this, event)" id="element_2_2" name="LastName" class="element text" maxlength="255" size="18" value=""/>
<label>Last</label>
</span>
<p class="guidelines" id="guide_2"><small>Please fill this in as the name I will see on Paypal.</small></p>
<!--EMAIL-->
</li> <li id="li_1" >
<label class="description" for="element_1">Email </label>
<div>
<input onkeypress="return handleEnter(this, event)" id="element_1" name="Email" class="element text medium" type="text" maxlength="255" value=""/>
</div>
<p class="guidelines" id="guide_1"><small>Your Paypal email</small></p>
<!--USERNAME-->
</li> <li id="li_3" >
<label class="description" for="element_3">DA/Tumblr Username </label>
<div>
<input onkeypress="return handleEnter(this, event)" id="element_3" name="User" class="element text medium" type="text" maxlength="255" value=""/>
</div>
<p class="guidelines" id="guide_3"><small>If you have an account on either Deviantart or Tumblr and wish to be credited in the commission description, please note it here.</small></p>
</li>
<p>
<!--POSITION TYPE-->
</li> <li id="li_26" >
<label class="description" for="element_26">Comm. Type </label>
<div>
<select class="element select medium" id="element_26" name="position">
<option value="" selected="selected"></option>
<option value="1" >Bust</option>
<option value="2" >Waist</option>
<option value="3" >Full-Body</option>
</select>
</div>
<p class="guidelines" id="guide_26">
<small>Please choose what position you wish for your character(s) to be viewed from. Price will change depending on what type you choose.</small></p>
<!--ADD ONS-->
</li> <li id="li_27" >
<label class="description" for="element_27" name="addons">Add-Ons </label>
<span>
<input name="cat" type="checkbox" onClick="return KeepCount()">
+1 Character<br>
<input name="dog" type="checkbox" onClick="return KeepCount()">
+2 Characters
I have multiple fields (such as the First and Last Name) that I need to be one single variable in the email code, and I need to be able to display all of the filled out form fields in an email. I can't even get more than one variable to show up at this point.
Also the form itself includes selects, checkboxes, textareas, and image uploads. I haven't even gotten to that point - I'm just trying to make some of the basic information show up right now. I'm very familiar with HTML and Javascript, just not so much PHP, though I've worked with it before. Some of the form data is used in Javascript code on the same page as the HTML, also. The actual PHP data is on a separate file that my form pulls from.
I think what's happening is you're attempting to use variables before they have the value you would like. Try something like the following and let me know how it goes.
<?php
$to = 'dustedpages#gmail.com';
$subject = 'Commission Inquiry';
$headers = "From: $email";
$fname = $_POST['FirstName'];
$lname = $_POST['LastName'];
$email = $_POST['Email'];
$user = $_POST['User'];
$position = "CHANGE-ME";
$addons = "CHANGE-ME";
$price = "CHANGE-ME";
$briefdesc = "CHANGE-ME";
$message = "Name: ".$fname.", ".$lname."\r\n";
$message .= "Email: $email \r\n";
$message .= "Username: $user \r\n";
$message .= "Type: $position \r\n";
$message .= "Add-Ons: $addons \r\n";
$message .= "Price: $price \r\n";
$message .= "Brief Description: $briefdesc \r\n";
$sent = mail($to, $subject, $message, $headers);
if($sent) {
echo "Success";
}
else {
echo "Failure";
}
?>

Contact page template - redirect to 'thank you page' not working

I'm trying to submit a form via custom page template but the problem is that it only works with form action="<?php the_permalink() ?>" and I need the form to be submitted and redirected to something like this form action="<?php bloginfo('url')?>/message-sent?id=<?php the_ID() ?>"
Full code:
<?php
$emailError = '';
if(isset($_POST['submitted'])) {
$email = trim($_POST['email']);
//setup self email address
$emailTo = $email;
$subject = "[reminder] Don't forget to download " . get_the_title();
$body = "Name: $name \n\nEmail: $email \n\nComments: $comments";
$headers = 'From: Myemail reminders <no-reply#xyz.com>' . "\r\n";
wp_mail($emailTo, $subject, $body, $headers);
$emailSent = true;
} ?>
<section class="box grid_9 list_posts">
<div class="inner">
<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
<div <?php post_class() ?> id="post-<?php the_ID(); ?>">
<div class="entry-content">
<div class="contact-form clearfix">
<?php if(isset($emailSent) && $emailSent == true) { ?>
<div class="thanks">
<?php _e('Thanks, your email was sent successfully.', 'framework') ?>
</div>
<?php } else { ?>
<?php the_content(); ?>
<?php if(isset($hasError) || isset($captchaError)) { ?>
<p class="error"><?php _e('Sorry, an error occured.', 'framework') ?>
<?php } ?>
<form action="<?php the_permalink()?>" id="contactForm" method="post">
<ul class="contactform">
<li>
<input type="email" name="email" id="email" value="<?php if(isset($_POST['email'])) echo $_POST['email'];?>" class="required requiredField email" required="required" />
</li>
<li class="buttons">
<input type="hidden" name="submitted" id="submitted" value="true" />
<input type="submit" value="Remind Me!"></input>
</li>
</ul></form>
<?php } ?></div>
</div>
</div>
<?php endwhile; else: ?>
<div id="post-0" <?php post_class() ?>>
<h1 class="entry-title"><?php _e('Error 404 - Not Found', 'framework') ?></h1>
</div>
<?php endif; ?></div>
</section>
I got no php errors in log, page is redirected successfully, but no email is sent. When using the_permalink, everything works just fine.
When submitting the form data to a different script, make sure the code for (validating the input and) sending the email is in that very file.
Otherwise, your URL /message-sent might rewrite to a completely different script and the script with the above code isn't involved at all once the submit button gets clicked.
Did that held you? Feel free, to ask, if my wording is incomprehensible or if my description isn't clear to you
Maybe you forgot to put ".php" at the end of your /message-sent?id=xxx file, i.e /message-sent.php?id=xxx?
Another thought: It is always a good idea to filter the user input, because you will receive a lot of spam, put some sort of CAPTCHA validation code and sanitize/validate the whole user input text, i.e. every text, which comes from input fields of your form.
For email:
$email = filter_var($_POST['email'], FILTER_VALIDATE_EMAIL);
For name and comments:
$name = filter_var($_POST['name'], FILTER_SANITIZE_STRING);
$comments = filter_var(strip_tags($_POST['comments']), FILTER_SANITIZE_STRING);