/    Sign up×
Community /Pin to ProfileBookmark

Variable passing problems

Hello guys,

I have a really frustrating problem? My script is something like that:

if (isset($_POST[‘addrecords’])) {
//DISPLAYING SOME CONFIRMATION, ADDING SOME COMMENTS ETC
redirect(script.php?action=show&data_id=”.$data_id.””);
}

if ($action == “edit”) {
echo “<form name=’myform’ method=’post’ enctype=’multipart/form-data’>”;
echo “<input type=’text’ name=’data_id’ value=’$data_id’>”;
echo “<input type=’text’ name=’data_name’ value=’$data_name’>
<input type=’submit’ name=addrecords’ value=’Save’ class=’button’>
</form>”;
}

if ($action == “add”) {
echo $_POST[‘data_id’];
echo $_
POST[‘data_name’];
//INSERTING $_POST[‘data_id’] and $_POST[‘data_name’] TO DATABASE
}

I first edit the form ($action == “edit”) then i’m redirected to $_POST[‘addrecords’] and then when I confirm some things, script redirects to $action == “add” where I insert the records to database.

The problem is when i try to insert do database values $_POST[‘data_id’] and $_POST[‘data_name’] i get nothing. What could be the problem.

Laura?

to post a comment
PHP

10 Comments(s)

Copy linkTweet thisAlerts:
@QuidamOct 09.2008 — I always use $_GET when I want to get a variable from the url
Copy linkTweet thisAlerts:
@laura81authorOct 09.2008 — Actually I cannot pass values via url, because there are tens of them and many of them are long sentences. But is it at all nesseecary within same file?

Laura
Copy linkTweet thisAlerts:
@laura81authorOct 10.2008 — Doesn't anybody have any hints ?
Copy linkTweet thisAlerts:
@UltimaterOct 10.2008 — $_POST will be lost during the redirect.

Try:

[code=php]
$_SESSION['post']=$_POST;
[/code]

then on the next page, do:
[code=php]
if(isset($_SESSION['post'])){
$_POST=$_SESSION['post'];
unset($_SESSION['post']);
}
[/code]


an alternative would be to store serialize($_POST) in the database or a text file then include the unique record id or file name as a parameter in your redirect.
Copy linkTweet thisAlerts:
@laura81authorOct 10.2008 — Hello Ultimater,

Could you be a little more precise....

I tried in one part of my file:

$_SESSION['data_id']=$_POST;

and another part

if(isset($_SESSION['data_id'])){

$_
POST=$_SESSION['data_id'];

unset($_
SESSION['data_id']);

}

If i echo $_SESSION['data_id'], i still get nothing ?

Laura
Copy linkTweet thisAlerts:
@UltimaterOct 10.2008 — [url=http://us3.php.net/manual/en/function.session-start.php]New to sessions?[/url] :rolleyes:
[code=php]
session_start();
[/code]

that line needs to appear in both files unless session.auto_start is set to 1 in your PHP config.
Copy linkTweet thisAlerts:
@laura81authorOct 10.2008 — Thank you so much, it works now?

Laura
Copy linkTweet thisAlerts:
@laura81authorOct 14.2008 — Hello,

I still have a problem. When I post value via $_FILES, i get no value. How should the code look like instead of the following when using $_FILES?


[code=php]$_SESSION['post']=$_POST;
[/code]

then on the next page, do:

[code=php]if(isset($_SESSION['post'])){
$_POST=$_SESSION['post'];
unset($_SESSION['post']);
} [/code]


Thank you in advance,

Laura?
Copy linkTweet thisAlerts:
@UltimaterOct 16.2008 — Well...You [i]could[/i] add $_FILES support yet after the redirect occurs, the files uploaded to the temporary directory will get [i]removed[/i] since uploaded files are stored in the temporary directory. Despite the uploaded files getting removed,

$_
FILES is an associate array containing nothing but information such as the whereabouts of the file more specifically where the file [b]used to be[/b] however the file will no longer exist when you try to open it from the next script.

A workaround would be to tell the client that the script finished so it redirects while PHP secretly continues running in the background so PHP doesn't terminate and clear the uploaded files from the temp dir before the next script runs and tries to open the file.

Also note that it is very important to add enctype="multipart/form-data" to the FORM lest $_FILES never get set in the first place from the initial INPUT TYPE="FILE" submit.

Here's an example:

Place both files in the same directory, run p.php, find an image and hit submit.

p.php:
[code=php]
<?php
if($_POST || $_FILES){
header("Connection: close");
session_start();
$_SESSION['files']=$_FILES;
session_write_close();
header("Location: j.php");
header("Content-Length: 0");//Key statement to tell the client that it is end of content
flush();//VERY IMPORTANT, flushes headers to client lest the client get stuck on the sleep
sleep(10);//amount of time for PHP to keep running without a client to care
exit;
}
?>
<form action="<?=$_SERVER['PHP_SELF']?>" method="post" enctype="multipart/form-data">
<input type="file" name="f">
<input type="submit">
</form>
[/code]


j.php:
[code=php]
<?php
session_start();
if(isset($_SESSION['files'])){
$_FILES=$_SESSION['files'];
unset($_SESSION['files']);
}

if(isset($_FILES) && isset($_FILES['f'])){
if(!file_exists($_FILES['f']['tmp_name'])){
echo '<span style="color:red;">Error:</span> Could not retrieve file from temporary upload directory! <br />';
echo '<span style="color:red;">File:</span> <em style="text-size:smaller;">'.htmlentities($_FILES['f']['name']).'</em>';
exit;
}
header("Content-Type: ".$_FILES['f']['type']);
echo file_get_contents($_FILES['f']['tmp_name']);
exit;
}
echo '<span style="color:red;">Error:</span> $_FILES was not set!';
?>
[/code]


Hope this workaround will suffice.
Copy linkTweet thisAlerts:
@laura81authorOct 17.2008 — Thank you Ultimator! It is more difficult than i thought, but i think i'll figure out something.

Laura?
×

Success!

Help @laura81 spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.19,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...