?
Solved

I need to substitute some values within the _POST array

Posted on 2008-11-16
16
Medium Priority
?
242 Views
Last Modified: 2013-12-13
I need to substitute some values within the _POST array.

When using GET, the URL looks like this. It's encoded.

http://localhost/newnew/list.php?FilterField%5B1%5D=designs.area_id_fk&FilterOperator%5B1%5D=like&FilterValue%5B1%5D=3&FilterAnd%5B2%5D=and&FilterField%5B2%5D=designs.design_style&FilterOperator%5B2%5D=like&FilterValue%5B2%5D=Detached&FilterAnd%5B3%5D=and&FilterField%5B3%5D=designs.beds&FilterOperator%5B3%5D=like&FilterValue%5B3%5D=4&FilterAnd%5B4%5D=and&FilterField%5B4%5D=designs.design_price&FilterOperator%5B4%5D=%3E%3D&FilterValue%5B4%5D=320000&FilterAnd%5B5%5D=and&FilterField%5B5%5D=designs.design_price&FilterOperator%5B5%5D=%3C%3D&FilterValue%5B5%5D=340000&FilterAnd%5B6%5D=and&FilterField%5B6%5D=designs.show_design&FilterOperator%5B6%5D=like&FilterValue%5B6%5D=yes

Unencoded, it looks like this:

http://localhost/newnew/list.php?
FilterField[1]=designs.area_id_fk
&FilterOperator[1]=like
&FilterValue[1]=3
&FilterAnd[2]=and
&FilterField[2]=designs.design_style
&FilterOperator[2]=like
&FilterValue[2]=Detached
&FilterAnd[3]=and
&FilterField[3]=designs.beds
&FilterOperator[3]=like
&FilterValue[3]=4
&FilterAnd[4]=and
&FilterField[4]=designs.design_price
&FilterOperator[4]=>=
&FilterValue[4]=320000
&FilterAnd[5]=and
&FilterField[5]=designs.design_price
&FilterOperator[5]=<=
&FilterValue[5]=340000
&FilterAnd[6]=and
&FilterField[6]=designs.show_design
&FilterOperator[6]=like
&FilterValue[6]=yes

After a conditional statement, I need to change the values for several keys>>values, add one more pair,and then refresh the page with the new _POST array.

The keys values are as follows:

&FilterField[4]=designs.design_price
&FilterOperator[4]=<=
&FilterValue[4]=1000000
&FilterAnd[5]=and
&FilterField[5]=designs.design_price
&FilterOperator[5]=<=
&FilterValue[5]=1000000
&shift=1

What would the PHP syntax look like?
0
Comment
Question by:ggjones
  • 9
  • 7
16 Comments
 
LVL 27

Expert Comment

by:ddrudik
ID: 22972730
Not sure I understand your mention of the POST array as I don't see your array, instead I parsed the URL as shown.  If you have different input you want to manipulate (i.e. an array, please shown the array output with var_export).
<pre>
<?php
$url='http://localhost/newnew/list.php?FilterField%5B1%5D=designs.area_id_fk&FilterOperator%5B1%5D=like&FilterValue%5B1%5D=3&FilterAnd%5B2%5D=and&FilterField%5B2%5D=designs.design_style&FilterOperator%5B2%5D=like&FilterValue%5B2%5D=Detached&FilterAnd%5B3%5D=and&FilterField%5B3%5D=designs.beds&FilterOperator%5B3%5D=like&FilterValue%5B3%5D=4&FilterAnd%5B4%5D=and&FilterField%5B4%5D=designs.design_price&FilterOperator%5B4%5D=%3E%3D&FilterValue%5B4%5D=320000&FilterAnd%5B5%5D=and&FilterField%5B5%5D=designs.design_price&FilterOperator%5B5%5D=%3C%3D&FilterValue%5B5%5D=340000&FilterAnd%5B6%5D=and&FilterField%5B6%5D=designs.show_design&FilterOperator%5B6%5D=like&FilterValue%5B6%5D=yes';
$url=parse_url($url);
preg_match_all('/[?&]([^=]*)=([^?&]*)/',urldecode($url['query']),$matches);
$matches=array_combine($matches[1],$matches[2]);
$matches['FilterField[4]']='designs.design_price';
$matches['FilterOperator[4]']='<=';
$matches['FilterValue[4]']=1000000;
$matches['FilterAnd[5]']='and';
$matches['FilterField[5]']='designs.design_price';
$matches['FilterOperator[5]']='<=';
$matches['FilterValue[5]']=1000000;
$matches['shift']=1;
$query='';
foreach($matches as $key=>$val){
	$key=urlencode($key);
	$val=urlencode($val);
	$query.="$key=$val&";
}
$url=$url['scheme'].'://'.$url['host'].$url['path'].'?'.substr($query,0,-1);
echo $url;
?>

