Joachim Vanthuyne
Joachim Vanthuyne

Reputation: 355

PHP json_encode , array not showing correct in javascript

I have this array, that I want to parse into javascript.

$offerText[] =

(
[0] => Name
[1] => Adres
[2] => ZIP
[3] => CITY
[4] => E-mail
)

I am using this code to do so:

$html .= '<td class="offerteCell"><a href="#" onclick="return addToOffer('.json_encode($offerText).');" class="offerte">Offerte</a></td>';

In my javascript function i want this array to be posted in an ajax call.

function addToOffer(offer_text) {
    $.ajax({
        url: "offer.php?action=add&offer_text="+offer_text,
        cache: false,
        method: "GET",
        async: false,
        complete: function(request) {
            if(!request || request.readyState != 4 || request.status != 200) return false;
            eval("var obj = "+request.responseText);

            $("span.error").hide();
            $("p").removeClass('error');
            if (obj.errors) {
                for (var i in obj.msg) {
                    $("#error_form_"+i).html(obj.msg[i]).css('display','block');
                    $("#p_form_"+i).addClass('error');

                    alert("error");
                }
            } else {
                var offer = $('#OfferContainer');
                offer.show().html(obj.html);

                var txt = Config.langOfferComplete;
                var buttons = {};
                buttons[Config.langOk] = false;
                buttons[Config.langGoToOffer] = true;

                $.prompt(txt,{
                    submit: function(v,m,f){
                        if (v) {
                            window.location = Config.base + '/' + Config.lang +  "/offer.htm";
                        }
                    },
                    buttons: buttons
                });
            }

            return false;
        }
    });

    return false;
}

But this is not working does somebody knows what I am doing wrong? I watch the output of my html i get this:

<a class="offerte" naam","adres","postcode","woonplaats","e-mailadres"]);"="" onclick="return addToOffer([" href="#">Offerte</a>

Upvotes: 0

Views: 766

Answers (3)

sakhunzai
sakhunzai

Reputation: 14500

The trick is that in a valid JSON string attributes are double quoted while escaping every . Therefore, you just need to change the double-quotes around function with single-quotes e. g

'<a onclick=\'return addToOffer('.json_encode($offerText).'\');">.... <a>';

i.e

onclick=\'...\'

But neither of solutions is proper . A better way would be to add script tag e.g

<script>
  var offerText=<?php echo json_encode($offerText) ;?>
 </script>
'<a onclick="return addOffer(offerText)"> ... <a>';

Upvotes: 0

Dr.Molle
Dr.Molle

Reputation: 117364

JSON contains doublequotes which are not allowed there, use htmlentities too to encode them(and other characters that are illegal there like < > & and may occure inside JSON):

$html .= '<td class="offerteCell"><a href="#" onclick="return addToOffer('.htmlentities(json_encode($offerText)).');" class="offerte">Offerte</a></td>';

Upvotes: 1

Jon
Jon

Reputation: 437904

That should be

onclick="return addToOffer('.addslashes(json_encode($offerText)).');"

Consider also using sprintf if you have such confusing string literals, it makes things look simpler:

$html .= sprintf(
   '<td class="offerteCell">'.
   '<a href="#" onclick="return addToOffer(%s);" class="offerte">Offerte</a>'.
   '</td>',
   addslashes(json_encode($offerText)));

I overdid it with the line breaks here, but I wanted the result to be comfortably viewable in this page.

Upvotes: 1

Related Questions