-1

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?

cathy305
  • 57
  • 1
  • 1
  • 8
  • Every time you use [the `mysql_`](http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php) database extension in new code **[this happens](http://2.bp.blogspot.com/-zCT6jizimfI/UjJ5UTb_BeI/AAAAAAAACgg/AS6XCd6aNdg/s1600/luna_getting_strangled.jpg)** it is deprecated and has been for years and is gone for ever in PHP7. If you are just learning PHP, spend your energies learning the `PDO` or `mysqli` database extensions and prepared statements. [Start here](http://php.net/manual/en/book.pdo.php) – RiggsFolly Apr 03 '17 at 14:53
  • 1
    According to your code, there is nothing there that links the 2 files together. – Jonnix Apr 03 '17 at 14:53
  • Jon See line 10 of `file1.php` – RiggsFolly Apr 03 '17 at 14:56
  • Why are you including file2 within file1? According to your code, it should not be included there. What are you trying to achieve? – yogur Apr 03 '17 at 14:56
  • I do not see why you would want to print a lump of JSON in that place in your form. Are you trying to avoid learning how to do AJAX? – RiggsFolly Apr 03 '17 at 14:58
  • @RiggsFolly Ah, took a few attempts to spot that one... that explains that then. – Jonnix Apr 03 '17 at 14:59
  • @yogur There is actually a js file use to create line graph based on the json data from file2.php. The radio buttons in file1.php allow user to select what kind of data to be displayed in the graph. The js is then called inside file1.php – cathy305 Apr 03 '17 at 15:01
  • 1
    Then it might be better to ask for this data using an AJAX call which woudl be part of the existing javascript, rather than trying to dump it inside a piece of HTML in a random way – RiggsFolly Apr 03 '17 at 15:03
  • @RiggsFolly I forgot to mention that we just started learning PHP and JSON. We haven't touch on AJAX yet. That's why we're only required to use JSON to get us familiar with it – cathy305 Apr 03 '17 at 15:14
  • @RiggsFolly Just for the record, your comments about mysql_ database extension got a big laugh from me and my developer team mates :D – yogur Apr 03 '17 at 16:01
  • @yogur Glad to have lightened the mood in your developer shop – RiggsFolly Apr 03 '17 at 16:08

1 Answers1

0

If you are not quite ready for coding an AJAX call to ask for this data from your javascript then this may get you moving in the right direction.

It will load the data into a javascript variable that can be used from your other javascript

file2.php

<?php    
$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;
        }
        echo '<script type="text/javascript">';            
        echo 'var theData = ' . json_encode($data) . ';';
        echo '<script type="text/javascript">';
        mysql_free_result($result);
    }
}

?>

Now call this from inside your <head> tag and you will be a little closer to what you want.

file1.php

<!DOCTYPE html>
<html>
<head>
<title>My page</title>

<?php include 'file2.php'; ?>

</head>
<body>
<form action="file1.php" method="post">
<input type="radio" name="radio" value="1">1
<input type="radio" name="radio" value="2">2

<input type="submit" name="submit" value="Submit">
</form>
//some codes omitted here
</body>
</html>

However I feel I must add Please dont use the mysql_ database extension it is deprecated and has been for years and is gone for ever in PHP7. If you are just learning PHP, spend your energies learning the PDO or mysqli database extensions and prepared statements. Start here

RiggsFolly
  • 93,638
  • 21
  • 103
  • 149
  • Very easy to understand. Thank you. However, the js file actually contains AJAX (which we'll learn in the future) that gets its data from the `json_encoded($data)` in the file2.php file. If we're doing it your way, how would I call the `theData` variable from the js file if I call the js file in file1.php like this: ``.? – cathy305 Apr 03 '17 at 15:40
  • Somewhere you must start the processing that is in that js file! How do you do that – RiggsFolly Apr 03 '17 at 15:48