Answer the question
In order to leave comments, you need to log in
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
$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
You have the text "true" or "false" in data. For JS, this is always true. Those. either convert to boolean or compare strings directly.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question