0

I have no idea how to do this. I want to insert php code into an image source through jquery.. but it is not working. Here is my code and help would be greatly appreciated!

JQUERY:

var mainId = $(this).attr('id');
$("#intabdiv img").attr('src', '<?=resize("../php/admin/portfolio/before/'+mainId+'",$settings)?>' );

HTML:

<div id="intabdiv">
<?php $settings = array('w'=>450,'h'=>450,'crop'=>true); ?>
              <img src="" border="0" class="pic" />
          </div>
</div>
1
  • post code from "View Source" in browser for the jQuery shown and make sure it is wrapped in ready event Commented Mar 9, 2012 at 6:02

3 Answers 3

2

It looks to me like you're trying to have the client browser execute PHP. That's not going to work for you. I'm not sure you have a completely clear idea of the separation between server and client, and what happens where.

Here's a simple model that you can use to think about where things occur:

  1. User puts URL in browser and hits enter
  2. Browser sends request to server
  3. Server receives request and attempts to provide the resource requested
  4. At this stage, if the file is a PHP file, any PHP instructions are executed. The end result of this, after all PHP is finished and done, is an HTML file, perhaps with some javascript.
  5. The server sends this HTML file back to the browser.
  6. The browser loads the HTML file, and executes any javascript, which can manipulate the HTML on the client side.

So, where does that leave us? You're attempting to take an image file that exists on the server, resize it, and display it to the end user. Presumably the "resize" function is one you have defined somewhere in your PHP code. I'll go on the assumption that you just need to do this once and have it show up for the user when the page loads.

In this case, what you need to do is something more like the following (all in the same file):

<?php
$settings = array('w'=>450,'h'=>450,'crop'=>true);
$imgsrc = resize("../php/admin/portfolio/before/imageid.jpg",$settings);
?>
<div id="intabdiv">
    <img src="<?= $imgsrc; ?>" border="0" class="pic" />
</div>

... no javascript required. Obviously, imageid.jpg must be the actual filename of the image you're trying to access. If, instead, you're trying to have some user action trigger the access to the image, please provide more context for what you're trying to accomplish and a better answer can be given.

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

3 Comments

Yes but this will not work for me. I have to pass that via jquery. I am not trying to run php on jquery. I simply want php to be inserted into the src code. I need the mainID which is being generated according to button click. Is there any way to pass raw html through jquery? Thanks
Forgive me for misunderstanding, you actually just want a string of PHP to show up in your img src attribute, that won't actually execute and will, for all intents and purposes, be nonsense to the browser? I'm curious as to your reasons, but I at least understand that you are generating the ID on the client side with a button click, which means that jquery will be involved in getting you where you want to go. If you can, could you please give a more complete explanation of what you're trying to accomplish from a user perspective? What is the user experience you're trying to create here?
@liveandream Lacking the full context of where this JavaScript is located, it's hard to see what you're trying to do. What does this line $("#intabdiv img").attr(...) look like when you view the page source in your browser?
1

The problem is you set the html as having an ID, but jquery is using the selector as being a class.

So change it from:

$(".intabdiv img")...

To

$("#intabdiv img")...

4 Comments

That is what I have originally, sorry that was a typo but it is still not working... any more help?
What is it currently giving you in the source?
Also; Is the Jquery meant to by dynamically generating it? Because if so, you would want to make an ajax request and send the results to the src attribute.
I am not sure what you mean about Ajax. this is what I see in the source code: <img src alt"" border"0"/>
0

Why not use something like jcrop? http://deepliquid.com/projects/Jcrop/demos.php

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.