Open in new window

0
 

Author Comment

by:ggjones
ID: 22973185
My apologies. The url is the output from a form submitted using the POST method. For illustrative purposes, I switched POST for GET in this example. So your $url var on the first line needs to reflect the POST variable pairs, rather than simply a url. If need be, I can then refresh the page after the for-loop, thereby resubmitting the query, using the GET method, (although POST would be preferable, if there was a way to do it).

regards,

G.J.
0
 
LVL 27

Expert Comment

by:ddrudik
ID: 22973321
If this is an array question in order to assist further I would need to see the relevant array that you have now and that you want after processing.

add this line to your script:
var_export($_POST);

And paste that into a comment here.

I guess I don't understand the issue so maybe you could explain further.  It sounds like you just want to assign certain $_POST keys new value and reload the page.
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 

Author Comment

by:ggjones
ID: 22974949
Thanks for the input. Yes, you are correct;

I  want to assign certain $_POST keys new values  .... and ADD another key>>value (shift=1) ...  and reload the page.

The headers are already sent, so I'm assuming something like this  to reload.:

<meta http-equiv="Refresh" content="0;url='$url' />
... UNLESS there is a way to use the POST method on reload

thanks,

GJ.

array (
  'FilterField' => 
  array (
    1 => 'designs.area_id_fk',
    2 => 'designs.design_style',
    3 => 'designs.beds',
    4 => 'designs.design_price',
    5 => 'designs.design_price',
    6 => 'designs.show_design',
  ),
  'FilterOperator' => 
  array (
    1 => 'like',
    2 => 'like',
    3 => 'like',
    4 => '>=',
    5 => '<=',
    6 => 'like',
  ),
  'FilterValue' => 
  array (
    1 => '4',
    2 => 'Detached',
    3 => '3',
    4 => '300000',
    5 => '320000',
    6 => 'yes',
  ),
  'FilterAnd' => 
  array (
    2 => 'and',
    3 => 'and',
    4 => 'and',
    5 => 'and',
    6 => 'and',
  ),
)

Open in new window

0
 
LVL 27

Expert Comment

by:ddrudik
ID: 22975898
I would imagine it's just assigning the new values with $_POST['FilterField'][4]='newvalue'; etc. and refresh the page with meta tag or js if you need the send the header first.
0
 

Author Comment

by:ggjones
ID: 22976103
... Ho ho... "it's just"!

The problem is more than simply "assigning the new values" (it seems to me):

When I refresh the page, the POST array is cleared. It seems to me therefore, that I need to capture and store the Post array in a form that I can parse, then substitute/add keys and values, and then Post the new, stored array in some way.  And I have little idea of how to accomplish this.
0
 
LVL 27

Expert Comment

by:ddrudik
ID: 22976124
To take that a step further, upon refresh your user will get a security message about sending data again.  Possibly you should consider another option for this data, such as a $_SESSION var instead.
0
 

Author Comment

by:ggjones
ID: 22976322

... ah... but the point is that I'm NOT sending data "again", since this is a "new" post array. Nonetheless, temporarilly storing the Post array in a Session var, parsing this new var, making the changes and then Posting this new var when refreshing the page sounds like a sensible approach. But this is at the periphery of my expertise.

thanks,

G.J.
0
 
LVL 27

Expert Comment

by:ddrudik
ID: 22976648
I guess I don't follow what you are trying to accomplish with modifying the $_POST data, have you presented the HTML page to the user when you want to resubmit the page to the user?  Maybe you should explain further and possibly provide a code example (of at least how you want it to work).  If you were using parameters in the URL instead of $_POST it would seem to make this process easier, also if you didn't send the headers to the browser before this was processed that would help as well.
0
 

Author Comment

by:ggjones
ID: 22976825


I'm using a framework, and for a variety of reasons cannot modify the SELECT statement except through these means.
Simply put, the page loads, and is populated based on POST variables/values.
If there are no results, I then need to modify the price parameters within the POST array, and resubmit the page with the new values, the other variables and their original values, and the new variables/values. As a last resort, the resubmission can use the GET method.
0
 
LVL 27

Expert Comment

