0

I have a php array that is being passed to a js function using "json_encode($array)", the problem is that when I click the button to trigger the function, nothing happens besides the page refreshing.

I think the problem might be the enclosing quotes, though I already tried changing from double to single and vice verse, no joy.

HTML:

 <button class="button1" 
  onclick="validateEncomenda('<?php echo json_encode($chosen_restaurant_addr);?>');
           return false;">Encomendar</button>

js:

function validateEncomenda(myarray){
   alert('Hello JS');
  }

if I echo the "json_encode($chosen_restaurant_addr)" I'm getting the right values in this format:

["ipsum lorem blah blah","Curabitur aliquam feugiat tellus"]

Any help or hint will be greatly appreciated.

3
  • Show us the array you want to encode to json. Commented Oct 7, 2014 at 14:25
  • 3
    Your double quotes in the json are closing the onclick event attribute. onclick="validateEncomenda('[" Commented Oct 7, 2014 at 14:26
  • Right, it seems to be a enclosing quotes problem. Can you post the computed value of json_encode($chosen_restaurant_addr) ? Commented Oct 7, 2014 at 14:26

3 Answers 3

2

A better way to handle this would be to separate the onclick handling out from the HTML source. You could create a script like this:

var chosenResturant = <?php echo json_encode($chosen_restaurant_addr);?>;
document.getElementById('button1').onclick = function() {
    // no need to parse JSON here as we created an array literal above
    alert(chosenRestaurant.length);
    return false;
};

Note here that I am assuming that you add an id button1 to the element as the means for determining which elements gets the event handler. If you truly want to attach this behavior to all elements with the class name of button1, you would simply substitute getElementsByClassName('button1') instead of getElementById('button1')

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

6 Comments

You're missing quotes around the json ;)
@blue112 I don't need quotes around it. I am defining an array literal (as I noted in my code sample).
Oh yeah, it's json, it's working out of box in js, of course, my bad.
Right the only problem is that i have all my JS in a separate file, and I'm using a function because for this case it works best as i have many other functions being called inside the validateEncomenda() function.
@MichelFigueiredo You could still have the onclick handler function definition in a static js file and define the variable dynamically via PHP inside script tags (in essence what you are doing in the inline function call now).
|
1

You should escape the quotes in the PHP output. Since HTML quote can't be escaped, rewrite it like this :

onclick='validateEncomenda("<?php echo addslashes(json_encode($chosen_restaurant_addr)); ?>"); return false;'>

1 Comment

This works if i remove the "return false;" with the return false it tell me "invalid return" as a syntax error.
0

I think that you should add new attribute for the button: type="button"

Comments

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.