0

NEW INFORMATION:
I used the print_r function on the $_REQUEST and something very strange is happening there too. Some values are being correctly passed by the GET such as a value on another form which passes in "TRUE". This can be seen in the print_r output but isn't written to the file... Still no closer to finding a solution to my problem however.

I'm working on a page with a lot of forms which are loaded in as needed by AJAX. This all works fine as does parsing the name:value pairs and storing them appropriately.

My error happens when the PHP parses the GET request sent by AJAX when the user is finished, it only seems to retrieve the values from certain fields.

The idea is that the user can add data from any number of forms, which are then turned into a GET request and sent to the server.

The JavaScript is building my request perfectly and all forms are sent correctly.

Depending on the forms the user submits, the data is processed by a large switch statement which passes the relevant names to a variadic function which grabs the values, and creates a string for writing to a file.

The strange error is that only some values get written to the file with others only having a blank line. No error reported by Apache or PHP, no error reported in the JavaScript console either.

I'll use the Colour form for example as this is one of the more complex.

So I add a colour action and click the button to submit all forms (this time, it's just the colour form though)

My get request looks like this: actionIDs=Colour&coOptionSelect=Tinting&coColourEffect=Sepia&coRemoveColour=#000000&coRemoveFuzzNumber=0&coRemoveHueSelect=None&coReplaceColour=#000000&coReplaceFuzzNumber=0&coReplacementColour=#000000&coReplacementAlphaNumber=0&coReplaceHueSelect=None&coReplacementHueSelect=None

Next, the PHP parses the actionIDs part as sometimes, there will be many actions. This works fine.

We now jump to the Colour part of the switch statement.

case "Colour":
    $config = processAction("coOptionSelect", "coColourEffect", "coRemoveColour", "coRemoveFuzzNumber", "coRemoveHueSelect", "coReplaceColour", "coReplaceFuzzNumber", "coReplacementColour", "coReplacementAlphaNumber", "coReplaceHueSelect", "coReplacementHueSelect");
    file_put_contents($confpath . "colour.conf", $config);
    break;

That writes to the correct file, but strangely, only coOptionsSelect and coColourEffect have their values written to the file. It isn't their input type as they are select statements similar to the other selects on the form. On other forms, it may be a number input or a text input that submits properly instead.

It isn't random either, the same ones will always write out properly. It also isn't positional as I moved around the values and it's still the same ones that write correctly, their position doesn't affect anything.

Finally here is processAction function.

function processAction()
{
    $config = "";

    foreach(func_get_args() as $field)
    {
        $temp = isset($_REQUEST[$field]) ? $_REQUEST[$field] : null;
        $config = $config . $temp . "\n";
    }

     return $config;
}

The end result should be all values should write to their relevant files correctly, rather than the current issue where only a few values from each form are written, with the rest of the values being written as blank lines.

Arcana
  • 239
  • 5
  • 13
  • In simple words please specify what you have tried and what you want to do/what is expected result. As this long story some time left unanswered.. – Naresh Oct 05 '14 at 18:50
  • @PuzzledBoy It is fairly long but with no error logs firing, I can't seem to pinpoint exactly where the error could be. My end goal is that the values from a large a GET request sent by AJAX are processed and written to relevant files. My problem: only some values are written with seemingly no logic as to why. I've tried looking for various error logs and writing out variable values as well as trying to break down the logic. No luck. – Arcana Oct 05 '14 at 18:54

1 Answers1

1

You probably need to encode your # sign to a encoded method %23

you could also use urlencode to do it before passing it to your variable.

Reference: http://php.net/manual/en/function.urlencode.php


Update:

If you are going to try to encode through javascript I would try and use this method

var newURL = 
   "http://example.com/index.php?url=" + encodeURIComponent(actionIDs);

or

var newURL = 
   "http://example.com/index.php?url=" + escape(actionIDs);

Reference: Encode URL in JavaScript?

You have three options:

  • escape() will not encode: @*/+

  • encodeURI() will not encode: ~!@#$&*()=:/,;?+'

  • encodeURIComponent() will not encode: ~!*()'

But in your case, if you want to pass a URL into a GET parameter of other page, you should use escape or encodeURIComponent, but not encodeURI.

See Stack Overflow question Best practice: escape, or encodeURI / encodeURIComponent for further discussion.

Community
  • 1
  • 1
Robert Lee
  • 1,541
  • 1
  • 10
  • 20
  • This has helped so thank you! Now values 3, 4, and 5 are also being written correctly. Then it stops again though... I implemented it by using JavaScript to perform a string replacement on the GET string I make replacing "#" with "%23" It has helped but it stops again... – Arcana Oct 05 '14 at 19:14
  • Have you tried to add the encode method with javascript instead of just fixing the # symbol? I updated my answer – Robert Lee Oct 05 '14 at 20:36
  • I've used escape and it's all passed fine to the PHP now, as shown by the print_r output. I need to do something however to allow me to process this however as it just seems to be string data now. EDIT: This should be fine for me though, because my problem was getting the data to the PHP, as long as I can parse it in someway, that solves my problem! – Arcana Oct 05 '14 at 21:26
  • look into this http://php.net/manual/en/function.json-decode.php to help you decode what you encoded. Might have to use this too http://php.net/manual/en/function.urldecode.php – Robert Lee Oct 05 '14 at 21:38
  • I've used urldecode and now have the string perfectly ready for working with. I can now obtain all the values I need and they work as intended. Thank you very much for your help! It's saved me many hours of stress! – Arcana Oct 05 '14 at 22:05