etxnreg Posted March 16, 2010 Share Posted March 16, 2010 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*/); ?> Link to comment https://forums.phpfreaks.com/topic/195419-warning-message-first-time-i-using-the-php-script/ Share on other sites More sharing options...
Catfish Posted March 16, 2010 Share Posted March 16, 2010 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. Link to comment https://forums.phpfreaks.com/topic/195419-warning-message-first-time-i-using-the-php-script/#findComment-1026900 Share on other sites More sharing options...
etxnreg Posted March 17, 2010 Author Share Posted March 17, 2010 Hi, Thanks for the response. Have you any idea how I can change the code, so this problem not occur? BR Niklas Link to comment https://forums.phpfreaks.com/topic/195419-warning-message-first-time-i-using-the-php-script/#findComment-1027467 Share on other sites More sharing options...
Catfish Posted March 17, 2010 Share Posted March 17, 2010 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. } Link to comment https://forums.phpfreaks.com/topic/195419-warning-message-first-time-i-using-the-php-script/#findComment-1027500 Share on other sites More sharing options...
etxnreg Posted March 17, 2010 Author Share Posted March 17, 2010 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 } } ?> Link to comment https://forums.phpfreaks.com/topic/195419-warning-message-first-time-i-using-the-php-script/#findComment-1027728 Share on other sites More sharing options...
Catfish Posted March 17, 2010 Share Posted March 17, 2010 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. Link to comment https://forums.phpfreaks.com/topic/195419-warning-message-first-time-i-using-the-php-script/#findComment-1027732 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.