2

my problem is that i use the special character & as an key and this seems not to work

my array is like this

$legalforms = array(
    'GmbH & Co.KG' => array(
            'namesToSubmit' =>array(
                'companyName'=>'required',
                'location'=>'required', 
                'phone'=>null,
                'fax'=>null,
                'web'=>null,
                'registryCourt'=>'required',
                'registryNumber'=>'required',
                'companyNameAssociate'=>'required',
                'locationAssociate'=>'required',
                'registryCourtAssociate'=>'required',
                'registryNumberAssociate'=>'requuired',
                'ceo'=>'required'
            ),
       )
)

and when i want to use the namesToSubmit i get an error that property of nameToSubmit is null, if i remove the special character & out of it , it works.. so how can i get it to work with the & ?

edit:

$("#sendLegalForm").click(function () { 
         selection = $('#selection').val();
         $.ajax({
             type:'GET',
             url:'http://192.168.10.24/php/sig.php?selectedLegalform='+ selection,
             dataType:'json',
             success: function (data){  

                 $("#legalform").hide();
                 $("#fields").show();
                 var fieldnames =[];
                 for(property in data.namesToSubmit){
                    fieldnames.push(property);
                 }
                 var fields=[];
                 for(var i=0; i<data.textfieldHeaders.length; i++){               
                 fields.push(data.textfieldHeaders[i],'<br>','<input name="',fieldnames[i],'" type="text"                                                       ',data.namesToSubmit[fieldnames[i]] == "required"?"required":"",'>','<br/>');        
                 }                

                 fields.push("<br>", 'Pflichtfelder (*)');
                 $("#fieldsForm").prepend(fields.join(''));              
             },
             error: function(jqXHR,textStatus,errorThrown){
                console.log(jqXHR);
                console.log(textStatus);
                console.log(errorThrown);
             }
         });
     });

and i get the error in this line

for(property in data.namesToSubmit){

tried the md5 , didn´t work , but thanks for all your help

Adrian
  • 57
  • 1
  • 8

3 Answers3

3

From the PHP Manual:

The key can either be an integer or a string. The value can be of any type.

Additionally the following key casts will occur:

  • Strings containing valid integers will be cast to the integer type. E.g. the key "8" will actually be stored under 8. On the other hand "08" will not be cast, as it isn't a valid decimal integer.
  • Floats are also cast to integers, which means that the fractional part will be truncated. E.g. the key 8.7 will actually be stored under 8.
  • Bools are cast to integers, too, i.e. the key true will actually be stored under 1 and the key false under 0.
  • Null will be cast to the empty string, i.e. the key null will actually be stored under "".
  • Arrays and objects can not be used as keys. Doing so will result in a warning: Illegal offset type.

So, instead of using & in your array key, you can use str_replace() to replace it with an allowed character, and once the array is declared, you can replace it back with &.

An alternative would be to use md5(), as suggested here:

md5('GmbH & Co.KG' => array( ...

Hope this helps!

Community
  • 1
  • 1
Amal Murali
  • 75,622
  • 18
  • 128
  • 150
2

Change the request method to POST:

$.ajax({
    type:'POST',
    url: 'http://192.168.10.24/php/sig.php',
    data: {selectedLegalform: selection},
    ...

In PHP:

$data = $_POST['selectedLegalform'];

If you send the string GmbH & Co.KG via GET it becomes GmbH%20%26%20Co%2EKG. That should be the problem.

bitWorking
  • 12,485
  • 1
  • 32
  • 38
1

You may replace the '&' with '-and-' in array and replacing back again when using an array. Hope it will help. I normally do it .

for replace, using str_replace()

LF00
  • 27,015
  • 29
  • 156
  • 295
M Shahzad Khan
  • 935
  • 1
  • 9
  • 22