I'm using a javascript shopping cart on a store, and I want to send an order confirmation on checkout. The problem is that the cart isn't saved in database of any kind, and is printed with javascript. How would I attach it to the email? I've included the shopping cart script on the page that sends the mail.
<table class="simpleCart_items"></table> would print the cart, but how would I attach the printed cart to email?
Hidden input or something?
UPDATE
My ajax call looks like this:
var data = $('#yhteystiedot').serialize();
data.cartContent = $('.simpleCart_items').html();
//alert (dataString);return false;
$.ajax({
type: "POST",
url: "order.php",
data: data,
dataType: "text",
error: function(){ alert("Jotakin meni pahasti pieleen! Yritä uudelleen?");
},
success: function() {
$(document).html("Tilaus lähti.");
}
});
You can make an ajax call to a php function that sends an email. The argument is the content generated by javascript.
You'll need to post the cart values to serverside PHP script and recreate the HTML for the cart in order to be able to send it through email. You can do direct form post or ajax post based on your need.
I asume your $.ajax() call looks something like this:
$('form').submit(function(){
var dataTrunk = $(this).serializeArray();
dataTrunk.push( { name: 'cartContent', value: $(your_table_selector).html()});
$.ajax({
url: 'mail.php', // your mail script
data: dataTrunk,
type: 'post'
});
return false;
});
In php you would trap $_POST['cartContent'] and render it in email and send it.
If you are sending email with html and plain text body, then it would probably be a good idea to strip html elements and replace them with chars that are compatible with plain text.
// edited: I've fixed the error
Related
Hello I am new and building an application in Flask and Javascript. I have a problem with sending data from Flask do JavaScript.
I have code in routes.py
#app.route('/mapaa',methods=['GET','POST'])
def mapa():
user_id = current_user.get_id()
slownik = {}
if 'event_form' in request.form:
name = request.form['name_event']
all_data = Event.query.filter_by(name=name).all()
for row in all_data:
date_st_string = str(row.date_start)
date_end_string = str(row.date_end)
slownik = {'id':row.id,'date_st':date_st_string,'date_end':date_end_string,'type':row.type,'name':row.name,'len_route':row.len_route,'route':row.route}
return jsonify(slownik)
return render_template('mapaa.html', title='Mapa')
I want to send jsonify(slownik) to my code in JavaScript but I dont want to do it with render_template bacause it refresh a page and I need this data to display it on the map using JavaScript. I tried return jsonify(slownik) but it return me the data on new page. How I can send the data when the user click event_form button to JavaScript without refreshing page?
You can use ajax for sending post request without refreshing the page.
Note- include jquery before this
<script src="https://code.jquery.com/jquery-3.1.0.js" ></script>
javascript code
$("#id_of_btn").click(function () { // make ajax request on btn click
$.ajax({
type: "POST",
url: "/mapaa", // url to the function
data: {
name: $("#id_of_input_tag").val(), // value of the form
},
success: function (response) {
console.log(response); // response contains the json
},
});
});
make sure you don't use form tag.
Edit:
If you want to send more data via forms, you could use
data: $('form').serialize()
this will take the name attribute of input tag and add it in the request.data
So if you have this
<input type="text" id="element_id" name="username" >
You can use the value of name attribute like this request.data['username']
follow this tutorial
I want to send GET values to another script so that the script takes those GET values and create a PDF as per it and sends to the user email. I just need how can I send GET values in URL using JavaScript without redirecting to the URL you send GET values with.
If anyone can help me here using JavaScript or jQuery. It will be a great help.
This is the button. It's not a form.
<span class="fa fa-check" data-tldinit="true"></span>SEND ME THE PDF<canvas class="lfb_shineCanvas" data-tldinit="true" width="202" height="41" style="border-radius: 4px;"></canvas>
This is how I want to send the values:
https://scripts.google.com/?name=name
Try this:
$.ajax({
url: "your url",
type: "get", //send it through get method
data: {
name: "put the name value here",
},
success: function(response) {
//Do Something
},
error: function(xhr) {
//Do Something to handle error
}
});
Simplest form of get call using jQuery
$.get('your_function.php', {'var':'value'}, function() {});
I have a very long form in a php template, all data are send by a jquery ajax in serialize mode to a php page to send an email.
I have this problem: How I can unserialize the data in the php page, and send them by email ?
The php template is completed with different data. So I can't use for each fields just urldecode in this way $nome = urldecode($_POST['nome']);
I have to find a way for take all the data arrive in the php page and send them in an email. I read about parse_str But I don't know if can works and how use it.
At moment this is my ajax code:
var datiform = $("#FormWorkspace").serialize();
$.ajax({
type: "POST",
url: ".../form-cf/engine.php",
data: datiform,
dataType: "html",
success: function(datiform)
{
alert("success" + datiform);
},
error: function()
{
alert("no success " + datiform);
}
});
serializing in jquery means to actually create a string var1=1&var2=234. You can simple access the data using GET['var1'] or POST['var2'] in PHP.
I've read countless examples but I just can't get the following to work. I want to submit a form, process it on the server side then decide which php script to load into a div (dynamic loading I can do).
The head:
$.ajax({
type: "POST",
url: url, // passed with onclick
data: $("#" + formName).serialize(),
success: function(data) {
// Some JSON? to go here to store variable returned from PHP ($thisVariable)
}
});
return false; // avoid to execute the actual submit of the form.
The php:
// Process the request (add to database e.g.)
$thisVariable back to ajax
I want to get a text from a div#main, send the text via Ajax jQuery to a php file from where I wil send it via email.
The user can choose the recipient of the from a multiple radio buttons choices on the site.
After I send the data to the PHP file I want to do some styles (e.g. remove a class, etc..).
So far I have this, can you tell me if it is right?
$(document).ready(function(){
$('#submit').click(function(){
$.ajax({
type: 'POST',
url: 'email.php',
email: { recipient: $('input[type=radio]:checked').attr("value")},
data: { content: $('#main').text()},
success: function()
{
$("#email").removeClass("active");
}
});
});
});
PS: I dont have the PHP file yet, I want just to figure out if my Ajax solution above is the right way to do what I've described.
All of the data that you want to send back to the server needs to be in the data property. Also, you probably want to use the val() method rather than accessing the value attribute directly.
data: { content: $('#main').text(), recipient: $('input:radio:checked').val() },
$(document).ready(function(){
$('#submit').click(function(){
$.post("email.php", { recipient: $('input[type=radio]:checked').val(), content: $('#main').text()}, function(){
$("#email").removeClass("active");
});
});
});