P
P
Phantasma2021-03-01 08:53:29
PHP
Phantasma, 2021-03-01 08:53:29

How to send the contents of the basket to the mail in the normal form?

Internet shop, food delivery. The form fields Name, Phone, Address, total amount of the order are sent as they should, and the list of products itself comes in this form:

Заказ [{"блюдо":"Овощи с жареным тофу","количество":2},{"блюдо":"Рисова лапша ,"количество":1},{"блюдо":"Лапша удон с говядиной и овощaми","количество":3},{"блюдо":"Суп с говядиной ","количество":2}]

I would like at least without brackets to begin with :)
This is how data is collected in js for sending

let array = cartProductsList.querySelector('.simplebar-content').children;
        let fullprice = fullPrice.textContent;
        
        let length = array.length;
            document.querySelector('.order-modal__quantity span').textContent = `${length} шт`;
            document.querySelector('.order-modal__summ span').textContent = `${fullprice}`;
            let formTotal = document.getElementById('formTotal')
            formTotal.value = `${fullprice}`;
            
            for (item of array) {
                let img = item.querySelector('.cart-product__img').getAttribute('src');
                let title = item.querySelector('.cart-product__title').textContent;
                let priceString = priceWithoutSpaces(item.querySelector('.cart-product__price').textContent);
                let id = item.querySelector('.cart-product').dataset.id;
                let qtyNumber = Number(item.querySelector('.cart-product-field').value);
                let productCost = Number(item.querySelector('.cart-product-field').dataset.price);
               
                orderModalList.insertAdjacentHTML('afterbegin', generateModalProduct(img, title, productCost,  priceString, qtyNumber,  id));
                
                let obj = {};
                obj.блюдо = title;
                obj.количество = qtyNumber;

                productArray.push(obj);
               
                let order = JSON.stringify(productArray)
                let total = JSON.stringify(fullprice)
        }


and this is php

use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;

require 'PHPMailer-6.3.0/src/Exception.php';
require 'PHPMailer-6.3.0/src/PHPMailer.php';

$mail = new PHPMailer(true);
$mail->CharSet = 'UTF-8';
$mail->setLanguage('ru', 'PHPMailer-6.3.0/language/');
$mail->IsHTML(true);

$mail->setFrom('adress.ru', 'zakaz'); //От кого письмо
$mail->addAddress('adress.ru'); // кому отправить
$mail->Subject = 'Заказ с сайта Azia-bo'; //Тема письма

//Тело письма 
$body = '<h1>Привет</h1>';

if(trim(!empty($_POST['name']))){
    $body.='<p><strong>Имя</srtong> '.$_POST['name'].'</p>';
}
if(trim(!empty($_POST['tel']))){
    $body.='<p><strong>Телефон</srtong> '.$_POST['tel'].'</p>';
}
if(trim(!empty($_POST['adress']))){
    $body.='<p><strong>Адрес доставки:</srtong> '.$_POST['adress'].'</p>';
}

if(trim(!empty($_POST['order']))){
    $body.='<p><strong>Заказ</srtong> '.($_POST['order']).'</p>';
}
if(trim(!empty($_POST['formTotal']))){
    $body.='<p><strong>Общая сумма заказа</srtong> '.$_POST['formTotal'].'</p>';
}
$mail->Body = $body;


//отправляем
if (!$mail->send()) {
    $message = 'Ошибка';

} else {
    $message = 'Данные отправлены';
}

$response = ['message' => $message];

header('Content-type: application/json');
echo json_encode($response);


I pulled everything from different sources, so it’s possible that the vinaigrette is complete here, I don’t understand anything in php at all.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
N
Nadim Zakirov, 2021-03-01
@zkrvndm

The one in brackets is called JSON. Google how to work with JSON in PHP.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question