0

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>
3
  • You're assigning your array to $offerText[] so you want to do json_encode( $offerText[0] ) Commented Mar 30, 2012 at 11:54
  • 1
    Why do you use async:false? That's almost always a bad idea. Commented Mar 30, 2012 at 11:56
  • @ThiefMaster: always a bad idea? why then would it even be an option... sometimes you want to prevent your script from manipulating the DOM before the ajax call is completed, when -for example- the html is returned, that is manipulated after the request has been sent. it would be a bad idea to be working async in that case Commented Mar 30, 2012 at 12:15

3 Answers 3

1

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.

Sign up to request clarification or add additional context in comments.

Comments

1

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>';

Comments

0

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>';

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.