I am trying to set the "to" address for an email sent via an html form utilizing phpmailer. The end goal is to utilize a droplist to let the user select which person they are contacting depending on their needs. However, as I don't know much about PHP I'm going step by step and am already stuck.
I'm using the example that came with the site I purchased, but trying to modify it to include a to field. Right now I'm simply trying to get a new variable to show up but have not succeeded. This is the HTML form I have in place right now:
<form name="contact" method="post" action="phpmailer.php" class="af-form" id="af-form">
<div class="af-outer af-required">
<div class="af-inner">
<label for="toemail" id="toemail_label">To Whom:</label>
<input type="text" name="toemail" id="toemail" size="30" value="" class="text-input span8"/>
<label class="error" for="toemail" id="toemail_error">To field is required.</label>
</div>
</div>
<div class="af-outer af-required">
<div class="af-inner">
<label for="name" id="name_label">Your Name:</label>
<input type="text" name="name" id="name" size="30" value="" class="text-input span8"/>
<label class="error" for="name" id="name_error">Name is required.</label>
</div>
</div>
<div class="af-outer af-required">
<div class="af-inner">
<label for="email" id="email_label">Your Email:</label>
<input type="text" name="email" id="email" size="30" value="" class="text-input span8"/>
<label class="error" for="email" id="email_error">Email is required.</label>
</div>
</div>
<div class="af-outer af-required">
<div class="af-inner">
<label for="input-message" id="message_label">Your Message:</label>
<textarea name="message" id="input-message" cols="30" class="text-input span8"></textarea>
<label class="error" for="input-message" id="message_error">Message is required.</label>
</div>
</div>
<div class="af-outer af-required">
<div class="af-inner">
<input type="submit" name="submit" class="form-button btn btn-large" id="submit_btn" value="Send Message!" />
</div>
</div>
The name, email, and message fields get picked up fine and populate in the email I recieve. However, the "toemail" field doesn't show up. Here is the process.php I am using for phpmailer.
<?php
if ((isset($_POST['toemail'])) && (strlen(trim($_POST['toemail'])) > 0)) {
$toemail = stripslashes(strip_tags($_POST['toemail']));
} else {$toemail = 'No to address entered';}
if ((isset($_POST['name'])) && (strlen(trim($_POST['name'])) > 0)) {
$name = stripslashes(strip_tags($_POST['name']));
} else {$name = 'No name entered';}
if ((isset($_POST['email'])) && (strlen(trim($_POST['email'])) > 0)) {
$email = stripslashes(strip_tags($_POST['email']));
} else {$email = 'No email entered';}
if ((isset($_POST['message'])) && (strlen(trim($_POST['message'])) > 0)) {
$message = stripslashes(strip_tags($_POST['message']));
} else {$message = 'No phone entered';}
ob_start();d
?>
<html>
<head>
<style type="text/css">
</style>
</head>
<body>
<table width="550" border="1" cellspacing="2" cellpadding="2">
<tr bgcolor="#eeffee">
<td>To Whom:</td>
<td><?=$toemail;?></td>
</tr>
<tr bgcolor="#eeffee">
<td>Name</td>
<td><?=$name;?></td>
</tr>
<tr bgcolor="#eeeeff">
<td>Email</td>
<td><?=$email;?></td>
</tr>
<tr bgcolor="#eeffee">
<td>Message</td>
<td><?=$message;?></td>
</tr>
</table>
</body>
</html>
<?
$body = ob_get_contents();
$to = 'personalemailtousefortesting.com';
$email = 'email#example.com';
$fromaddress = "you#example.com";
$fromname = "Online Contact";
require("phpmailer.php");
$mail = new PHPMailer();
$mail->From = "mail#yourdomain.com";
$mail->FromName = "Contact Form";
$mail->AddAddress("mypersonalemailfortestingpurposes#gmail.com","Name 1"); // addresses here
$mail->WordWrap = 50;
$mail->IsHTML(true);
$mail->Subject = "Contact form submitted";
$mail->Body = $body;
$mail->AltBody = "This is the text-only body";
if(!$mail->Send()) {
$recipient = 'mypersonalemailfortestingpurposes#gmail.com';
$subject = 'Contact form failed';
$content = $body;
mail($recipient, $subject, $content, "From: mail#yourdomain.com\r\nReply-To: $email\r\nX-Mailer: DT_formmail");
exit;
}
?>
The email I get populates the name, email, and message fields fine, however, I get my fall back error message of 'No to address entered' for the "toemail" input.
This is issue number one. I'll post the phpmailer.php that I'm using if anyone needs to see it. I do not know if I need to define the variable in there first.
After I get that working, I need to make it work for a dropdown list so the user cannot just send the email to whomever they would like.
THEN, I need to set that as the actual send-to address. I do not know how to set the actual address for sending purposes. I would assume that I change
$mail->AddAddress("mypersonalemailfortestingpurposes#gmail.com","Name 1"); // addresses here
to something like...
$mail->AddAddress($toemail,"Name 1"); // addresses here
...but odds are that's not correct.
Any help you can provide would be wonderful.
If you need to see my phpmailer.php I can post that too.
Thanks again,
-Rocky Colt TumTum
Just incase you didn't find this, this is what was listed on the PHP Manual site.
<?php
// multiple recipients
$to = 'aidan#example.com' . ', '; // note the comma
$to .= 'wez#example.com';
// subject
$subject = 'Birthday Reminders for August';
// message
$message = '
<html>
<head>
<title>Birthday Reminders for August</title>
</head>
<body>
<p>Here are the birthdays upcoming in August!</p>
<table>
<tr>
<th>Person</th><th>Day</th><th>Month</th><th>Year</th>
</tr>
<tr>
<td>Joe</td><td>3rd</td><td>August</td><td>1970</td>
</tr>
<tr>
<td>Sally</td><td>17th</td><td>August</td><td>1973</td>
</tr>
</table>
</body>
</html>
';
// To send HTML mail, the Content-type header must be set
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
// Additional headers
$headers .= 'To: Mary <mary#example.com>, Kelly <kelly#example.com>' . "\r\n";
$headers .= 'From: Birthday Reminder <birthday#example.com>' . "\r\n";
$headers .= 'Cc: birthdayarchive#example.com' . "\r\n";
$headers .= 'Bcc: birthdaycheck#example.com' . "\r\n";
// Mail it
mail($to, $subject, $message, $headers);
?>
Related
How to install phpMailer in a shared hosting environment?
Seen this How to install phpMailer in a shared hosting environment? but I don't understand what does "include the main file with this line:" means and the next part too "After that, you will need an external SMTP account, like gmail.com for example. Here is a working example of PHPMailer with GMAIL:"
Thanks in advance
Athlios
This is the send_form.php
<?php
$formid = $_POST['contactform'];
$email_to = "info#a-wd.eu";
$fullname = $_POST['fullname']; // required
$email_from = $_POST['email']; // required
$subject = $_POST['subject']; // required
//$subjectselect = $_POST('subject').value();
$message = $_POST['message']; // required
echo($email_from);
$email_message = "Submission details below.\n\n";
$email_message .= "Fullname: ".clean_string($fullname)."\n";
$email_message .= "Email: ".clean_string($email_from)."\n";
$email_message .= "Whats this about: ".clean_string($subject)."\n";
$email_message .= "Message: ".clean_string($message)."\n";
$email_message = wordwrap($email_message, 70, "\r\n");
$headers = 'From: '.$email_from."\r\n".
'Reply-To: '.$email_from."\r\n" .
'X-Mailer: PHP/' . phpversion();
mail($email_to, $email_subject, $email_message,$headers) or die("Error!");
echo "Thank you for contacting us. We will be in touch with you very soon. \r\n";
?>
This is the page
<div class="container-fluid">
<div class="row">
<div class="col-12">
<div class="container contact-form-container">
<div class="row">
<div class="col-md-12">
<form action="send_form.php" id="contactform" name="contactform" method="post" data-parsley-validate>
<div class="row">
<!-- <span class="required-key">Fields marked with a <span class="label-required">*</span> are required.</span> -->
<div class="form-group col-lg-12 col-12">
<label for="fullname">Name <span>(Required)</span>:</label>
<input type="text" name="fullname" data-parsley-trigger="focusin focusout" required data-parsley-required="true" data-parsley-errors-messages-disabled />
</div>
<div class="form-group col-lg-12 col-12">
<label for="email">Email <span>(Required)</span>:</label>
<input type="email" data-parsley-type="email" name="email" data-parsley-trigger="focusin focusout" required data-parsley-required="true" data-parsley-errors-messages-disabled />
</div>
<div class="form-group col-lg-12 col-12">
<label for="subject">Subject <span>(Required)</span>:</label>
<select name="subject" required data-parsley-required="true" data-parsley-errors-messages-disabled>
<option value="question">General Question</option>
<option value="quote">Request a Quote</option>
<option value="sponsorship">Sponsorship</option>
<option value="other">Other</option>
</select>
</div>
<div class="form-group col-lg-12 col-12">
<label for="message">Message <span>(Required)</span>:</label>
<textarea name="message" data-parsley-trigger="focusin focusout" data-parsley-minlength="20" data-parsley-maxlength="1000" data-parsley-validation-threshold="10" data-parsley-minlength-message="Minimum message length is 20 characters" data-parsley-maxlength-message="Maximum message length is 1000 characters" data-parsley-required="true"></textarea>
</div>
<div class="form-group col-lg-12 col-12">
<label for="message">Captcha <span>(Required)</span>:</label>
<script src='https://www.google.com/recaptcha/api.js'></script>
<div class="g-recaptcha" data-sitekey="6LeAAUkUAAAAAJeW7fjroLKFkYtETHvXGgflK49u"></div>
</div>
<div class="form-group col-lg-12 col-12">
<button class="btn-contact" name="send" type="submit">Send Message <i class="fa fa-paper-plane" aria-hidden="true"></i></button>
</div>
</form>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
Go to the PHPMailer github, click the green "Clone or Download" button, and click "Download ZIP". On your local computer, unarchive the ZIP file and upload the PHPMailer-master folder to your shared server's public_html directory.
Now, wherever you want to use PHPMailer, include the parts you need:
<?php
require '/path/to/PHPMailer-master/src/PHPMailer.php'; // Only file you REALLY need
require '/path/to/PHPMailer-master/src/Exception.php'; // If you want to debug
?>
You don't need an external SMTP account for this to work, as noted in the PHPMailer documentation. So in a script where you're sending out an email, your code should look something like this:
<?php
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;
require 'PHPMailer-master/src/PHPMailer.php'; // Only file you REALLY need
require 'PHPMailer-master/src/Exception.php'; // If you want to debug
$mail = new PHPMailer(true); // Passing `true` enables exceptions
try {
//Recipients
$mail->setFrom('from#example.com', 'Mailer');
$mail->addAddress('joe#example.net', 'Joe User'); // Add a recipient
$mail->addAddress('ellen#example.com'); // Name is optional
$mail->addReplyTo('info#example.com', 'Information');
$mail->addCC('cc#example.com');
$mail->addBCC('bcc#example.com');
//Attachments
$mail->addAttachment('/var/tmp/file.tar.gz'); // Add attachments
$mail->addAttachment('/tmp/image.jpg', 'new.jpg'); // Optional name
//Content
$mail->isHTML(true); // Set email format to HTML
$mail->Subject = 'Here is the subject';
$mail->Body = 'This is the HTML message body <b>in bold!</b>';
$mail->AltBody = 'This is the body in plain text for non-HTML mail clients';
$mail->send();
echo 'Message has been sent';
} catch (Exception $e) {
echo 'Message could not be sent. Mailer Error: ', $mail->ErrorInfo;
}
?>
This should be all you need to get started using PHPMailer on a shared server. Check out the project's README and look at some examples for a better understanding of all the awesomeness you get with this library.
Update for OP's code
Put the include code at the top of send_post.php:
<?php
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;
require 'PHPMailer-master/src/PHPMailer.php'; // Only file you REALLY need
require 'PHPMailer-master/src/Exception.php'; // If you want to debug
// Form details
$formid = $_POST['contactform'];
$email_to = "info#a-wd.eu";
$fullname = $_POST['fullname']; // required
$email_from = $_POST['email']; // required
$subject = $_POST['subject']; // required
$message = $_POST['message']; // required
$email_message = "Submission details below.\n\n";
$email_message .= "Fullname: ".clean_string($fullname)."\n";
$email_message .= "Email: ".clean_string($email_from)."\n";
$email_message .= "Whats this about: ".clean_string($subject)."\n";
$email_message .= "Message: ".clean_string($message)."\n";
$email_message = wordwrap($email_message, 70, "\r\n");
// No need to set headers here
// Replace the mail() function with PHPMailer
$mail = new PHPMailer(true); // Passing `true` enables exceptions
try {
//Recipients
$mail->setFrom($email_from, 'From Name');
$mail->addAddress($email_to, $fullname); // Add the recipient
//Content
$mail->isHTML(true); // Set email format to HTML
$mail->Subject = $subject;
$mail->Body = $email_message;
$mail->send();
echo "Thank you for contacting us. We will be in touch with you very soon. \r\n";
} catch (Exception $e) {
echo 'Message could not be sent. Mailer Error: ', $mail->ErrorInfo;
}
in pop up box, we are displaying login form. once user enter wrong email id or password, we are displaying message : Invalid login/pw , but once they enter correct details and click on login button, than user will be logged in.
but issue is pop up box will remain until we close the pop up manually with help of close button as in below image, but i want to hide the pop up box if login is successfull.
enter image description here
Once we click on login, under Response tab, if there is no message, than i want to hide the pop up, if there is message as success":false,"error":"Invalid login or password." , than i don't want to hide the pop up
enter image description here
I tried below code :
Html
<form>
//code for email and pw
<button onclick = "hideWindow()">Login</button>
</form>
Ajax
function hideWindow()
{
var username=jQuery( "#customusername" ).val();
var password=jQuery( "#custompassword" ).val();
url="";
new Ajax.Request(url, {
method: 'POST',
onFailure: function(response){
},
parameters: {
username: username,
password:password
},
onSuccess: function(response)
{
if(response.responseText=="")
{
//trigger to close popup
document.getElementById('something').style.display = 'none';
document.getElementById('ajaxlogin-mask').style.display = 'none';
}
}
});
}
edit
<div class="ajaxlogin-window" id="something">
<form>
<div class="content">
<ul class="form-list">
<li>
<input type="hidden" id="likeproduct_id" name="product_id" value=""/>
<label for="email" class="required"><em>*</em>Email</label>
<div class="input-box">
<input type="text" name="login[username]" value="<?php echo $this->htmlEscape($this->getUsername()) ?>" id="email" title="<?php echo $this->__('Email Address') ?>" />
</div>
</li>
<li>
<label for="pass" class="required"><em>*</em><?php echo $this->__('Password') ?></label>
<div class="input-box">
<input type="password" name="login[password]" id="pass" title="<?php echo $this->__('Password') ?>" />
</div>
</li>
<?php echo $this->getChildHtml('form.additional.info'); ?>
</ul>
</div>
<div class="buttons-set">
<button onclick = "hideWindow()" type="submit" class="button" title="<?php echo $this->__('Login') ?>" name="send" id="send2" ><span><span><?php echo $this->__('Login') ?></span></span></button>
</div>
</div>
As I can see there is some thing wrong with your ajax function try this out:
onSuccess: function(response)
{
document.getElementById('something').style.display = 'none';
document.getElementById('ajaxlogin-mask').style.display = 'none';
}
function hideWindow()
{
var db_username = 'ankit';
var db_password = 'password';
var username=jQuery( "#customusername" ).val();
var password=jQuery( "#custompassword" ).val();
if(username = db_username && password == db_password) {
jQuery('#something').hide();
}
else{
jQuery('#something').prepend('Wrong Cred ');
}
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="ajaxlogin-window" id="something">
<form>
<div class="content">
<ul class="form-list">
<li>
<input type="hidden" id="likeproduct_id" name="product_id" value=""/>
<label for="email" class="required"><em>*</em>Email</label>
<div class="input-box">
<input type="text" name="login[username]" value="" id="email" title="Email Address" />
</div>
</li>
<li>
<label for="pass" class="required"><em>*</em>Password</label>
<div class="input-box">
<input type="password" name="login[password]" id="pass" title="Password" />
</div>
</li>
</ul>
</div>
<div class="buttons-set">
<button onclick = "hideWindow()" type="submit" class="button" title="Login" name="send" id="send2" ><span><span>Login</span></span></button>
</div>
Please check the code instead of ajax call i have just added a if condition.
Hope it might help you
Thanks
i been stuck at this problem for a long time. I have a page with list of student and student's score. I want to add a button to upload xls/xlsx file at the current page, and after i uploaded the file, the column data fill the student's score input. How can i do that? Please i need help on this.
View
<form action="<?php echo base_url()?>index.php/nilai/ubahNilaiItem/<?php echo $idKelas ?>/<?php echo $id ?>" method="post" class="form-horizontal">
<button name="tombol" type="submit" value="import" class="btn pull-right btn-primary">Import</button>
<input type="file" name="inputfile" size="20" class="pull-right"/>
</div>
<div class="col-lg-12">
<table class="table">
<thead>
<tr>
<th class="col-md-1 active">No</th>
<th class="col-md-2 active">NIM</th>
<th class="col-md-5 active">Nama</th>
<th class="col-md-1 active">Nilai</th>
</tr>
</thead>
<tbody>
<?php
$no = 0;
foreach($input as $i){
$no++
?>
<tr>
<td><?php echo $no ?></td>
<td><?php echo $i->nim ?></td>
<td><?php echo $i->nama ?></td>
<td>
<div class="input-group">
<input hidden value="<?php echo $i->id ?>" name="id[]">
<input class="text-center form-control" value="<?php echo $i->angka_nilai ?>" name="angka[]"> //this is for inputing student score
<span class="input-group-addon"><i class="glyphicon glyphicon-question-sign" data-toggle="tooltip" title="Gunakan koma (,) untuk nilai desimal"></i></span>
</div>
</td>
</tr>
<?php } ?>
</tbody>
</table>
<button name="tombol" value="simpan" type="submit" class="btn pull-right btn btn-primary">Simpan</button>
</form>
Controller for the view
public function itemView($idKelas,$id){
//load model
$this->load->model('nilais');
//take data from database
$data2['input'] = $this->nilais->input_item($idKelas,$id)->result();
$data3['detail'] = $this->nilais->tampil_detail($idKelas)->result();
//merge array
$data4 = array_merge($data2, $data3);
$data4['item'] = $this->nilais->item_detail($id)->result();
$data4['idKelas'] = $idKelas;
$data4['id'] = $id;
//take data for header and notification
$data6['notifikasi'] = $this->nilais->notifikasi($this->session->userdata('id_user'))->result();
$data6['count'] = count($data6['notifikasi']);
$data6['matakuliah'] = $idKelas;
$data6['active'] = 'nilai';
//load view nilai and header
$this->load->view('nilai/item_view',$data4);
$this->load->view('header',$data6);
}
Update
Now i used PHPExcel, but i cant get the null cell. How can i get all cell even if its null?
Controller for excel
public function importAction($idKelas,$id){
//load model
$this->load->model('nilais');
$this->load->library('excel');
$config['upload_path'] = './file/excel/';
$config['allowed_types'] = 'xls|xlsx';
$config['max_size'] = 0;
$this->load->library('upload', $config);
if ($this->upload->do_upload('inputfile')) {
$upload_data = $this->upload->data();
$objPHPExcel = PHPExcel_IOFactory::load($upload_data['full_path']);
$cell_collection = $objPHPExcel->getActiveSheet()->getCellCollection();
foreach ($cell_collection as $cell) {
$column = $objPHPExcel->getActiveSheet()->getCell($cell)->getColumn();
$row = $objPHPExcel->getActiveSheet()->getCell($cell)->getRow();
$data_value = $objPHPExcel->getActiveSheet()->getCell($cell)->getValue();
//header will/should be in row 1 only. of course this can be modified to suit your need.
if ($row == 1) {
$header[$row][$column] = $data_value;
} else {
$arr_data[$row][$column] = $data_value;
}
}
//send the data in an array format
$data['header'] = $header;
$data['values'] = $arr_data;
}
}
Hope some PHP gurus can help me out with this. I am creating a simple donation form to accept payments using Stripe. I have included my code here. The form works fine in terms of processing payments. However, the script is supposed to send a confirmation email to the donor, saying that their donation has been received, etc. The issue I am having is that the script does not send such a confirmation. I am using GoDaddy hosting and have checked that my MX records are correct. I ran test from my hosting account (with help of GoDaddy support) as well to see if there's any issue with the account itself. The tests showed no such issues. I have tried minor tweaks such as changing headers, adding <>, etc., but no luck.
Here's my code for config.php
<?php
return array(
'test-mode' => true,
'secret-key' => 'YOUR_SECRET_KEY',
'publishable-key' => 'YOUR_PUBLISHABLE_KEY',
'thank-you' => 'http://example.com/thank-you.html',
'email-from' => 'no-reply#example.com',
'email-bcc' => 'admin#example.com',
'email-subject' => 'Thank you for your donation!',
'email-message' => "Dear %name%,\n\nThank you for your order of %amount%."
);
Here's my code for index.php
<?php
require('lib/Stripe.php');
$config = require('config.php');
if ($config['test-mode'] && $_SERVER['HTTPS'] != 'on') {
header('HTTP/1.1 301 Moved Permanently');
header('Location: https://' . $_SERVER["SERVER_NAME"] . $_SERVER["REQUEST_URI"]);
exit;
}
if ($_POST) {
Stripe::setApiKey($config['secret-key']);
$token = $_POST['stripeToken'];
$first_name = $_POST['first-name'];
$last_name = $_POST['last-name'];
$name = $first_name . ' ' . $last_name;
$address = $_POST['address'] . "\n" . $_POST['city'] . ', ' . $_POST['state'] . ' ' . $_POST['zip'];
$email = $_POST['email'];
$phone = $_POST['phone'];
$amount = (float) $_POST['amount'];
try {
if ( ! isset($_POST['stripeToken']) ) {
throw new Exception("The Stripe Token was not generated correctly");
}
// Charge the card
$donation = Stripe_Charge::create(array(
'card' => $token,
'description' => 'Donation by ' . $name . ' (' . $email . ')',
'amount' => $amount * 100,
'currency' => 'usd'
));
// Build and send the email
$headers = 'From: ' . $config['emaily-from'];
$headers .= "\r\nBcc: " . $config['emaily-bcc'] . "\r\n\r\n";
// Find and replace values
$find = array('%name%', '%amount%');
$replace = array($name, '$' . $amount);
$message = str_replace($find, $replace , $config['email-message']) . "\n\n";
$message .= 'Amount: $' . $amount . "\n";
$message .= 'Address: ' . $address . "\n";
$message .= 'Phone: ' . $phone . "\n";
$message .= 'Email: ' . $email . "\n";
$message .= 'Date: ' . date('M j, Y, g:ia', $donation['created']) . "\n";
$message .= 'Transaction ID: ' . $donation['id'] . "\n\n\n";
$subject = $config['email-subject'];
// Send it
if ( !$config['test-mode'] ) {
mail($email,$subject,$message,$headers);
}
// Forward to "Thank You" page
header('Location: ' . $config['thank-you']);
exit;
} catch (Exception $e) {
$error = $e->getMessage();
}
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Stripe Payment Form</title>
<link rel="stylesheet" type="text/css" href="style.css" media="all">
<script type="text/javascript" src="https://js.stripe.com/v2"></script>
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
<script type="text/javascript">
Stripe.setPublishableKey('<?php echo $config['publishable-key'] ?>');
</script>
<script type="text/javascript" src="script.js"></script>
</head>
<body>
<div class="wrapper">
<h1>Stripe Payment Form</h1>
<p><strong>Thanks for donating!</strong></p>
<div class="messages">
<!-- Error messages go here go here -->
</div>
<form action="#" method="POST" class="donation-form">
<fieldset>
<legend>
Contact Information
</legend>
<div class="form-row form-first-name">
<label>First Name</label>
<input type="text" name="first-name" class="first-name text">
</div>
<div class="form-row form-last-name">
<label>Last Name</label>
<input type="text" name="last-name" class="last-name text">
</div>
<div class="form-row form-email">
<label>Email</label>
<input type="text" name="email" class="email text">
</div>
<div class="form-row form-phone">
<label>Phone</label>
<input type="text" name="phone" class="phone text">
</div>
<div class="form-row form-address">
<label>Address</label>
<textarea name="address" cols="30" rows="2" class="address text"></textarea>
</div>
<div class="form-row form-city">
<label>City</label>
<input type="text" name="city" class="city text">
</div>
<div class="form-row form-state">
<label>State</label>
<select name="state" class="state text">
<option value="AL">AL</option>
<option value="WY">WY</option>
</select>
</div>
<div class="form-row form-zip">
<label>Zip</label>
<input type="text" name="zip" class="zip text">
</div>
</fieldset>
<fieldset>
<legend>
Your Generous Donation
</legend>
<div class="form-row form-amount">
<label><input type="radio" name="amount" class="set- amount" value="25"> $25</label>
<label><input type="radio" name="amount" class="set-amount" value="100"> $100</label>
<label><input type="radio" name="amount" class="other-amount" value="0"> Other:</label> <input type="text" class="amount text" disabled>
</div>
<div class="form-row form-number">
<label>Card Number</label>
<input type="text" autocomplete="off" class="card-number text" value="4242424242424242">
</div>
<div class="form-row form-cvc">
<label>CVC</label>
<input type="text" autocomplete="off" class="card-cvc text" value="123">
</div>
<div class="form-row form-expiry">
<label>Expiration Date</label>
<select class="card-expiry-month text">
<option value="01" selected>January</option>
<option value="12">December</option>
</select>
<select class="card-expiry-year text">
<option value="2017" selected>2017</option>
<option value="2018">2018</option>
<option value="2019">2019</option>
</select>
</div>
<div class="form-row form-submit">
<input type="submit" class="submit-button" value="Submit Donation">
</div>
</fieldset>
</form>
</div>
<script>if (window.Stripe) $('.donation-form').show()</script>
<noscript><p>JavaScript is required for the donation form.</p></noscript>
</body>
</html>
Any help is greatly appreciated.
i am doing an invoice project using node.js in which i have to store multiple products in a string and my code is
the schema file
var mongoose = require('mongoose');
var autoIncrement = require('mongoose-auto-increment');
var invoiceSchema = mongoose.Schema({
date: String,
documentNo:Number,
vendorName:String,
pno:Number,
productName:String,
description:String,
quantity:Number,
costPrice :Number,
mrp:Number,
invoiceNo:Number,
advance:Number,
due:Number,
discount:Number,
Tax : String,
netTotal:Number,
size:String,
colour:String,
grandTotal:Number
},{collection:'invoice-data'});
var invoice = module.exports = mongoose.model('invoice',invoiceSchema);
now for routing
var product = require('../models/product');
var invoice= require('../models/invoice.js');
exports.addaInvoice=function(req, res) {
var date=req.body.date;
var invoiceNo=req.body.invoiceNo;
var vendorName = req.body.vname;
var pno = req.body.orderNo;
var productName = getElementById(req.body.productname);
var description = req.body.desc;
var colour = req.body.colour;
var size = req.body.size;
var quantity = req.body.quantity;
var costPrice = req.body.costPrice;
var mrp = req.body.mrp;
var amount=req.body.amount;
var discount=req.body.discount;
var discountvalue=req.body.discountvalue;
var netamount=req.body.netamount;
var advance = req.body.advance;
var due = req.body.due;
var Tax = req.body.Tax;
var netTotal = req.body.netTotal;
var subTotal = req.body.subTotal;
var grandTotal = req.body.grandTotal;
var newInvoice = new invoice({
date: date,
invoiceNo:invoiceNo,
vendorName:vendorName,
pno:pno,
productName:productName,
description:description,
colour:colour,
size:size,
quantity:quantity,
costPrice :costPrice,
amount:amount,
discount:discount,
discountvalue:discountvalue,
netamount:netamount,
mrp:mrp,
advance:advance,
due:due,
Tax :Tax,
netTotal:netTotal,
subTotal : subTotal,
grandTotal : grandTotal
});
newInvoice.save();
req.flash('success_msg',' purchase invoice added ')
res.redirect('/users/invoicedetails');
}
html code
<head>
<script type="text/javascript">
function calculate()
{
var quantity = document.getElementById('quantity');
var costprice = document.getElementById('costprice');
//var amount = document.getElementById('amount');
var result=quantity.VA*costprice.value;
//amount.value = result;
$("#amount").val(result);
}</script>
<SCRIPT language="javascript">
function addRow(tableID) {
var table = document.getElementById(tableID);
var rowCount = table.rows.length;
var row = table.insertRow(rowCount);
var colCount = table.rows[0].cells.length;
for(var i=0; i<colCount; i++) {
var newcell = row.insertCell(i);
newcell.innerHTML = table.rows[0].cells[i].innerHTML;
//alert(newcell.childNodes);
switch(newcell.childNodes[0].type) {
case "text":
newcell.childNodes[0].value = "";
break;
case "checkbox":
newcell.childNodes[0].checked = false;
break;
case "select-one":
newcell.childNodes[0].selectedIndex = 0;
break;
}
}
}
</SCRIPT>
</head>
<div class="form-group">
<label class="col-md-4 control-label" for="date">date</label>
<div class="col-md-8">
<input id="date" name="date" placeholder="date" class="form-control input-md" type="date">
</div>
</div>
<!-- Text input-->
<div class="form-group">
<label class="col-md-4 control-label" for="invoiceNo">invoiceNo</label>
<div class="col-md-8">
<input id="invoiceNo" name="invoiceNo" placeholder="invoiceNo" class="form-control input-md" type="text">
</div>
</div>
<!-- Select Basic -->
<div class="form-group">
<label class="col-md-4 control-label" for="vendorname">Vendor name</label>
<div class="col-md-8">
<select id="vendorName" name="vendorName" class="form-control">
{{# each items }}
<option> {{ this.vendorName}}</option>
{{/each}}
</select>
</div>
</div>
</td>
</tr>
</table>
<INPUT type="button" value="Add Product " onclick="addRow('dataTable')" />
<table class="table table-hover" >
<thead>
<tr>
<td>productno</td>
<td>productName</td>
<td>size</td>
<td>colour</td>
<td>quantity</td>
<td>costprice</td>
<td>MRP</td>
<td>amount</td>
<td>discount%</td>
<td>discount value</td>
<td>net amount</td>
</tr>
</thead>
<TABLE id="dataTable" class="table table-hover" >
<TR>
<td>
<input id="orderNo" name="orderNo" placeholder="productno" class="form-control input-md" required="" type="text">
</td>
{{# each items }}
<option value="{{ this.productid}}"> {{ this.productname}}</option>
{{/each}}
</select>
</td>
<td>
<select id="size" name="size" class="form-control-md">
{{# each items }}
<option > {{ this.size}}</option>
{{/each}}
</select>
<!-- Text input-->
</td>
<td>
<select id="colour" name="colour" class="form-control-md">
{{# each items }}
<option > {{ this.colour}}</option>
{{/each}}
</select>
</td>
<td>
<input id="quantity" name="quantity" placeholder="Quantity" class="form-control " type="text">
</td>
<td>
<input id="costPrice" name="costPrice" placeholder="costPrice" class="form-control" type="text" onChange="calculate()">
</td>
<!-- Text input-->
<td>
<input id="mrp" name="mrp" placeholder="mrp" class="form-control" type="text">
</td>
<td>
<td>
<input id="amount" name="amount" placeholder="amount" class="form-control" >
</td>
<td>
<input id="discount" name="discount" placeholder="discount%" class="form-control"type="text">
</td>
<td>
<input id="discountvalue" name="discountvalue" placeholder="discountvalue" class="form-control"type="text">
</td>
<td>
<input id="netamount" name="netamount" placeholder="netamount" class="form-control"type="text">
</td>
</tr>
</tbody>
</table>
<table>
<!-- Text input-->
<td>
<label class="col-md-8 control-label" for="advance">Advance</label>
<div class="col-md-4">
<input id="advance" name="advance" placeholder="Advance" class="form-control input-md" type="text">
</div>
<div class="form-group">
<label class="col-md-8 control-label" for="Tax">Tax</label>
<div class="col-md-4">
<select id="Tax" name="Tax" class="form-control">
<option value="14.50">14.10</option>
<option value="12.50">12.50</option>
</select>
</div>
</div>
</td>
</table>
</table>
<label class="col-md-8 control-label" for="netTotal">netTotal</label>
<div class="col-md-4">
<input id="netTotal" name="netTotal" placeholder="netTotal" class="form-control input-md" type="text">
</div>
<label class="col-md-8 control-label" for="subTotal">SubTotal</label>
<div class="col-md-4">
<input id="subTotal" name="subTotal" placeholder="subTotal" class="form-control input-md" type="text">
<label class="col-md-8 control-label" for="grandTotal">grandTotal</label>
<div class="col-md-4">
<input id="grandTotal" name="grandTotal" placeholder="grandTotal" class="form-control input-md" type="text">
<label class="col-md-8 control-label" for="due">due</label>
<div class="col-md-4" align="left">
<input id="due" name="due" placeholder="due" class="form-control input-md" type="text">
</div>
<!-- Button -->
<div class="form-group" align="bottom">
<label class="col-md-4 control-label" for="vadd"></label>
<div class="col-md-4">
<button id="iadd" name="iadd" class="btn btn-primary">ADD</button>
</div>
</div>
</fieldset>
</form>
</div>
</div>
this is my html code in it the script is for cloning of the table for addingg a row in the tablle for multiple product entry
now this will work for one product if i have 2 or more products
how can i save these three products in a same document as like purchase invoice
If you are using bodyParser middleware, then you can pass an array of values from the client-side.
On client-side you might need to name your input elements as follows
<input name="products[#{1}][productname]" type="text">
<input name="products[#{1}][description]" type="text">
<input name="products[#{2}][productname]" type="text">
<input name="products[#{2}][description]" type="text">
and so on
This way your request body will have
{
"products": {
"1": {
"product name": "name goes here ",
"description": "desc goes here "
},
"2": {
"product name": "name goes here",
"description": "desc goes here"
}
}
}
You can iterate through that and get the values in your router as
var products = Array.prototype.slice.call(request.body.products);
for (var i = 0; i < messages.length; i++) {
var newInvoice = new invoice({
productName: products[i].productname,
description: products[i].description
});
newInvoice.save();
});
First you need to change your invoice schema to add multiple products in it. You need to add a array of product sub documents, I have added a sample schema below
// Product Schema
var ProductSchema = mongoose.Schema({
pno:Number,
productName:String,
description:String,
quantity:Number,
costPrice :Number,
mrp:Number,
size:String,
colour:String,
});
var invoiceSchema = mongoose.Schema({
date: String,
documentNo:Number,
vendorName:String,
products: [ProductSchema],
invoiceNo:Number,
advance:Number,
due:Number,
discount:Number,
Tax : String,
netTotal:Number,
grandTotal:Number
},{collection:'invoice-data'});
I haven't checked this code syntactically, I am just giving you an idea how to create schema if you want to store multiple sub documents in a single document.
And in your node.js code also, you need to save an array of products
var productArr = [];
var temProduct = {};
temProduct.pno= pno;
temProduct.productName= productName;
temProduct.description= description;
temProduct.colour= colour;
temProduct.size= size;
temProduct.quantity= quantity;
temProduct.costPrice = costPrice;
temProduct.amount = amount;
temProduct. = mrp;
productArr.push(temProduct);
var newInvoice = new invoice({
date: date,
invoiceNo:invoiceNo,
vendorName:vendorName,
products:productArr,
advance:advance,
due:due,
Tax :Tax,
netTotal:netTotal,
subTotal : subTotal,
grandTotal : grandTotal
});
newInvoice.save(function(err, new_data) {
if(err) {
// Handle error here, if any come
} else {
req.flash('success_msg',' purchase invoice added ')
res.redirect('/users/invoicedetails');
}
});
You can push as mush as products you want in productArr, then add it in invoice object and then save it.
I am mentioning again, that above code is not good to run directly, you need to do few changes, but it will give you an idea how to do it.