Author Topic: Warning message first time I using the php script  (Read 346 times)

0 Members and 1 Guest are viewing this topic.

Offline etxnregTopic starter

  • Irregular
  • Posts: 11
    • View Profile
Warning message first time I using the php script
« on: March 16, 2010, 07:06:33 AM »
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*/);
?>





Offline Catfish

  • Enthusiast
  • Posts: 352
    • View Profile
    • beardedDonkey
Re: Warning message first time I using the php script
« Reply #1 on: March 16, 2010, 07:19:41 AM »
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.
This signature is not a massive image that detracts from the forum's purpose.
Standard Disclaimer - Any code or help I post is 'as is'. I don't know if my code will work or even if it's correct. It's free isn't it?
My PHP Tutorials

Offline etxnregTopic starter

  • Irregular
  • Posts: 11
    • View Profile
Re: Warning message first time I using the php script
« Reply #2 on: March 17, 2010, 04:08:27 AM »
Hi,
Thanks for the response.
Have you any idea how I can change the code, so this problem not occur?

BR Niklas

Offline Catfish

  • Enthusiast
  • Posts: 352
    • View Profile
    • beardedDonkey
Re: Warning message first time I using the php script
« Reply #3 on: March 17, 2010, 06:20:20 AM »
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.
}
This signature is not a massive image that detracts from the forum's purpose.
Standard Disclaimer - Any code or help I post is 'as is'. I don't know if my code will work or even if it's correct. It's free isn't it?
My PHP Tutorials

Offline etxnregTopic starter

  • Irregular
  • Posts: 11
    • View Profile
Re: Warning message first time I using the php script
« Reply #4 on: March 17, 2010, 01:18:53 PM »
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
    
}
}

?>




Offline Catfish

  • Enthusiast
  • Posts: 352
    • View Profile
    • beardedDonkey
Re: Warning message first time I using the php script
« Reply #5 on: March 17, 2010, 01:32:03 PM »
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.
« Last Edit: March 17, 2010, 01:35:46 PM by Catfish »
This signature is not a massive image that detracts from the forum's purpose.
Standard Disclaimer - Any code or help I post is 'as is'. I don't know if my code will work or even if it's correct. It's free isn't it?
My PHP Tutorials