Page 1 of 1

copy an array to $_POST

Posted: Tue Nov 09, 2004 10:18 am
by primate
I'm trying to copy an array called $pages to $_POST so the values exist after the form has been submitted, I'm sure I've done this before but I just can't get it to work.

Code: Select all

<?php
if (isset($_POST['delete_page'])){     



	$query="DELETE FROM Navigation
				WHERE (PageID = '{$_POST['page']}')";  //delete page
				
	mssql_query($query);

	

	
$query="SELECT PageID, PageName, Position FROM Navigation";
$results=mssql_query($query);
while ($query_result=mssql_fetch_array($results)){

	if (isset($pages)){
		
			$pages[$n]['PageID'] = $query_result['PageID'];
			$pages[$n]['PageName'] = $query_result['PageName'];
			$pages[$n]['Postion'] = $query_result['Position'];
			$n++;
			
				} else {
				
					$n=0;
					$pages = array ( $n => array ('PageID' => $query_result['PageID'], 'PageName' => $query_result['PageName'], 'Position' => $query_result['Position']));
					$n++;
					
					}
			}

$n=count($pages);

?>
<p><strong>Delete a Top Level page</strong></p>
<form action="delete_top_level_page.php" method="post" name="delete_top_level_page">
<table width="50%" border="1">
<td>Which Page do you want to delete?</td>
<td><select name="page">
<?php 

for ($x=0; $x<$n; $x++){
	
		print '<option value="' . $pages[$x]['PageID'] . '">' . $pages[$x]['PageName'] . '</option>';
		
		}
		
?>
</select></td></tr></table>
<p><input type="submit" name="delete_page" value="Delete Page" /></p>
</form>
<?php	
$_POST['pages'] = $pages;

	} else {  //repeats form for first input


?>
Any suggestions?

Posted: Tue Nov 09, 2004 10:33 am
by snicolas
why don't you add a hidden tag to your form with the page value?
<input type="hidden" name="pages" value="<?php echo $pages?>">

s.

Posted: Tue Nov 09, 2004 1:19 pm
by rehfeld
$_POST does NOT carry its data from page to page

you must be thinking of $_SESSION

http://php.net/session


unless, you just want the variable $pages to be used at some point later during the scripts execution.

could you explain what your doing in more detail?

Posted: Wed Nov 10, 2004 7:23 am
by primate
$_POST passes its values to whatever page you specify in the <form> tag's action attribute doesn't it? And thats where I'm trying to retrieve $pages from. So why can't I manually add a value to $_POST?

Posted: Wed Nov 10, 2004 1:56 pm
by rehfeld
because the POST array is created at the beggining of script execution, and will not hold any values after the script stops.


post will contain the info from a form, that was sent by a USER. so if you do not specifically add the info to the form, the user cannot send it to you again. just adding it to the _POST array does not give the info back to the user.

you would need to do as snicolas said, hide the values in the next form, in hidden fileds most likely.

maybe this will help

Code: Select all

// page 1

&lt;form mehtod="post" action="page2.php"&gt;
&lt;input type="text" name="firstname"&gt;firstname&lt;br&gt;
&lt;input type="text" name="lastname"&gt;lastname&lt;br&gt;
&lt;input type="submit"&gt;
&lt;/form&gt;

Code: Select all

&lt;?php

$hidden_fields = '';

foreach ($_POST as $key =&gt; $value) {
    $hidden_fields .= "&lt;input type="hidden" name="$key" value="$value"&gt;\n";
}

?&gt;


&lt;form mehtod="post" action="page3.php"&gt;

&lt;?php echo $hidden_fields; ?&gt;
&lt;input type="text" name="address"&gt;address&lt;br&gt;
&lt;input type="submit"&gt;
&lt;/form&gt;
now page 3 will receive the info from the first form, and the second form. if you did not add the hidden fields to the second form, page3 will only receive the "address" field. just because something is in the _POST array does not automatically give it back to the user, you need to do it manuall like this.

if your trying to pass data from the sql query, youll need to do the same, store it in hidden fields/

theres still problems though, if the user hits the back button on one of the pages, they are likely to lose some/all the info. hence why i suggested you learn how to use sessions.

if thats not what you meant, you need to tell us in detail what your trying to do. theres many solutions, and it depends on what exactly your trying to acheive.

Posted: Thu Nov 11, 2004 3:22 am
by primate
Thanks, thats cleared it up, I have used sessions before so thats not a problem, I just thought I could keep it simple and add stuff to the $_POST array, but clearly not :)