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 8975081
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T18:47:36+00:00 2026-06-15T18:47:36+00:00

I am adding some javascript code into my php file like as following: This

  • 0

I am adding some javascript code into my php file like as following:

This is the action part:

<script type="text/javascript">
function show_product(){
<?php foreach($products as $p){ ?>
if("<?php echo $p->category ?>" == "Smartphone")
document.write('<?php echo ($p->name); ?>');
<?php } ?>
}
</script>

This is the button part:

<?php
$js = 'onClick="show_product()"';
echo form_button('change', 'Search', $js); 
?>

But, the function is calling after a button calling which is placed on the same place and all the data are available at first, still my page show loading after clicking the button, though it shows the perfect result what I desired.
Can anyone help me to find out what the problem is?

  • 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-15T18:47:37+00:00Added an answer on June 15, 2026 at 6:47 pm

    Can u pass the $products array from php by encoding it into JSON and use as array in javascript. Use that array in for or foreach loop to get the data.. so that u no need to use more php tags inside it.. it wll work…

    use json_encode to convert it into json in php before sending to view template where u have javascript

    $message = "Hello";
    
    $json = array(   
          'message' => $message,
    );
    echo $json_encoded = json_encode($json);
    
    it wll output as
    {"message":"Hello"}
    
    and in javascript u just try the below,
    
    var output = <?php echo $json_encoded; ?>
    alert(output);
    alert(output.toSource());
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am converting some php code into a Ruby script and I am attempting
I'm working on adding some analytics code into our e-commerce website. What I'm doing
Is there a better way to write this function? I've inherited some javascript code
When I moved a paragraph of javascript code from a php file to a
I'm adding some <script/> tags from javascript to load some libraries (e.g., jquery). When
I'm looking into adding some unit tests for some classes in my data access
I am adding some code to an existing FreeBSD device driver and I am
first some code: I have many elements like that: <section class=item> <div class=caption> </div>
I have the following JavaScript code: var ans_el = document.createElement( 'input' ); ans_el.setAttribute( 'id',
I'm attempting to place some embed code into a Premium WordPress Theme. NOTE: I'm

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.