Jump to content

Recommended Posts

Hello

I am trying to loop through the $_POST, find a key and change its value in $_POST.  Hoping to later see the changed value elsewhere.  It is not changing with the snippet below.  I believe I read something about having to refernece it first.  Can someone help?

 

while(list($key, $value) = each($_POST)){

if ($key=="number") { $value =$value*100;}

}

tia

 

Link to comment
https://forums.phpfreaks.com/topic/47591-solved-change-_post-value/
Share on other sites

Note: Unless the array is referenced, foreach operates on a copy of the specified array and not the array itself. Therefore, the array pointer is not modified as with the each() construct, and changes to the array element returned are not reflected in the original array. However, the internal pointer of the original array is advanced with the processing of the array. Assuming the foreach loop runs to completion, the array's internal pointer will be at the end of the array.

 

As of PHP 5, you can easily modify array's elements by preceding $value with &. This will assign reference instead of copying the value.

 

Using each(), I think you would have to do:

 

if ($key=="number") $_POST[$key] =$value*100;

so you're saying the alteration i made in the foreach loop only applies for the duration of the loop?

 

this would be effective?

<?php
        foreach($_POST as $key => $val){
                if($key == 'your_num'){ $_POST[$key] *= 100; }
        }
?>

Hello

Sorry for my obtuseness, however if I have ...?text=text&number=1 in the $_POST array, why doesn't this work:

foreach($_POST as $k => $v){

    if ($k=='number'){

      $_POST[$k] = $v*100;

      }

}

 

beginning minds want to know, grin. 

 

<?php

  foreach($_POST as $k => $v) {
    if ($k == 10) {
      $_POST[$k] = $v*100;
    }
  }

?>

those variables aren't stored in the $_POST array, they're stored in the $_GET array. although, i've never used $_GET in a foreach loop. i've always called on the variables individually like $_GET['text'] and $_GET['number']. you can try it in the foreach and see what happens.

Hi

Sorry I showed it as a get array, but I was thinking like an Action/servlet.  The variables are in a post, that comes from a simple html form with action post.  I still have the problem, if you excuse the syntax of description.  Is it something that I am doing wrong with searching for the key as $k=='number'  ? 

Hi All

Sorry it was a typo on my part.  Yes, of course, it works.  Thank you.

 

BTW, I am getting the response from a post sent by the php script into a array.  Is there a way to just pull out the key/value pairs from everything else in the response?  I have seen/extrapolate to syntax like this:

 

$response_vars  = (explode '&', $_response)

  $pairvalue = (explode '=', $_response_vars)

  foreach ($pairvalue as $k => $v){

      $data .= $key . '=' . urlencode($value) . '&';

    }

 

     

Hello

Thank you.  However I want to build up the parameter string of key/value pairs so when I set

curl_setopt($curl_handle, CURLOPT_URL, "https://mydomain.com/context/myAction.do?");

      curl_setopt($curl_handle, CURLOPT_POSTFIELDS, $data);

        $response_2nd = curl_exec ($curl_handle) or die ("There has been an error connecting");

 

it will execute the well-formed full url and post it.

can you explain some of that syntax?  tia.

 

Hello

Thank you.  However I want to build up the parameter string of key/value pairs so when I set

curl_setopt($curl_handle, CURLOPT_URL, "https://mydomain.com/context/myAction.do?");

      curl_setopt($curl_handle, CURLOPT_POSTFIELDS, $data);

        $response_2nd = curl_exec ($curl_handle) or die ("There has been an error connecting");

 

it will execute the well-formed full url and post it.

can you explain some of that syntax?  tia.

 

 

click 'Topic Solved' on this thread and start a new thread, please.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.