Page 1 of 1

How the script is called affects the output???

Posted: Tue Sep 16, 2003 6:32 am
by jasonmcc
I'm working on a small PHP utility to enable easy editing of a javascript based menu system. The menu is stored in a file on the server. The utility reads the menu, alters it in some way, and write the result back to the menu file.

Using the functions that I have written (which use regular expressions and the preg family of functions), inserting a top level menu to be second in the list, the code would look like this: (note: I've ommitted most error checking for the sake of brevity)

Code: Select all

$filename="menus/{$_POST["menu_to_edit"]}";
$menutext=read_menu_file($filename);

$newmenu=insert_menu_item($menutext,$_POST["new_menu_order"],$_POST["new_menu_name"],$_POST["new_link"],$image_file,$mouseover_image_file);
	
//$image_file & $mouseover_image_file have been added to from 
//the $_POST values to include the image page

if(!output_menu_file($newmenu,$filename))
  {
  site_error("Failed to update menu");
  }
else
  {
  site_ok("Menu updated");
  }
All the individual functions work and the menu items are created, changed or deleted correctly.

The utility is using the Smarty template engine (Don't know how I survived before I started using that :lol: ), and on the form where the data for the menu is entered, there are several buttons which call javascript functions via the onclick event.
e.g. The code for a new menu item(my smarty delimiters are {%% & %%} - I like them)

Code: Select all

function new_menuitem(menutype)
  {
  if (confirm('Are you sure you want to create a new '+menutype+' menu?'))
    {
    document.formsї"menueditform"].action="{%%$update_target%%}?action=new%20menu"
    document.formsї"menueditform"].submit()
    }
  
  }
Here is the problem, two menu items are inserted (at the correct place), but only one should have been. I've racked my brains going over the code, but it is all functioning correctly. I have pinned down the problem to how the script with the above PHP code is called.

If I replace the $_POST values with static values, fill out the form & submit (so the values from the form have no effect), the problem still occurs.

With the static values still in place, if I type out the URL: http://my_server/menutest/update_menu.php?action=new%20menu , (rather than submitting the form), then everything works correctly. Only one menu item is inserted into the file.

Is that strange or what? :cry:

Obviously, some of you guys would want to see other bits of code in order to comment, but I didn't want to overface everyone with loads of code straight away.

Does anyone have any ideas?? This wasn't a big utility - and at this rate it's easier to edit the menu manually (but for the intended users, that really isn't an option), but I'm starting to loose the will to live..

Any help is greatly appreaciated.

Jason.

Posted: Tue Sep 16, 2003 2:33 pm
by m3rajk
have you tried checking WHERE the values are coming from? track that down. that's likely the source.

Posted: Wed Sep 17, 2003 2:36 am
by jasonmcc
HI,

If I understand your statement, the values that you refer to would be the parameters that are used to create the menuitem (from the $_POST array).

These are submitted from a form. I have checked all the values that are submitted, and all are correct. This is borne out by the fact when ignoring the posted values and using hard coded ones, the effect is the same.

The only way (at the moment), of ensuring that the correct result is obtained is to call the script directly rather than it being the action of a form. Obviously this is not a solution as I can't then pass in the parameters from the form. :?

What is even stranger, if I echo out the contents of $newmenu before I write it to the file, the contents are correct. If I echo out $newmenu after I write it to the file, the contents are correct.

However, either using the newly changed menu, or just looking at the file reveals TWO new menu items where there should only be one - that's the weird part.

The code I'm using the write the file is pretty standard stuff

Code: Select all

function output_menu_file($newmenutext,$filename)
{
$fp=fopen($filename,'w'); //will overwite if exists or create if not
$written=fwrite($fp,$newmenutext);
fclose($fp);
if(!$written) //zero bytes or false - doesn't matter - still a fail
  {
  return false;
  }
else
  {
  return true;
  }
}
//note: to do - write to a temp file, verify the write then copy to the menu file.