L
L
Lyubimoff2020-12-20 15:18:58
PHP
Lyubimoff, 2020-12-20 15:18:58

php ajax js feedback form. How to receive data on the server?

There is a form:

<form form-name="callback" id='callback' class="js-form">
            
                <div class="form-group">
                <input field-name="phone" name="phone" type="text" class="phone form-control" placeholder="+7 (___) ___-__-__">
                <small class="form-text text-muted">Укажите ваш номер телефона и мы свяжемся с вами в ближайшее время.</small>
                </div>
                
                <button class="submit btn btn-warning">Перезвоните мне!</button>
            </form>


js handler:
self.Send = function()
  {
    if ( typeof self.GetExport().phone !== 'undefined' )
    {
      var obj = JSON.stringify( self.GetExport() );
      
      console.log( obj );
      
      $.post( 'no', obj )
      .done( function( data )
      {
        console.log( data );
        
        self.Clear();
        modals.HideAll(); // скрыть все модальные окна
        modals.Open( 'form-success' ); // показать modal success
      } );
    }
    else
    {
      alert( 'Номер телефона не может быть пустым!' );
    }
  };


I can't write php code to accept this data and send it to
php mail:
<?php

if (isset($_POST['email'])) {$email = $_POST['email']; if ($email == '') {unset($email);}}
if (isset($_POST['phone'])) {$phone = $_POST['phone']; if ($phone == '') {unset($phone);}}
if (isset($_POST['name'])) {$text = $_POST['name']; if ($text == '') {unset($text);}}
 
$answer=['ok'=>$phone.' = phone'];

if (isset($email) or isset($phone) or isset($text)){
  $to  = "<[email protected]***.ru>, " ; 
  $to .= "<***[email protected]>"; 

  $subject = "Заявка с сайта!"; 

  $message = ' <b>Имя:'.$name.' </b> </br> <b>Телефон: '.$phone.'</b> </br><i>Почта: '.$email.'</i> </br>';

  $headers  = "Content-type: text/html; charset=utf-8 \r\n"; 
  $headers .= "From: <[email protected]***>\r\n"; 
  $headers .= "Reply-To: ***[email protected]\r\n"; 


  $send = mail($to, $subject, $message, $headers); 

  if ($send == true) {
    $answer =[
     	'ok'=>'done'
    ];
  }
  else {
    $answer=[
      'ok'=>'error'
    ];
  }
}
echo json_encode($answer);

?>


In the console I get the following response:
{"phone":"71515123131","form":"callback","url":"/"}
{"ok":" = phone"}

As I understand it, I'm parsing the data incorrectly in php. How should this be done here?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexander Lykasov, 2020-12-21
@lykasov-aleksandr

Start by checking the php script input. Dump $_POST (and $_GET) with var_dump or var_export

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question