I have an if isset statement that fires when my site receives approval from my merchant account api after a form is submitted. Everything seems to work except for the order details from the form getting written to my database. Here is the code for my form:
<form id="input-form" class="form-inline" action="" method="post">
<?php
if($error <> '') {
echo '<div class="alert alert-danger">',$error,'</div>';
}
?>
<div id="makepayment">
<div class="form-group">
<label for="order_fname">First Name</label>
<input class="form-control" style="width: 273px;" type="text" name="order_fname" id="order_fname" value="<?php echo $user['user_fname']; ?>" placeholder="First Name" />
</div>
<div class="form-group">
<label for="order_lname">Last Name</label>
<input class="form-control" style="width: 273px;" type="text" name="order_lname" id="order_lname" value="<?php echo $user['user_lname']; ?>" placeholder="Last Name" />
</div>
<div class="form-group">
<label for="order_phone">Primary Phone</label>
<input class="form-control" style="width: 273px;" type="text" name="order_phone" id="order_phone" value="<?php echo $user['user_phone']; ?>" placeholder="Phone Number" />
</div>
<div class="form-group">
<label for="order_cell">Cell Phone</label>
<input class="form-control" style="width: 273px;" type="text" name="order_cell" id="order_cell" value="<?php echo $user['user_cell']; ?>" placeholder="Cell Phone" />
</div>
<div class="form-group">
<label for="order_email">Email</label>
<input class="form-control" style="width: 273px;" type="text" name="order_email" id="order_email" value="<?php echo $user['user_email']; ?>" placeholder="Email Address" />
</div><br/>
<div class="clear"></div>
<div class="form-group" style="width:100% !important">
<label for="company_id">Property Information</label><br/>
<br/>
<input id="company_id" type="hidden" id="company_id" name="company_id" value="<?php echo $company['company_id']; ?>" />
<input id="company_name" type="hidden" id="company_name" name="company_name" value="<?php echo $company['company_name']; ?>" />
<div class="property">
<strong><?php echo $company['company_name']; ?></strong><br/>
<?php echo $company['company_street']; ?><br/>
<?php echo $company['company_city']; ?>, <?php echo $company['company_state']; ?> <?php echo $company['company_zip']; ?>
</div>
<div class="clear"></div>
</div><br/>
<div class="clear"></div>
<div class="form-group">
<label for="order_cc">Card Number</label>
<input class="form-control" type="text" name="ssl_card_number" id="ssl_card_number" style="width:100% !important" />
</div><br/>
<div class="clear"></div>
<div class="form-group">
<label for="order_code">Security Code</label>
<input class="form-control" style="width: 75px;" type="text" name="order_code" id="order_code" placeholder="ex:123" />
</div>
<div class="form-group">
<label for="order_customernumber">Customer Number</label>
<input class="form-control" type="text" style="width: 75px;" name="order_customernumber" id="order_customernumber" />
</div>
<div class="form-group">
<label for="order_invoicenumber">Invoice Number</label>
<input class="form-control" type="text" style="width: 75px;" name="order_invoicenumber" id="order_invoicenumber" />
</div>
<div class="form-group">
<label for="order_amount">Payment Amount</label>
$ <input class="form-control" type="text" style="width: 100px" name="ssl_amount" id="ssl_amount" />
</div>
<div class="form-group" style="width:100% !important">
<input class="btn btn-primary" type="submit" name="submit" value="Submit" />
</div>
</div>
</form>
Here is my if isset statement that fires if the api sends back an approval code. I know that part works because it does this action when I put in my card info and throws the proper error when I put in fake info.
if(isset($response['ssl_result_message']) && $response['ssl_result_message'] == "APPROVAL") {
//If transaction successful write to database
$userid = $session['user_id'];
$addOrderSQL = "
INSERT INTO orders (
user_id,
company_id,
order_fname,
order_lname,
order_phone,
order_cell,
order_email,
company_name,
order_customernumber,
order_invoicenumber,
order_amount
)
VALUES (
'$userid',
'$_POST[company_id]',
'$_POST[order_fname]',
'$_POST[order_lname]',
'$_POST[order_phone]',
'$_POST[order_cell]',
'$_POST[order_email]',
'$_POST[company_name]',
'$_POST[order_customernumber]',
'$_POST[order_invoicenumber]',
'$_POST[ssl_amount]'
)";
$firstname= $_POST['order_fname'];
$lastname = $_POST['order_lname'];
$invoice = $_POST['order_invoicenumber'];
$customernum = $_POST['order_customernumber'];
$property = $_POST['company_name'];
$ordertotal = $_POST['ssl_amount'];
/* -------------------------------------------------
Send Email to Administrator
------------------------------------------------- */
$to = "web#abcprintingink.com";
$subject = "Payment for Customer # ". $customernum;
$headers = "Content-type: text/html; charset=iso-8859-1\r\n";
$headers .= "From: webmaster#turfmastersct.com" . "\r\n" .
"Reply-To: webmaster#example.com" . "\r\n" .
"X-Mailer: PHP/" . phpversion();
$message = "<p>A customer has completed a payment. Here are the payment details:</p>
Customer Name: ". $firstname ." ". $lastname ."<br/>
Customer #: ". $customernum ."<br/>
Invoice #: ". $invoice ."<br/>
Property: ". $property ."<br/>
Payment Amount: $". $ordertotal;
// Send it.
mail($to, $subject, $message, $headers);
//once executed queries redirect user
header('Location: /payments/orders.php');
exit;
}
elseif(isset($response['errorCode'])) {
$error = 'Your credit card has been declined';
}
else {
$error = 'Failed to process your payment';
}
The form is communicating with the api, I get a response from it and I can see the payment details post to my demo account. The isset statement fires, I get the email that it sends and it redirects me to the orders.php page like it's supposed to. It just doesn't write to the database. You can see my form and you can see the fields I am writing to my database. All those fields exist in the database itself. Am I missing something here?
You never run the $addOrderSQL query.
See mysqli_query() to do this.
Related
I'm not very good at PHP (more of a front-end developer) and I'm doing a website for a client. I got everything working up to now except for one thing. When the user fills out the contact form and submits it successfully i want to display a message that tells them the email was sent. I already have the div set up I just don't know who to execute it with PHP.
<div class="sent">Email has been sent.</div>
<form action="" method="POST" class="contact-form">
<img class="close" src="images/close-icon.png">
<div class="top-title">Contact Us</div>
<input type="hidden" name="action" value="submit">
<div class="input-title">Name*</div>
<input class="input" name="name" type="text" value="" size="30" required><br>
<div class="input-title">Phone Number*</div>
<input class="input" name="phone" type="text" value="" size="30" required><br>
<div class="input-title">Email Address*</div>
<input class="input" name="email" type="text" value="" size="30" required><br>
<div class="input-title">How did you hear about us?</div>
<input class="input" name="company" type="text" value="" size="30"/><br>
<div class="input-title">Let us know how we can help you</div>
<textarea class="textarea" name="message" rows="7" cols="30"></textarea><br>
<div class="required">*Fill is required</div>
<input class="submit-button" type="submit" value="Send Email"/>
</form>
<?php
{
$name=$_POST['name'];
$email=$_POST['email'];
$phone=$_POST['phone'];
$company=$_POST['company'];
$message= "\r\nName: " . $_POST['name'] ."\r\nPhone Number: ". $_POST['phone']."\r\nEmail: ". $_POST['email'] ."\r\nHow you know the company: ".$_POST['company'] ."\r\nReason for message: ".$_POST['message'];
$from="From: $name<$email>\r\nReturn-path: $email";
$subject="GS Website Message";
mail("name#email.com", $subject, $message, $from);
}
?>
Change: (and using a conditional statement)
mail("name#email.com", $subject, $message, $from);
to:
if(mail("name#email.com", $subject, $message, $from)){
$sent = "Message sent";
}
Then modify:
<div class="sent">Email has been sent.</div>
to read as:
<div class="sent"><?php if(!empty($sent)) { echo $sent; } ?></div>
Seeing you're new to PHP/forms, it's best to also check if your inputs are empty or not, otherwise you risk in either not getting mail, or receiving empty data.
http://php.net/manual/en/function.empty.php
Set inside a conditional statement also.
I.e.: using NOT empty: ! is the NOT operator in PHP.
if(!empty($_POST['var'])){...}
Edit:
The logic needs to be reversed.
First, place your PHP above your HTML form.
I also added a submit name attribute to your submit button, with a conditional statement and a ternary operator in the div.
<?php
if(isset($_POST['submit'])){
$name=$_POST['name'];
$email=$_POST['email'];
$phone=$_POST['phone'];
$company=$_POST['company'];
$message= "\r\nName: " . $_POST['name'] ."\r\nPhone Number: ". $_POST['phone']."\r\nEmail: ". $_POST['email'] ."\r\nHow you know the company: ".$_POST['company'] ."\r\nReason for message: ".$_POST['message'];
$from="From: $name<$email>\r\nReturn-path: $email";
$subject="GS Website Message";
if(mail("email#example.com", $subject, $message, $from)){
$sent = "Message sent";
}
}
?>
<div class="sent"><?php echo isset($sent) ? $sent : ''; ?></div>
<form action="" method="POST" class="contact-form">
<img class="close" src="images/close-icon.png">
<div class="top-title">Contact Us</div>
<input type="hidden" name="action" value="submit">
<div class="input-title">Name*</div>
<input class="input" name="name" type="text" value="" size="30" required><br>
<div class="input-title">Phone Number*</div>
<input class="input" name="phone" type="text" value="" size="30" required><br>
<div class="input-title">Email Address*</div>
<input class="input" name="email" type="text" value="" size="30" required><br>
<div class="input-title">How did you hear about us?</div>
<input class="input" name="company" type="text" value="" size="30"/><br>
<div class="input-title">Let us know how we can help you</div>
<textarea class="textarea" name="message" rows="7" cols="30"></textarea><br>
<div class="required">*Fill is required</div>
<input class="submit-button" type="submit" value="Send Email" name="submit"/>
</form>
$password=md5($_POST ['password']);
$password3 =md5($_POST ['password2']);
if($password != $password3)
{
$message = "Passwords do not match";
}
etc.
you need add
<form>
<div class="form-group">
<input type="password" name="password" id="password" class="form-control" placeholder="Password *" value="" required/>
</div>
<div class="form-group">
<div class="message"><?php if(!empty($message)) { echo $message; } ?></div>
<input type="password" name="password2" class="form-control" id="password2" placeholder="Confirm Password *" value="" required/>
</div>
</form>
I'm currently using an html5 website with a contact form and a php action to receive contact emails. I've tried a few different codes in my php but non have stopped the blank emails from coming. I do have google analytics enables on my code but have blocked the crawler through robot.txt. Here is my code.
PHP CODE
<?php
foreach ($_GET as $Field=>$Value) {
if($Value != ''){
$body .= "$Field: $Value\n";
}
}
$name = trim(htmlentities($_GET['name'],ENT_QUOTES,'utf-8'));
$email = trim(htmlentities($_GET['email'],ENT_QUOTES,'utf-8'));
$phone = trim(htmlentities($_GET['phone'],ENT_QUOTES,'utf-8'));
$messages = trim(htmlentities($_REQUEST['messages'],ENT_QUOTES,'utf-8'));
if (strlen($name) == 0 )
{
echo "<script>window.location = 'http://www.mason372.org/error.html'</script>";
}
if (strlen($email) == 0 )
{
echo "<script>window.location = 'http://www.mason372.org/error.html'</script>";
}
$to = "junior.8791#gmail.com";
$message = "Name: ".$name;
$message.="\n\nEmail: ".$email;
$message.="\n\nPhone: ".$phone;
$message .= "\n\nMessage: ".$messages;
$headers = "From: $email";
$headers .="\nReply-To: $email";
$success = mail($to, $subject, $message, $headers);
if ($success) {
echo "<script>window.location = 'http://www.mason372.org/thankyou.html'</script>";
} else {
echo "<script>window.location = 'http://www.mason372.org/error.html'</script>";
}
?>
CONTACT FORM
<form action="email.php" method="post" id="form">
<div class="5grid">
<div class="row">
<div class="6u">
<input type="text" class="name" name="name" id="name" placeholder="Name" value="" aria-describedby="name-format" required aria-required=”true” pattern="[A-Za-z-0-9]+\s[A-Za-z-'0-9]+" title="e.g.'John Doe'" required="" />
</div>
<div class="6u">
<input type="email" class="email" name="email" id="email" placeholder="Email" required="" />
</div>
</div>
<div class="row">
<div class="12u">
<input type="tel" class="tel" name="phone" id="phone" name="phone" type="text" placeholder="Phone Number" pattern="(?:\(\d{3}\)|\d{3})[- ]?\d{3}[- ]?\d{4}" required />
</div>
</div>
<div class="row">
<div class="12u">
<textarea name="messages" id="messages" placeholder="Message"></textarea>
</div>
</div>
<div class="row">
<div class="12u">
<input type="submit" class="button" value="Send Message">
<input type="reset" class="button button-alt" value="Clear Form">
I have the following code in my PHP form. How would i adjust it to include my Google Anayltic Code below?:
<?php
$sendMail="";
if (isset($_POST["sendemail"])){
$from = $_POST["email"];
$subject = $_POST["name"];
$message = $_POST["message"];
$message = wordwrap($message, 70);
$m=mail(get_post_meta( get_the_ID(), 'Seller Email', true ),$subject,$message,"From: $from\n");
if($m){
$sendMail="Message sent successfully.";
}else{
$sendMail="Message not sent.";
}
} ?>
<!--=== Contact Form ===-->
<form role="form" class="contactform" method="post">
<div class="form-group">
<label for="email">Your email address</label>
<input type="email" class="form-control" id="email" name="email" placeholder="Enter email" data-original-title="" title="">
</div>
<div class="form-group">
<label for="name">Your name</label>
<input type="text" class="form-control" id="name" name="name" placeholder="Enter name" data-original-title="" title="">
</div>
<div class="form-group">
<label for="message">Message</label>
<textarea class="form-control" id="message" name="message" placeholder="Information regarding property REF:<?php the_title();?>" style="height:100px;"></textarea>
</div>
<div class="form-group">
<button type="submit" name="sendemail" class="btn btn-lg btn-color">Send</button>
</div>
<?php if($sendMail!='') echo '<div class="form-group">'. $sendMail .'</div>';?>
</form>
</div><div style="clear:both;"></div>
My Google Analytics is the following, and I am unsure how I could add this onto the form on submission:
<script>
(function(i, s, o, g, r, a, m) {
i['GoogleAnalyticsObject'] = r;
i[r] = i[r] || function() {
(i[r].q = i[r].q || []).push(arguments)
}, i[r].l = 1 * new Date();
a = s.createElement(o),
m = s.getElementsByTagName(o)[0];
a.async = 1;
a.src = g;
m.parentNode.insertBefore(a, m)
})(window, document, 'script', '//www.google-analytics.com/analytics.js', 'ga');
ga('create', 'UA-50671476-1', 'auto');
ga('send', 'pageview');
</script>
You can track form submission by creating the Goal in your Google Analytics Account.
Goal Type : Destination Url
DO one thing, On Successfully Submitted the form take the user to another page and show "Message Successfully Sent" there and set the Goal on that page so that you can track the users who successfully submitted the form.
Code :
<?php
$sendMail="";
if (isset($_POST["sendemail"])){
$from = $_POST["email"];
$subject = $_POST["name"];
$message = $_POST["message"];
$message = wordwrap($message, 70);
$m=mail(get_post_meta( get_the_ID(), 'Seller Email', true ),$subject,$message,"From: $from\n");
if($m){
header('location:successfull.html');
}else{
$sendMail="Message not sent.";
}
} ?>
<!--=== Contact Form ===-->
<form role="form" class="contactform" method="post">
<div class="form-group">
<label for="email">Your email address</label>
<input type="email" class="form-control" id="email" name="email" placeholder="Enter email" data-original-title="" title="">
</div>
<div class="form-group">
<label for="name">Your name</label>
<input type="text" class="form-control" id="name" name="name" placeholder="Enter name" data-original-title="" title="">
</div>
<div class="form-group">
<label for="message">Message</label>
<textarea class="form-control" id="message" name="message" placeholder="Information regarding property REF:<?php the_title();?>" style="height:100px;"></textarea>
</div>
<div class="form-group">
<button type="submit" name="sendemail" class="btn btn-lg btn-color">Send</button>
</div>
<?php if($sendMail!='') echo '<div class="form-group">'. $sendMail .'</div>';?>
</form>
</div><div style="clear:both;"></div>
successfull.html :
Message Successfully Sent
Steps you have to done in Google Analytics account to setup the Goal :
Admin -> View -> Goals -> New Goal -> Goal Setup -> Custom
I have a contact form that is only passing over some of the form fields and i wondered if anyone can help as to why?
The html form is as follows:
<form enctype="multipart/form-data" method="post" action="referral.php" onsubmit="return submitForm()">
<fieldset>
<div class="field">
<input name="name" id="name" type="text" class="validate" placeholder="Your Name" />
<span class="form-msg" id="name-error">Please add your name</span>
</div>
<div class="field here-for-next-until">
<input name="email" id="email" type="text" class="validate" placeholder="Your E-mail" class="text-input validate" />
<span class="form-msg" id="email-error">Please add your e-mail address</span>
</div>
<div class="field">
<input name="phone" id="phone" type="text" placeholder="Your Phone Number" class="text-input validate" />
<span id="phone-error" class="form-msg" >Please add your phone number</span>
</div>
<div class="area here-for-next-until">
<textarea name="message" id="message" class="text-input validate" placeholder="Introduce Yourself"></textarea><br/>
<span class="form-msg" id="email-error">Please add an introduction to yourself</span>
<input type="text" id="cap_val" name="cap_val" class="code validate" placeholder="Enter Code From Right"/>
<img src="bin/captcha.php" id="cap"/><img src="images/refresh.jpg" id="cap-refresh" onclick="change_captcha()"/><br/>
<span id="captcha-error" class="form-msg" >Please add the above code</span>
<div class="clear here-for-next-until"></div>
</div>
</div>
<div class="flt-lt w420">
<h2 class="subheader">Client</h2>
<div class="field">
<input name="referralsname" id="name" type="text" class="validate" placeholder="Name" />
<span class="form-msg" id="name-error">Please add your referrals name</span>
</div>
<div class="field here-for-next-until">
<input name="referralsemail" id="email" type="text" class="validate" placeholder="E-mail address" class="text-input validate" />
<span class="form-msg" id="email-error">Please add your referrals e-mail address</span>
</div>
<div class="field">
<input name="referralsphone" id="phone" type="text" placeholder="Phone Number" class="text-input validate" />
<span id="phone-error" class="form-msg" >Please add your referrals phone number</span>
</div>
<div class="area here-for-next-until">
<textarea name="referralsmessage" id="message" class="text-input validate" placeholder="Introduce Yourself"></textarea><br/>
<span class="form-msg" id="email-error">Please add an introduction to yourself</span>
<div class="clear here-for-next-until"></div>
<div id="submit-holder">
<input class="submit" type="submit" value="Submit"/>
<input id="reset" class="submit" type="reset" value="Reset"/>
</div>
</div>
</fieldset>
</form>
Basically this is two very similar contact forms side by side that share a submit button
When posted it goes to this:
$owner_email = 'myemail.co.uk';
$subject = A message from your site visitor ' . $_POST["name"];
$messageBody = "\nVisitor: " . $_POST["name"] . "\n";
$messageBody .= "Email Address: " . $_POST['email'] . "\n";
$messageBody .= "Phone Number: " . $_POST['phone'] . "\n";
$messageBody .= "Message: " . $_POST['message'] . "\n";
$messageBody = "\nVisitor: " . $_POST["referralsname"] . "\n";
$messageBody .= "Email Address: " . $_POST['referralsemail'] . "\n";
$messageBody .= "Phone Number: " . $_POST['referralsphone'] . "\n";
$messageBody .= "Message: " . $_POST['referralsmessage'] . "\n";
However when I submit the form I onyl get one set of details (the first 4 $messageBody)
Any help anyone could offer would be greatly appreciated.
Thanks again
David
Change:
$messageBody = "\nVisitor: " . $_POST["referralsname"] . "\n";
To:
$messageBody .= "\nVisitor: " . $_POST["referralsname"] . "\n";
(.= instead of =, you are re-assigning value to $messageBody instead of chaining it to $messageBody)
if i had to guess, i would say it is because you are repeating the IDs. In HTML element IDs have to be unique. make all IDs match your "name" values. im referring to
<input name="phone" id="phone"....
<input name="referralsphone" id="phone"....
change all these cases to:
<input name="phone" id="phone"....
<input name="referralsphone" id="referralsphone"....
I would also suggest checking the contents of POST. this question shows how Echo an $_POST in PHP
I am seeking some more advice on the way to handle this.
I have got one page with links to each admin member which when clicked takes their display name over. On the second page which is a form it takes that display names and populates the subject field with their display name. I need to grab the email address that is associated to that user too but use that as the email address the form on the second page gets sent to as currently my script can only send it to an email address I hard code into it.
So page one is:
<?php
$args1 = array(
'role' => 'committee',
'orderby' => 'user_nicename',
'order' => 'ASC'
);
$committee = get_users($args1);
foreach ($committee as $user) {
echo '
<a href="../contact-form?displayname=' . $user->display_name . '"><b style="font-size:18px;">
<tr>
<td style="padding: 10px;">' .$user->job_title .' - </td>
<td style="padding: 10px;">' .$user->display_name .'</td>
</tr></b></a><br><br>';
}
?>
Page two is:
<?php $displayname = $_GET['displayname'];?>
<form role="form" method="post" action="../mailuser.php">
<div class="form-group">
<input type="hidden" name="displayname" value="<?php echo $displayname ?>">
<input type="text" name="hp" class="hp" value="" alt="if you fill this field out then your email will not be sent">
</div>
<div class="form-group">
<label for="InputName">Your name</label>
<input type="name" class="form-control" id="InputName" placeholder="Enter your name" name="username">
</div>
<div class="form-group">
<label for="InputEmail">Email address</label>
<input type="email" class="form-control" id="InputEmail" placeholder="you#example.com" name="emailFrom">
</div>
<div class="form-group">
<label for="InputMsg">Message</label>
<textarea class="form-control" rows="8" id="InputMsg" placeholder="Please begin typing your message..." name="emailMessage"></textarea>
</div>
<button type="submit" class="btn btn-primary pull-right">Send</button>
</form>
And my send script has my email hard coded in as:
$mail->From = 'myemail#dummy.com';
So I need that be variable depending on which person you clicked on in the first place. It needs to be sent to both my hard coded email and also the persons email that is associated to them in the Wordpress user database.
Based on our comment discussion, you should be able to do something along the lines of the following on page two. Be sure to correct my email_address, I'm not sure if that's how get_users returns the email address or not.
<?php
$displayname = $_GET['displayname'];
$args1 = array(
'role' => 'committee',
'orderby' => 'user_nicename',
'order' => 'ASC'
);
$committee = get_users($args1);
$matchingEmail = false;
foreach ($committee as $user) {
if ( !$matchingEmail && $user->display_name == $displayname ) {
// great, we found our match
$matchingEmail = $user->email_address; // I don't know if email_address is right, double check this and modify if necessary
}
}
if ( $matchingEmail ) {
// only proceed if a matching email address is found
?>
<form role="form" method="post" action="../mailuser.php">
<div class="form-group">
<input type="hidden" name="displayname" value="<?php echo $displayname; ?>">
<input type="hidden" name="matchingEmail" value="<?php echo $matchingEmail; ?>">
<input type="text" name="hp" class="hp" value="" alt="if you fill this field out then your email will not be sent">
</div>
<div class="form-group">
<label for="InputName">Your name</label>
<input type="name" class="form-control" id="InputName" placeholder="Enter your name" name="username">
</div>
<div class="form-group">
<label for="InputEmail">Email address</label>
<input type="email" class="form-control" id="InputEmail" placeholder="you#example.com" name="emailFrom">
</div>
<div class="form-group">
<label for="InputMsg">Message</label>
<textarea class="form-control" rows="8" id="InputMsg" placeholder="Please begin typing your message..." name="emailMessage"></textarea>
</div>
<button type="submit" class="btn btn-primary pull-right">Send</button>
</form>
<?php
} else {
?>
<p>Something is wrong, I can't find your email address! Please try again.</p>
<?php
}
?>
Finally on page three, where you send the email, you can do something like:
<?php $mail->addAddress(stripslashes( $_POST["matchingEmail"] ) ); ?>