Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9257297
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T12:08:47+00:00 2026-06-18T12:08:47+00:00

I would like to call a function onclick, but with a json_encode parameter like

  • 0

I would like to call a function onclick, but with a json_encode parameter like this:

<a href="#" onclick="javascript:table1Modif(<?php echo substr(json_encode($res['base']),1,-1); ?>);return false;">

And the Jquery function just alert the parameter:

<script>
    function table1Modif(key){
        $('#table1').html(function() {
            alert(key);
        }); 

    }
</script>

But I have an “undefined” error!
I am sure that it’s due to json_encode, but I don’t know how to solve it.
Thank you!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T12:08:48+00:00Added an answer on June 18, 2026 at 12:08 pm

    It looks like you are passing a string to json_encode in order to generate a JavaScript string literal (which is not valid JSON).

    You are using substr to remove the quotes from this string.

    JavaScript will therefore see an identifier (which in that context will be treated as a variable).

    You need a string literal, so the first thing to do is remove the substr call.

    This will create a new problem. You are inserting the string into an HTML document, but not expressing it as HTML. The " character at the start of the string literal will therefore be treated as end of attribute value, which you don’t want.

    When inserting non-HTML content into an HTML document you need to express it as HTML. Run the code through the htmlspecialchars function to do this.

    onclick="table1Modif(<?php 
        echo htmlspecialchars(
            json_encode($res['base'])
        );
    ?>);return false;"
    

    Incidently, I’ve removed the entirely useless javascript: label. You don’t have a loop to break or continue from so it isn’t doing anything. While you’re at it, you should replace href="#" with something more sensible. Follow the principles of Progressive Enhancement and Unobtrusive JavaScript.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I would like to extend my onclick parameter of button_to_remote with another javascript function
I would like to call JavaScript function many time . I have tried like
I would like to create a function from this: element.onclick = function(e){ ... code
I have a vector of pointers. I would like to call a function for
I've got a pickerview. I would like to call a specific function each time
I would like to have a function that can wrap any other function call.
I would like to loop through an NSString and call a custom function on
AIM: I would like to set the below function to call every 5 seconds.
I can use Eric Martin's simplemodal somewhat. But I would like to call it
I'm trying to call a JavaScript function through PHP and have met some problems.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.