0

I have an array of data sent by jquery ajax to a php service:

  requestAjax = jQuery.ajax({
            type: "POST",
            url: "ajax/ajax.salva-valutazione.php",
            data: {formdata:formdata, arrAccessori: arrAccessori},
        //  data:formdata,
            dataType: "json",
            success: function(risposta){
               alert(".."+risposta);
             }
    });

In the php response page I use, if I do a print_r of all data:

 error_log(print_r($arrValutazione, TRUE) );

I get in error log:

  [formdata] => nome=John&cognome=Doe&indirizzo=My+address&citta=London&user_name=fedepupo&clienteID=1

While if I do

 error_log(print_r($arrValutazione['formdata'], TRUE) );

I obtain

nome=John&cognome=Doe&indirizzo=My+address&citta=London&user_name=fedepupo&clienteID=1

In error log.

My problem is how direcly accessing nome, cognome (...) values because if I try doing

 error_log(print_r($arrValutazione['formdata']['cognome'], TRUE) );

I get 'n', and also with

error_log(print_r($arrValutazione['formdata'][0]['cognome'], TRUE) );

I get the same value 'n'.

Any suggestions?

3 Answers 3

2

In error log.

My problem is how direcly accessing nome, cognome (...) values because if I try doing

error_log(print_r($arrValutazione['formdata']['cognome'], TRUE) );

I get 'n', and also with

error_log(print_r($arrValutazione['formdata'][0]['cognome'], TRUE) );

I get the same value 'n'.

$arrValutazione['formdata'] =
nome=John&cognome=Doe&indirizzo=My+address&citta=London&user_name=fedepupo&clienteID=1

Reason is you are accessing string not array, so you get always n

Here is demo

$ php -r '$string="abcdefgh"; 
  echo $string[0].PHP_EOL;
  echo $string[2].PHP_EOL; 
  echo $string["unknown_index"].PHP_EOL;'
a
c
PHP Warning:  Illegal string offset 'unknown_index' in Command line code on line 4
a
Sign up to request clarification or add additional context in comments.

Comments

1

Use below code to get value of each variable

parse_str($arrValutazione['formdata'],$output);
print_r($output);
$nome = $output['nome'];
$cogname = $output['cogname'];

Similarily you can fetch any variable detail.

Comments

1

Please try the below code,

parse_str($arrValutazione['formdata'],$formdata);
error_log(print_r($formdata['cognome'], TRUE));

Here is the reference link, http://php.net/manual/en/function.parse-str.php

1 Comment

As rightly said by @AkshayHegde in his answer, the reason for your issue was that you are not accessing an array but a string.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.