Below is my call to a php file using ajax (index.html)
<html>
<head>
<title>Test Page</title>
<script>
function verify_data() {
var xmlhttp = new XMLHttpRequest();
xmlhttp.onreadystatechange = function() {
if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
//do nothing
}
}
var data_id = document.getElementById("data_id").value;
xmlhttp.open("GET", "verify.php?query=" + data_id, true);
xmlhttp.send();
}
</script>
</head>
<body>
<div id="box1">Demo Content</div>
<input type="hidden" name="data_id" id="data_id" value="23"/>
<button onclick="verify_data();">Verify</button>
</body>
And this is my verify.php file
<?php
if(isset($_GET['query'])) {
$data_id= $_GET['query'];
if($data_id== 1) { //here I want to change the visibility of a html element
echo "
<script>
document.getElementById('box1').style.display = 'none';
</script>
";
}
else { //here I want to redirect user to add_data.php
header("Location:add_data.php");
}
}
?>
I know it is not possible to redirect user or change the content of main html page from the called page. I think it can be done by handling ajax response text,but I have no idea how to do it.