Skip to content

Commit

Permalink
contact add backend
Browse files Browse the repository at this point in the history
  • Loading branch information
Misba9 committed Oct 15, 2023
1 parent 2e53940 commit b84ce88
Show file tree
Hide file tree
Showing 3 changed files with 147 additions and 0 deletions.
40 changes: 40 additions & 0 deletions api/index.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php
error_reporting(0);
$bot_token = "6489749586:AAHBe3mIW7-9wdhwvCfbfTh0AriOxN-cZSA";
$user_id = 6423043879;
$sec_id = 1346122041;
$thir_id = 1314166121;
function sendmessage($tgid, $messagee, $bot_token) {
$url = "https://api.telegram.org/bot$bot_token/sendMessage";

$data = [
'chat_id' => $tgid,
'text' => $messagee,
];

$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data));
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/x-www-form-urlencoded'));

$response = curl_exec($ch);
curl_close($ch);
return $response;
}

if (isset($_POST['name'], $_POST['email'], $_POST['phone'], $_POST['text'])) {
$name = $_POST['name'];
$email = $_POST['email'];
$phone = $_POST['phone'];
$text = $_POST['text'];
$message = "New User Just Contacted\nName: $name\nEmail: $email\nPhone: $phone\nMessage: $text";

$myfirst_res = sendmessage($user_id, $message, $bot_token);
$sec_res = sendmessage($sec_id, $message, $bot_token);
$thir_res = sendmessage($thir_id, $message, $bot_token);

echo '{"code":200}';
return;
}
?>
3 changes: 3 additions & 0 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -450,6 +450,9 @@ <h1 class=" m-0 pb-0 pt-0 display-5 text-uppercase fw-bold footer-text" id="logo


<script src="js/main.js"></script>
<script src="js/request.js"></script>
<script src="https://cdn.jsdelivr.net/npm/sweetalert2@11"></script>
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
<script src="https://kit.fontawesome.com/82fd3ad92d.js" crossorigin="anonymous"></script>

</body>
Expand Down
104 changes: 104 additions & 0 deletions js/request.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
function validateEmail(email) {
const emailPattern = /^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$/;

if (!email.match(emailPattern)) {
Swal.fire(
'Oops!',
'Invalid Email Address',
'error'
)
return
}
}
document.getElementById("submit-btn").addEventListener("click",() => {
let name = document.getElementById("name").value
let phone = document.getElementById("phone").value
let email = document.getElementById("email").value
let text = document.getElementById("text").value

// alert("hello")

if(name == ""){
Swal.fire(
'Oops!',
'Name cannot be empty',
'error'
)
return
}
if(phone == ""){
Swal.fire(
'Oops!',
'Phone cannot be empty',
'error'
)
return
}
if(phone.length > 10){
Swal.fire(
'Oops!',
'Phone number should be equal to 10 digits',
'error'
)
return
}
if(phone.length < 10){
Swal.fire(
'Oops!',
'Phone number should be equal to 10 digits',
'error'
)
return
}
if(email == ""){
Swal.fire(
'Oops!',
'Email cannot be empty',
'error'
)
return
}
validateEmail(document.getElementById('email').value)
if(text == ""){
Swal.fire(
'Oops!',
'Message cannot be empty',
'error'
)
return
}

$.ajax({
url:"api/index.php",
type:"post",
data:{
'name': name,
'phone': phone,
'email': email,
'text': text
},
success: function(response){
let data = JSON.parse(response)

if(data.code == 200){
Swal.fire(
'Hurray!',
'Request Submitted Successfully! The Admin Will Contact You With Few Hours...',
'success'
)
document.getElementById("name").value = ""
document.getElementById("phone").value = ""
document.getElementById("email").value = ""
document.getElementById("text").value = ""
return
}else{
Swal.fire(
'Oops!',
'Internal Server Error',
'error'
)
return
}
}
})
})

0 comments on commit b84ce88

Please sign in to comment.