Javascript send data with post and PHP get the data
PHP get the data
<?php
// API endpoint URL
// $api_url = 'https://test.httpapi.com/api/domains/search.json?auth-userid=1218079&api-key=ot4Qrz0PMetJIzl2PLQmRg5hzB3esQkU&no-of-records=10&page-no=1';
$jsonData = file_get_contents("php://input");
if (!empty($jsonData)) {
$data = json_decode($jsonData, true);
if ($data === null) {
$response = array("error" => "Invalid JSON data");
http_response_code(400); // Bad Request
} else {
$domain_name = $data["searchnthsk"];
$api_url = 'https://domaincheck.httpapi.com/api/domains/available.json?auth-userid=1218079&api-key=ot4Qrz0PMetJIzl2PLQmRg5hzB3esQkU&domain-name='.$domain_name.'&tlds=com&tlds=in&tlds=co.in&tlds=org&tlds=net';
$ch = curl_init($api_url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); // Return the response as a string
$response = curl_exec($ch);
if (curl_errno($ch)) {
echo 'cURL Error: ' . curl_error($ch);
}
curl_close($ch);
if ($response !== false) {
$data = json_decode($response, true);
if ($data !== null) {
// print_r($data);
$response = array("message" => "Data received successfully", "data" => $data);
} else {
// echo 'Error decoding JSON response';
$response = array("error" => "Error decoding JSON response");
}
} else {
// echo 'Error making API request';
$response = array("error" => "Error making API request");
}
// $response = array("message" => "Data received successfully", "key1" => $domain_name);
}
} else {
$response = array("error" => "No data received");
http_response_code(400); // Bad Request
}
header("Content-Type: application/json");
echo json_encode($response);
exit();
?>
<!--<input type="" name="">-->
PHP get the data
<?php
// API endpoint URL
// $api_url = 'https://test.httpapi.com/api/domains/search.json?auth-userid=1218079&api-key=ot4Qrz0PMetJIzl2PLQmRg5hzB3esQkU&no-of-records=10&page-no=1';
// $jsonData = file_get_contents("php://input");
// if (!empty($jsonData)) {
// $data = json_decode($jsonData, true);
// if ($data === null) {
// $response = array("error" => "Invalid JSON data");
// http_response_code(400); // Bad Request
// } else {
if (isset($_GET['domain'])) {
$searchQuery = $_GET['domain'];
echo 'Search Query: ' . $searchQuery . '<br>';
} else {
echo 'Search Query is not provided.<br>';
}
$domain_name = $searchQuery;
$api_url = 'https://domaincheck.httpapi.com/api/domains/available.json?auth-userid=1218079&api-key=ot4Qrz0PMetJIzl2PLQmRg5hzB3esQkU&domain-name='.$domain_name.'&tlds=com&tlds=in&tlds=co.in&tlds=org&tlds=net';
$ch = curl_init($api_url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); // Return the response as a string
$response = curl_exec($ch);
if (curl_errno($ch)) {
echo 'cURL Error: ' . curl_error($ch);
}
curl_close($ch);
if ($response !== false) {
$data = json_decode($response, true);
if ($data !== null) {
// print_r($data);
$response = array("message" => "Data received successfully", "data" => $data);
} else {
// echo 'Error decoding JSON response';
$response = array("error" => "Error decoding JSON response");
}
} else {
// echo 'Error making API request';
$response = array("error" => "Error making API request");
}
// $response = array("message" => "Data received successfully", "key1" => $domain_name);
// }
// } else {
// $response = array("error" => "No data received");
// http_response_code(400); // Bad Request
// }
// if ($data === null) {
// header("Content-Type: application/json");
// echo json_encode($response);
// exit();
// }
?>
<!--<input type="" name="">-->
<!DOCTYPE html>
<html lang="en">
<head>
<title>Digital Startups Price List</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="icon" href="favicon-32x32.png">
<link rel="stylesheet" href="css/bootstrap.min.css">
<link rel="stylesheet" href="css/font-awesome.css">
<link rel="stylesheet" href="css/style.css">
</head>
<body>
<div class="container pt-4 pb-3">
<div class="row d-flex flex-wrap justify-content-center">
<div class="col-md-9 col-sm-9 pb-2">
<a href="javascript:void(0)" class="hvr-icon-back" onclick="goBack()">Back</a>
</div>
<div class="col-md-6 col-sm-6 price-list tbl-bdr pl-0 pr-0">
<table class="table table-bordered">
<thead>
<tr>
<th width="50%">Domain Name</th>
<th width="50%">Class</th>
<th width="50%">Status</th>
</tr>
</thead>
<tbody>
<?php
foreach ($data as $key=>$value){
?>
<tr>
<td><?php echo $key; ?></td>
<td><?php echo $value['classkey']; ?></td>
<td><?php echo $value['status']; ?></td>
</tr>
<?php
}
?>
</tbody>
</table>
</div>
</div>
</div>
<script src="js/jquery.min.js"></script>
<script src="js/popper.min.js"></script>
<script src="js/bootstrap.min.js"></script>
<script>
function goBack() {
window.history.back();
}
</script>
</body>
</html>
JAVASCRIPT
function validateForm(){
var searchnthsk = document.getElementById("searchnthsk").value;
if (searchnthsk === "" ) {
alert("Search field cannot be empty!");
return false; // Prevent form submission
}
var redirectUrl = "domain-list.php?domain=" + encodeURIComponent(searchnthsk);
window.location.href = redirectUrl;
// var xhr = new XMLHttpRequest();
// xhr.open("GET", "test_api.php", true);
// var data = {
// searchnthsk: searchnthsk,
// };
// Convert the data to a JSON string
// var jsonData = JSON.stringify(data);
// xhr.open("POST", "domain-list.php", true);
// Set the appropriate headers for sending JSON data
// xhr.setRequestHeader("Content-Type", "application/json;charset=UTF-8");
// Define a callback function to handle the response
// xhr.onreadystatechange = function() {
// if (xhr.readyState === 4 && xhr.status === 200) {
// var response = JSON.parse(xhr.responseText);
// console.log(response); // Handle the response data here
// }
// };
// Send the request with the JSON data
// xhr.send(jsonData);
}
Comments
Post a Comment