K
K
Kozlov2018-03-05 15:21:53
PHP
Kozlov, 2018-03-05 15:21:53

How to get response when sending php telegram data?

There is a form through which data is sent to php using ajax, which sends this data to the telegram bot.
I can't make the alert that pops up after submit react to the state of php. That is, alert always writes ok even if the script has not been executed.

js

function formsend(formMain) {
  jQuery.ajax({
    url:     "post.php",
    type:     "POST",
    dataType: "text",
    data: jQuery("#"+formMain).serialize(), success: function(data) {
   if (data) { 
   	//Выводим сообщение ели все успешно
        alert ("Сообщение отправлено!");
      } else {     
       //Выводим если что-то пошло не так
        alert ("Что-то полшо не так\nПопробуйте позже");
      }
    },
    })			
}

PHP
<?php
$empty='';
$text=$_REQUEST['text'];
$first_name=$_REQUEST['first_name'];
$phone=$_REQUEST['phone'];
if ($text==$empty){
  echo ("false");
}
else if ($first_name==$empty){
  echo("flase");
}
else if ($phone==$empty){
  echo ("false");
}
else{
  $testmessage="<b>Имя:</b> ".$first_name."\n<b>Телефон: </b>".$phone."\n".$text;
  $send="https://api.telegram.org/bot/sendMessage?chat_id=&parse_mode=html&text=".urlencode($testmessage);
   file_get_contents($send);
   echo ("true");
}
?>

Answer the question

In order to leave comments, you need to log in

2 answer(s)
V
Vladimir Skibin, 2018-03-05
@romandkoz

You have the text "true" or "false" in data. For JS, this is always true. Those. either convert to boolean or compare strings directly.

E
Egor Zhivagin, 2018-03-05
@Krasnodar_etc

You don't need to give a string in php, but a boolean

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question