Jump to content

Warning message first time I using the php script


etxnreg

Recommended Posts

Hi,

I have  create a script that remember variable from a number of html page and then use these in the last page.

The problem I have is when I start the script the first time, I got a warning message when I push the next button.

If I use the back button and than the next button again the problem has disappeared.

Please need help to solve this.

 

Thanks Niklas

 

Following warning occur:

 

Warning: array_merge() [function.array-merge]: Argument #1 is not an array in /media/disk/php/mpbn70/interface.php on line 5

 

Warning: array_merge() [function.array-merge]: Argument #1 is not an array in /media/disk/php/mpbn70/interface.php on line 13

 

start page:

<?php
session_start();
if (!empty($_POST)) {
    if (!isset($_POST['wizard_finished'])) { // preferably only present on the last step
        $_SESSION['_POST'] = array_merge($_SESSION['_POST'], $_POST);
    } else {
        //wizard finished
        $_POST = $_SESSION['_POST'];
        //process as if it were one-page request
    }
}

# $_POST = array_merge($_SESSION['_POST'] /*stored values*/, $_POST /*post values from the last page*/);
?>



<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>Untitled</title>
...
...
...

 

Next page:


<?php
session_start();
if (!empty($_POST)) {
    if (!isset($_POST['wizard_finished'])) { // preferably only present on the last step
        $_SESSION['_POST'] = array_merge($_SESSION['_POST'], $_POST);
    } else {
        //wizard finished
        $_POST = $_SESSION['_POST'];
        //process as if it were one-page request
    }
}

$_POST = array_merge($_SESSION['_POST'] /*stored values*/, $_POST /*post values from the last page*/);
?>

 

 

 

 

Warning: array_merge() [function.array-merge]: Argument #1 is not an array in /media/disk/php/mpbn70/interface.php on line 5

 

line 5: $_SESSION['_POST'] = array_merge($_SESSION['_POST'], $_POST);

 

argument #1 for function array_merge is $_SESSION['_POST']. I would guess that on the first run, $_SESSION['_POST'] is not set, or does not exist and hence "is not an array" when the function is being called. If you are pressing "Next" button, the server would be setting $_SESSION['_POST'] in the session variables, so then pressing "Back" and "Next" again would not yield the error as $_SESSION['_POST'] is now set and _is_ an array.

 

That's my guess.

yes. verify the existance of $_SESSION['_POST'] with isset():

 

if (isset($_SESSION['_POST']))
{
   $_SESSION['_POST'] = array_merge($_SESSION['_POST'], $_POST); // merge $_SESSION['_POST'] with $_POST
}
else
{
   $_SESSION['_POST'] = $_POST; // no need to merge $_SESSION['_POST'] with $_POST as $_SESSION['_POST'] does not even exist here! so set $_SESSION['_POST'] to equal $_POST.
}

Thanks a lot,

I am very new is this area.

Stupid question, should I add these new script lines before my old ones?

 

BR Niklas

<?php
session_start();
if (isset($_SESSION['_POST']))
{
   $_SESSION['_POST'] = array_merge($_SESSION['_POST'], $_POST); // merge $_SESSION['_POST'] with $_POST
}
else
{
   $_SESSION['_POST'] = $_POST; // no need to merge $_SESSION['_POST'] with $_POST as $_SESSION['_POST'] does not even exist here! so set $_SESSION['_POST'] to equal $_POST.
}


if (!empty($_POST)) {
    if (!isset($_POST['wizard_finished'])) { // preferably only present on the last step
        $_SESSION['_POST'] = array_merge($_SESSION['_POST'], $_POST);
    } else {
        //wizard finished
        $_POST = $_SESSION['_POST'];
        //process as if it were one-page request
    }
}

?>

 

 

no, it needs to become part of the nest of if() conditions. place it here:

 

<?php
session_start();

if (!empty($_POST)) {
    if (!isset($_POST['wizard_finished'])) { // preferably only present on the last step
// insert the new lines here
       if (isset($_SESSION['_POST']))
       {
         $_SESSION['_POST'] = array_merge($_SESSION['_POST'], $_POST); // merge $_SESSION['_POST'] with $_POST
       }
       else
       {
         $_SESSION['_POST'] = $_POST; // no need to merge $_SESSION['_POST'] with $_POST as $_SESSION['_POST'] does not even exist here! so set $_SESSION['_POST'] to equal $_POST.
       }
// end of new lines.
    } else {
        //wizard finished
        $_POST = $_SESSION['_POST'];
        //process as if it were one-page request
    }
}

?>

 

The if() { } blocks are building a logical system to decide what action the script should follow, based on what data is available.

 

Also note that if $_POST is empty, your script will do nothing. Not even output something to tell you $_POST is empty.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.