0

Through use of jQuery I arrive at a number (id of a specific widget on my page). The variable that holds this number is active_widget_id.

What I need to do is:

  • make use of this number as in the following:

<?php $toggleImage = $text_widgets[active_widget_id]['toggle']; ?>

  • get the resulting $toggleImage value (it is the name of a file, like 'big-widget-header.gif')

  • And display the image in a div (say #toggleHolder) on the page.

The trouble is with a) the jQuery needing to run at document.ready to get the number, b) and once I am able to construct "$text_widgets[active_widget_id]['toggle'];", not being able to run the php code (wrapping it with php echo tags doesn't work, it just lists the above, not its result.)

I am more of a designer and I am trying to go with appends, prepends, html() and such but I suspect I should look at an ajax type of solution.

Any pointers appreciated...

1
  • Ended up displaying the image inside the widget, copying its html, pasting inside the desired div, removing the original instance. God knows when I will finally get my hands dirty with some hardcore jQuery action :) I will pick Mark's answer as it is a clear roadmap for future jquery-challenged questioners. Thanks Mark and Jason. Commented Jan 6, 2010 at 2:06

2 Answers 2

2

Basically you need to have the page execute again via an ajax call (which you can do through jQuery).

The sequence would be something like:

  1. execute PHP code
  2. page loads
  3. run current jQuery code
  4. use jQuery to make an ajax request passing parameter from (3) as GET or POST data
  5. (request asks PHP to compute some data, PHP returns a value)
  6. parse response from jQuery ajax request in (5)
  7. use jQuery to update the div and display the result from (6)
Sign up to request clarification or add additional context in comments.

2 Comments

So I am at a point where I have $toggleImage = $text_widgets[active_widget_id]['toggle'];". Isn't this good enough to run the ajax request? I feel like all I need is to get the result of this (like I am at point 6). Or would you say that I am only at point 3 at this stage?
The PHP code will undoubtedly have to parse the GET or POST data from the request. You'll need to literally echo the result once you compute what it is...
1

You can't really mix client side and server side like you're trying to do here. What you need to be doing if you want to get data from the server while on the client side is run some $.post/$.ajax/$.get calls and then perform your DOM modifications in the callback functions.

(very crude) Example:

$.post('example.com/yourService', data, yourCallbackFunction, 'json');

function yourCallbackFunction(data) {
    $('.yourClass img').attr('src', data.imageSrc);
}

1 Comment

Thanks. I'm going to have to study this... I have been using visualjquery.com as a resource for examples, hopefully there are real-life examples similar to my situation there.

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.