I need to substitute some values within the _POST array

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?
ggjonesAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

ddrudikCommented:
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
ggjonesAuthor Commented:
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
ddrudikCommented:
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
Introducing Cloud Class® training courses

Tech changes fast. You can learn faster. That’s why we’re bringing professional training courses to Experts Exchange. With a subscription, you can access all the Cloud Class® courses to expand your education, prep for certifications, and get top-notch instructions.

ggjonesAuthor Commented:
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
ddrudikCommented:
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
ggjonesAuthor Commented:
... 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
ddrudikCommented:
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
ggjonesAuthor Commented:

... 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
ddrudikCommented:
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
ggjonesAuthor Commented:


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
ddrudikCommented:
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
ddrudikCommented:
$_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
ggjonesAuthor Commented:

... 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
ddrudikCommented:
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
ggjonesAuthor Commented:

... 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
ddrudikCommented:
Thanks for the question and the points.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.