-1

I have the following HTML...

<img id="image1" src="URL" alt="image1" name="image1" width="137" height="119" border="0" style="position: relative; left: -355px; top: 62px;" >

How would I go about storing the following information into a variable..from the style attribute.

 $variable1 = -355;
 $variable2 = 62;

Thank you in advance!

UPDATE:

Apologies for not explaining clearly and thank you for your help.

The goal right now, after submitting a form on the page, to find all the img tags and store each attribute within the img tag into a PHP variables.

I will then want to store those values into a MySQL database and retrieve them on a different page.

I hope that clears things up.

Richard
  • 1,414
  • 2
  • 16
  • 27
  • Remember, PHP is a server side language. Sure you can store the values is a PHP variable, but once the page has been rendered you aren't going to be able to do anything with them without using javascript. What is the purpose of storing them, what are you looking to accomplish? – Brett Gregson Dec 18 '12 at 23:26
  • Question is: how did it get there in the first place. You wouldn't be the first who generates HTML in PHP and then uses a big chunk of complex code to parse the HTML they just generated. – GolezTrol Dec 18 '12 at 23:26
  • The position values within the style attribute are generated by a jQuery plugin. I need to somehow grab those values after someone hits submit and store them into variables for later retrieval. – Richard Dec 18 '12 at 23:28
  • Storing? Parsing? Retrieving? As it stands, your question is incomplete. – Ignacio Vazquez-Abrams Dec 18 '12 at 23:33
  • @Richard then I recommend doing that with jQuery/Javascript. – Nick Fury Dec 18 '12 at 23:36
  • http://simplehtmldom.sourceforge.net/manual.htm#frag_find_advanced – andrewk Dec 18 '12 at 23:53

2 Answers2

0

If you create the page then you should be able to know these values in advance because you can either assign the variables first and then print them in these tags, or, they are already preset somewhere in the template and you are just going to read these values.

Otherwise, if that content is generated in another way, not by your system, then you will need some JavaScript code to 'read' these attributes and send it to you 'aside' as user submits the form. Usually this is done by adding some hidden fields with the something like "document.image1.src" as the value to be sent.

It is impossible for me to give you even a snipped of code here as we have no idea what really is your application structured like. However, this should give you the understanding on how this works.

MAXIM
  • 1,223
  • 1
  • 9
  • 16
  • Thank you MAXIM, the values I really need are within the inline style attribute which are generated by JavaScript (jQuery Plugin). Those values, I would like to store into my database and then retrieve them on another page. I know how to store values into a DB, I just need to know how to grab those values after a user hits submit. – Richard Dec 18 '12 at 23:47
  • i think this can help you to start: http://stackoverflow.com/questions/1098349/reading-non-inline-css-style-info-from-javascript – MAXIM Dec 18 '12 at 23:50
  • I'm not good in JavaScript and CSS, so I can't tell you the exact way to read these computed values, to later assign them to a hidden form, and finally send them along the user data to the PHP file. – MAXIM Dec 18 '12 at 23:55
  • Interesting, thanks so much for your help. I believe I will then need to use JavaScript to grab the style values, store them in variables and then do the following to get those values into PHP variables, var whatever = "= $phpVar ?>"; – Richard Dec 18 '12 at 23:56
  • stop. Not really. We were talking about storing these in the database. More like for a statistic purpose. You can't read them in JavaScript and then send it to the PHP before the page finished loading to adjust page rendering or something. Once your server finished sending the page to the browser, and if you couldn't read these values before, then only the browser can send them back to you with those JavaScript methods. The received values you can, at most, use for the next page. Not the current. Unless you use other technologies like Ajax ecc – MAXIM Dec 18 '12 at 23:59
  • but then again the paradox here. If you are printing that page, so you can also print the JavaScript code on it to then get the values back (which you will need to place them in the same spot on next reload). Why can't you simply grab them from somewhere in the first place? Before loading the whole thing? Look well, there gotta be a way – MAXIM Dec 19 '12 at 00:03
  • Im using the jQuery UI draggable() function. I need to grab whatever the values created by draggable(). Doesn't sound like it's an easy solution. – Richard Dec 19 '12 at 00:06
0

You could do this like that:

When your jQuery plugin sets image attributes you could also add hidden input fields to the form with those attributes values. This is possible if the plugin api has events available. Otherwise you would need to extend it by your own. After subbmiting form, server would receive hidden input fields values along with other. So in PHP this would be accesible from the $_GET or $_POST global array (depending on form method).

Another way:

You could also capture the moment when user clicks submit button on the form. Then - stop default behavior. Next send an Ajax call to the server, passing current image attributes values. Server can store those values in the database, or do whatever you want and return a response. After response you can trigger the form submission programmatically.

Luigi
  • 866
  • 13
  • 34
  • I like your first option, but how would using the POST method grab the value from "style=whatever". Ideally if I can just grab "style=whatever" from the image attribute I would be set. – Richard Dec 19 '12 at 00:20
  • This is an example: http://jsfiddle.net/f6cLr/2/ . After submitting the form check $_POST array. – Luigi Dec 19 '12 at 00:47