0

A lot of versions around but i can not get it right...

I need to transfer variables from one php script file to another.

script1.php has the variables:

$t1 =  $_POST['t1'];
$t2 =  $_POST['t2'];

and I need to work with these variables in another script file - script2.php

How can I access these variables in script2.php file?

OK. i will post all the script.

script1.php or in fact getImage.php is following

<?php 
$today = date("Ymd_His"); 

$t1 =  $_POST['t1']; //mazais nosaukums
$t2 =  $_POST['t2']; //lielais nosaukums

$c1 =  $_POST['c1'];  //kjeksis
$c2 =  $_POST['c2'];  //kjeksis 
$c3 =  $_POST['c3'];  //kjeksis 
$c4 =  $_POST['c4'];  //kjeksis 

$mazais =  $_POST['mazais'];    //maza bilde atseviskji
$lielais =  $_POST['lielais'];    //maza bilde atseviskji    

$response = "";

$error_response = "error.php";
$succes_response = "nextstep.php";


$etikjete = base64_decode($_REQUEST['png']);
if ($handle1 = fopen("render/".$today.'.png', 'w+')) {
    if (!fwrite($handle1, $etikjete) === FALSE) {
        fclose($handle1);
        $response .= "Success etikjete! "; 
    } else {
        $response .=  "fwrite error etikjete! ";
        $error = true;
    }
} else {
    $response .=  "fopen error etikjete! ";
    $error = true;    
}

if($mazais){
    $etikjete = base64_decode($mazais);
    if ($handle1 = fopen("render/".$today.'_mazais.png', 'w+')) {
        if (!fwrite($handle1, $etikjete) === FALSE) {
            fclose($handle1);
            $response .= "Success mazais! "; 
        } else {
            $response .=  "fwrite error mazais! ";
            $error = true;
        }
    } else {
        $response .=  "fopen error mazais! ";
        $error = true;    
    } 
}      
if($lielais){
    $etikjete = base64_decode($lielais);
    if ($handle1 = fopen("render/".$today.'_lielais.png', 'w+')) {
        if (!fwrite($handle1, $etikjete) === FALSE) {
            fclose($handle1);
            $response .= "Success lielais! "; 
        } else {
            $response .=  "fwrite error lielais! ";
            $error = true;
        }
    } else {
        $response .=  "fopen error lielais! ";
        $error = true;    
    } 
}
if($error){
         echo $error_response;
} else {
        echo $succes_response;
}
?>

and script2.php here is called nextstep.php and in this nextstep.php I need to save the variables from getImage.php into database. nextstep.php so far is like this but does not work. connection with db is ok, if I set different independent variables on nextstep.php then they gets stored into db.

<?php 

include 'getImage.php';

// Connects to your Database
mysql_connect("localhost", "user", "pass") or die(mysql_error()) ;
mysql_select_db("myDB") or die(mysql_error()) ;

//Writes the information to the database
mysql_query("INSERT INTO jos_orders (maza_bilde,liela_bilde,mazais_nosaukums,lielais_nosaukums)
VALUES ('$t1', '$c2', '$mazais', '$lielais')") ;

?>
raivis
  • 37
  • 1
  • 2
  • 9
  • 4
    That depends. How are you accessing `script2.php`? – Palladium Jul 27 '12 at 15:38
  • script2.php or in fact nextstep.php gets accessed by `$succes_response = "nextstep.php";` – raivis Jul 27 '12 at 16:08
  • Firstly, `echo`ing a filename does not execute the file. Secondly, @ARIF's answer is probably the easiest way to solve your problem (recap: either use sessions or merge the files into one). – Palladium Jul 27 '12 at 16:11

3 Answers3

2

How about these:

  1. Session - store your data to session variables to that it can be retrieve to any pages of your site. The data will be cleared when you close your browser.

    session_start();

    $_SESSION['t1'] = $_POST['t1'];
    
    $_SESSION['t2'] = $_POST['t2'];
    
    1. Get Variable = Pass the variable through the URL

page2.php?t1=somevars&t2=somevars

$t1 = $_GET['t1'];

$t2 = $_GET['t2'];

3.$_COOKIE[] - http://www.w3schools.com/php/php_cookies.asp

Johndave Decano
  • 2,101
  • 2
  • 16
  • 16
1

You want to access the variables from a different script so it seems you are redirecting your script else all the global variables are accessible from any script as we know. So the solution can be session, else you if you are just doing some insert or update or delete action I suggest you submit it to the same page and include a file pass an action value check it from the included file perform the action according to that and redirect. Another solution is to built a small mvc application take a look at some mvc applications code I hope you can understand. Hope this will help you.

ARIF MAHMUD RANA
  • 5,026
  • 3
  • 31
  • 58
  • Hijacking this answer for a moment and quoting @Truth: Please, don't use mysql_* functions for new code. They are no longer maintained and the community has begun the deprecation process. See the red box? Instead you should learn about prepared statements and use either PDO or MySQLi. If you can't decide, this article will help to choose. If you care to learn, here is good PDO tutorial. – Palladium Jul 27 '12 at 16:07
  • yes, thanks @Palladium ! I will rewrite to use MySQLi. But in my case nextstep.php gets executed.. At least if I write simple echo syntax there then it gets displayed after I run getImage.php hmm... stucked really.. I will try sessions – raivis Jul 27 '12 at 16:19
  • @raivis your coding purpose isn't clear to me are you submitting data than follow my instructions I think it will be best for you. – ARIF MAHMUD RANA Jul 27 '12 at 16:27
0

You can create them in $_SESSION[]

Check this out : Storing Form Data as a Session Variable

Community
  • 1
  • 1
mlishn
  • 1,689
  • 14
  • 19