by:ddrudik
ID: 22976849
Do you need to display the page with no results to the user?  If so, that will make redirecting with PHP not possible and redirecting POST with JS will issue a security dialog in the browser, consider this example:
<?php
if(!empty($_POST)){
	echo '<pre><hr>'.print_r($_POST,true).'<hr>';
	$_POST['text1']='newvalue';
	echo print_r($_POST,true).'<hr></pre>';
}
?>
<html>
<head>
</head>
<body>
<form name="form1" action="<?php echo$_SERVER['PHP_SELF']; ?>" enctype="multipart/form-data" method="post">
<input name="text1" type="text" value="<?php if(isset($_POST['text1'])){ echo $_POST['text1']; } ?>"/>
<br /><input type="submit" name="submit" value="submit">&nbsp;<input type="button" name="reload" value="reload" onclick="location.reload();" />
<br /><br /><input type="button" name="clear" value="clear" onclick="location.href=('<?php echo$_SERVER['PHP_SELF']; ?>');" />
</form>
</body>
</html>

Open in new window

0
 
LVL 27

Expert Comment

by:ddrudik
ID: 22977070
$_POST vars are not saved when redirecting with location in PHP even if that was an option for you (attempted 307 temp redirect), searching for previous questions on this topic (there have been many) it seems the consensus is to populate a form and issue a javascript to cause it to submit or use cURL or other similar method to use GET vars instead.
0
 

Author Comment

by:ggjones
ID: 22977552

... yes, the value substitution appears to work using this method. I'm left with the need to then format the new POST array to convert it to a query string for the GET method, as below:
if(!empty($_POST)){
	$_POST['FilterValue'][4]='NEW VALUE';
 
	//substitute additional values, etc
 
	$_POST['shift']='1';
 
	// resubmit
 
	echo '<meta http-equiv="Refresh" content="0;url=URL?'NEW _ARRAY IN APPROPRIATE FORMAT" />';
}

Open in new window

0
 
LVL 27

Accepted Solution

by:
ddrudik earned 2000 total points
ID: 22977687
An example of that process:
<pre>
<?php
$_POST=array(
  'FilterField' => 
  array (
    1 => 'designs.area_id_fk',
    2 => 'designs.design_style',
    3 => 'designs.beds',
    4 => 'designs.design_price',
    5 => 'designs.design_price',
    6 => 'designs.show_design',
  ),
  'FilterOperator' => 
  array (
    1 => 'like',
    2 => 'like',
    3 => 'like',
    4 => '>=',
    5 => '<=',
    6 => 'like',
  ),
  'FilterValue' => 
  array (
    1 => '4',
    2 => 'Detached',
    3 => '3',
    4 => '300000',
    5 => '320000',
    6 => 'yes',
  ),
  'FilterAnd' => 
  array (
    2 => 'and',
    3 => 'and',
    4 => 'and',
    5 => 'and',
    6 => 'and',
  ),
);
$query='';
foreach($_POST as $key=>$subarray){
	foreach($subarray as $subkey=>$subval){
		$newkey=urlencode($key."[".$subkey."]");
		$newval=urlencode($subval);
		$query.="$newkey=$newval&";
	}
}
$query=substr($query,0,-1);
echo $query;
?>

Open in new window

0
 

Author Comment

by:ggjones
ID: 22978089

... outstanding. Thank you. Final solution below.

G.J.
if(!empty($_POST)){
 
$query='';
 
	// modify existing values
	$_POST['FilterValue'][4]='1000000';
	$_POST['FilterValue'][5]='1000000';
	$_POST['FilterOperator'][4]='<=';
 
	// add new keys and values
	$_POST['shift'][1]='1';
 
foreach($_POST as $key=>$subarray){
	foreach($subarray as $subkey=>$subval){
		$newkey=urlencode($key."[".$subkey."]");
		$newval=urlencode($subval);
		$query.="$newkey=$newval&";
	}
}
$query=substr($query,0,-1);
//echo $query;
 
	// refresh page with modified POST array as a query string
echo '<meta http-equiv="Refresh" content="0;url=myPage.php?' . $query . '" />';
 
}

Open in new window

0
 
LVL 27

Expert Comment

by:ddrudik
ID: 22979014
Thanks for the question and the points.
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
There are times when I have encountered the need to decompress a response from a PHP request. This is how it's done, but you must have control of the request and you can set the Accept-Encoding header.
The viewer will learn how to dynamically set the form action using jQuery.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
Suggested Courses

571 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question