0

PHP-how to send data to another php-sql page and recive an chart image on the same page

hi guys. i have an problem and i dont know how i could solve that. i have an mainpage, where i want to put some data in an input field, which would send the data to an other page to create the chart. ok. thats not the problem. it works fine, if i open the secound page. the chart img is created. but if i want to stay on the mainpage klicking the button and recive the img from the secound page, the img is empty and i dont know how to realize that.

here is an short code of the mainpage.php

echo "<form action='#' method='post' type='text'>";
echo "Abzufragendes Jahr [JJJJ]:";
echo '<input type="text" name="eingabe_1" value="">';
echo "<input type='submit' name='eingabe_2' value='DB Abfrage'/>";
echo "<input type='submit' name='ausgabe_statistik' value='Charts erstellen' 
formaction='#' formmethod='post'/></br></br></br></br>";
echo "</form>";

//Versuch das Bild zu erzeugen
if(isset($_POST['eingabe_1']) && isset($_POST['ausgabe_statistik'])) {
  echo "<img src='charts/charts_jahr.php'> </br>";
}

and the secound page consists

include("../../PHP/pChart2.1.4/class/pData.class.php"); 
include("../../PHP/pChart2.1.4/class/pDraw.class.php"); 
include("../../PHP/pChart2.1.4/class/pImage.class.php"); 

include("../../SQL/log-in.php"); 
mysql_select_db(MYSQL_DATENBANK) or die("Auswahl der Datenbank fehlgeschlagen");

for ($a = 1 ; $a <= 365 ; $a++) {
  $kalenderwoche[] = $a;
}
$var = $_POST['eingabe_11'];
$var_jahr = "j" . $var;

$Result = mysql_query("SELECT sum(gewicht) as gewicht_1 FROM $var_jahr group by datum",$db_link);
while($row = mysql_fetch_array($Result))
{
  $gewicht[] = $row["gewicht_1"];
  $fett[] = $row["fett"];
}
//chartpart  
$myData = new pData();
$myData->addPoints($gewicht,"Serie1");
..
.

i thought over another solution to save the temp var in he sql and get the value for $var_jahr out of them. but i think thats not the solution right?

br and many thx for an solution!

  • 1
    Don't use the `mysql_*` functions. They have been deprecated since v5.5 (Jun 2013) and removed since v7.0 (Dec 2015). Instead use the [**mysqli_***](https://secure.php.net/manual/en/book.mysqli.php) or [**PDO**](https://secure.php.net/manual/en/book.pdo.php) functions with [**prepared statements**](https://secure.php.net/manual/en/pdo.prepare.php) and [**bound parameters**](https://secure.php.net/manual/en/pdostatement.bindparam.php). – Alex Howansky Jun 02 '17 at 17:57
  • 1
    Your code is vulnerable to [**SQL injection**](https://en.wikipedia.org/wiki/SQL_injection) attacks. You should use [**mysqli**](https://secure.php.net/manual/en/mysqli.prepare.php) or [**PDO**](https://secure.php.net/manual/en/pdo.prepared-statements.php) prepared statements with bound parameters as described in [**this post**](https://stackoverflow.com/questions/60174/how-can-i-prevent-sql-injection-in-php). – Alex Howansky Jun 02 '17 at 17:57
  • You want to do an asynchronous call from the client. – Matt Jun 02 '17 at 17:57
  • thx for the information. i will change that soon. never thought about that. i am new in that scene. @mkaatman, so i have the Impression, thats not possible what i want to do. so i have to bind an tmp var in my sql database and query it out right? – ein_noch_mieser_progger Jun 02 '17 at 19:35
  • @ein_noch_mieser_progger It is possible, it's an ajax request. You have your rendered HTML with a form or however you want to input the data and you can either immediately render it with javascript (d3js) or you can make an ajax call to a PHP script that processes the data and sends back an image. Both of those options will avoid a page refresh/redirect. – Matt Jun 02 '17 at 19:54
  • @mkaatman: thx for the info, but i cant Java atm. i solved it with SQL inputs and querys. – ein_noch_mieser_progger Jun 05 '17 at 21:35

1 Answers1

0

My solution:

    if ( isset($_POST['statistik_gewicht']) ) {
            $var22 = $_POST['eingabe_1'];
            mysql_query("update variablen set var1 = $var22 where id = 1");
            mysql_query("update variablen set var_gewicht = 1 where id = 1");
            echo "<div align='center'><img src='charts/charts_jahr.php'></div> </br>";
    }

Not well, but it works! Write an tmp var over

echo '<td colspan="3">Abzufragendes Jahr [JJJJ]';  echo '<input type="text"     name="eingabe_1" value="">';