Perhaps the question is stupid, but I'm still a novice, so I can not handle the second day. It is impossible to simultaneously receive mail and data on the form and text data from the block.

The main problem begins in the JS file with the line $ .post ('Post_Constructor.php' ...

  1. If I delete '& item_price =' + $ ('# price'). Text ()

    • Come to email data on the form!
  2. And if I delete $ ('# contruct-from'). SerializeArray ()

    • Comes to the email text content <span id="price">900</span>
  3. If nothing is deleted, then the browser inspector will show this:

item_price: 900 [object Object], [object Object], [object Object]

... and the form will be given the number 900 I need and the string of "objects".

As far as I know, the matter is not with the handler, but in the wrong data transfer.

HTML

 <span id="price">900</span> <form id="contruct-from"> <input name="namecontr"> <input name="telcontr"> <input name="emailcontr"> <button form="contruct-from" type="submit">ОТПРАВИТЬ</button> </form> 

Js

 $('.btn-contruct').click(function(){ if($("#contruct-from")[0].checkValidity()) { $.post('Post_Constructor.php', '&item_price=' + $('#price').text() + $('#contruct-from').serializeArray(), function(data) { }); console.log('ok'); return false; } }); 

PHP handler

 <?php session_start(); new post_contact($_POST); class post_contact { public function __construct($post_data) { $this->send_contact_form($post_data); } function send_contact_form($post_data) { $headers = "MIME-Version: 1.0" . PHP_EOL . "Content-Type: text/html; charset=utf-8" . PHP_EOL . 'From: test@email.com <test@email.com>' . PHP_EOL; $message = '<p>'.$_POST['namecontr'].'</p>'; $message .= '<p>'.$_POST['telcontr'].'</p>'; $message .= '<p>'.$_POST['emailcontr'].'</p>'; $message .= '<p>'.$_POST['item_price'].'</p>'; mail('test@email.com', 'Помогите пожалуйста', $message, $headers ); } } 

    1 answer 1

    Use

      $('#contruct-from').serialize() 

    instead

      $('#contruct-from').serializeArray(); 

    serializeArray () returns an array of objects that can be sent in JSONP format, you need to use serialize () to get the query string

    shipping:

     $.post('Post_Constructor.php', 'item_price=' + $('#price').text() +'&'+ $('#contruct-from').serialize(), function(data) {}); 
    • I did, now this is the result: ----------------- NULL string (19) ---------- "+ 38 (012) 345-67- 89 "string (14) ------------" test@email.com "string (19) -------------" 900namecontr = NAME "--- ----------------------------- And this is what comes to the mail --------------- ---------- +38 (012) 345-67-89 ------------------------- test@email.com ---------------------- 900namecontr = NAME - Maxim Spassky
    • $ .post ('Post_Constructor.php', 'item_price =' + $ ('# price'). text () + '&' + $ ('# contruct-from'). serialize (), function (data) { }); - Andrei Berzhanin
    • Thank you very much. Everything works on perfectly. I wonder how now to do so in order not to come to spam) - Maxim Spassky
    • use smtp - Andrei Berzhanin