Let say I have file1.php containing:
<!DOCTYPE html>
<html>
<head>
<title>My page</title>
</head>
<body>
<form action="file1.php" method="post">
<input type="radio" name="radio" value="1">1
<input type="radio" name="radio" value="2">2
<?php include 'file2.php'; ?>
<input type="submit" name="submit" value="Submit">
</form>
//some codes omitted here
</body>
</html>
and file2.php:
<?php
header('Content-Type: application/json');
$value_selected = 0;
if (isset($_POST['submit'])) {
if(isset($_POST['radio'])) {
$value_selected = $_POST['radio'];
}
}
include("connect_to_database.php");
$sql =
"SELECT field1, field2 FROM some_table WHERE field1 = ".$value_selected;
$query = mysql_query($sql);
$data = array();
while ($row = mysql_fetch_assoc($query)) {
$data[] = $row;
}
mysql_free_result($result);
print json_encode($data);
?>
The problem is when I run file1.php, it displays in text (JSON I believe) form because of the header in file2.php. Is there any way to make only the file2.php in JSON format but not file1.php?