Shadowing Posted May 21, 2012 Share Posted May 21, 2012 Hey guys im having a real problem trying to make this string happend the problem is passing parameters on the javascript function "onmouseover" $map_array .= "<a href='map.php?planet=$address' onmouseover='ajax_popup ($picture,$detail_name,$stargate_address,$detail_owner,$time_conquered,$time_offset,$detail_siege, $alliance)'> <img src='images/star.jpg' style='position:absolute; left:".$b_x."px; top:".$b_y."px; border-style:solid; border-color: yellow;'></a>"; if I try this it works onmouseover='ajax_popup(1,2,3,4,5,6,7,'> So the problem is when the variables equal words. Cause of the quote issue. Is there like a way i should be using JSON to do what im trying to do? I have to keep this as a php string too. Or end as a php string Quote Link to comment https://forums.phpfreaks.com/topic/262850-syntax-issues/ Share on other sites More sharing options...
darkfreaks Posted May 21, 2012 Share Posted May 21, 2012 have you tried using heredoc $map_array = <<<EOT <a href='map.php?planet=$address' onmouseover='ajax_popup ($picture,$detail_name,$stargate_address,$detail_owner,$time_conquered,$time_offset,$detail_siege, $alliance)'> <img src='images/star.jpg' style='position:absolute; left:"$b_x"px; top:"$b_y"px; border-style:solid; border-color: yellow;'></a> EOT; this treats the whole thing like double quotes. you can use php variables much more easily inside them except when you do $_POST['foo'] which you would have to escape with curly braces {} . Quote Link to comment https://forums.phpfreaks.com/topic/262850-syntax-issues/#findComment-1347194 Share on other sites More sharing options...
trq Posted May 21, 2012 Share Posted May 21, 2012 Javascript doesn't handle new line charaters like php does. I suggest you put the entire js statement on a single line. Quote Link to comment https://forums.phpfreaks.com/topic/262850-syntax-issues/#findComment-1347226 Share on other sites More sharing options...
silkfire Posted May 21, 2012 Share Posted May 21, 2012 You should probably use AJAX with jQuery for this not dump variables into the attribute of the link. Quote Link to comment https://forums.phpfreaks.com/topic/262850-syntax-issues/#findComment-1347229 Share on other sites More sharing options...
Shadowing Posted May 21, 2012 Author Share Posted May 21, 2012 thanks guys for the replies Javascript doesn't handle new line charaters like php does. I suggest you put the entire js statement on a single line. yah its on a single line i just did so it was more reader friendly to who viewed my post. You should probably use AJAX with jQuery for this not dump variables into the attribute of the link. not sure what you mean. ajax_popup is a java script function that has jquery/ajax code in it. anyways i had to abandon this whole idea. cause the end game wasnt what i was going to want anyways. working on a differant route atm what im doing is im making a pop up box that shows new updated data every time the mouse hovers over it. Quote Link to comment https://forums.phpfreaks.com/topic/262850-syntax-issues/#findComment-1347245 Share on other sites More sharing options...
trq Posted May 21, 2012 Share Posted May 21, 2012 not sure what you mean. ajax_popup is a java script function that has jquery/ajax code in it. If your using jQuery don't use inline JavaScript. It has a css selector engine that enables your code to be separated from your markup. Quote Link to comment https://forums.phpfreaks.com/topic/262850-syntax-issues/#findComment-1347255 Share on other sites More sharing options...
silkfire Posted May 21, 2012 Share Posted May 21, 2012 not sure what you mean. ajax_popup is a java script function that has jquery/ajax code in it. If your using jQuery don't use inline JavaScript. It has a css selector engine that enables your code to be separated from your markup. Exactly. Using "onclick=" attribute was popular in early 2000s but now with jQuery that is completely unneseccary. Quote Link to comment https://forums.phpfreaks.com/topic/262850-syntax-issues/#findComment-1347265 Share on other sites More sharing options...
Shadowing Posted May 21, 2012 Author Share Posted May 21, 2012 have you tried using heredoc $map_array = <<<EOT <a href='map.php?planet=$address' onmouseover='ajax_popup ($picture,$detail_name,$stargate_address,$detail_owner,$time_conquered,$time_offset,$detail_siege, $alliance)'> <img src='images/star.jpg' style='position:absolute; left:"$b_x"px; top:"$b_y"px; border-style:solid; border-color: yellow;'></a> EOT; this treats the whole thing like double quotes. you can use php variables much more easily inside them except when you do $_POST['foo'] which you would have to escape with curly braces {} . idk i couldnt get that to work at all. i cant use just jquery i dont think. or cant think of a way of doing it at least. cause something has to be attached to the link so have somewhere to start from. Really hard to explain lol. i cant just give the link a id and use jquery. cause the id name would always be changing. i figured out that i can do this onmouseover='popup(\"$picture\")' but if I want to add a little html i cant get passed the next set of quotes onmouseover='popup("<div class='planet_float'><img src='images/planets/$picture.png'/></div>")' Quote Link to comment https://forums.phpfreaks.com/topic/262850-syntax-issues/#findComment-1347363 Share on other sites More sharing options...
Kays Posted May 21, 2012 Share Posted May 21, 2012 Then just kill those quotes. <?php $picture = trim($picture, '\'"\n\r'); $picture = str_replace('"', '"', $picture); $picture = str_replace("'", '''); // then on mouseover: onmouseover='ajax_popup("$picture", ...) Not complete code above, but you get the idea right? Just trim the quotes. Then escape the ones inside. And wrap that in quotes so ajax_popup will treat it as a string. Quote Link to comment https://forums.phpfreaks.com/topic/262850-syntax-issues/#findComment-1347365 Share on other sites More sharing options...
Shadowing Posted May 22, 2012 Author Share Posted May 22, 2012 Then just kill those quotes. <?php $picture = trim($picture, '\'"\n\r'); $picture = str_replace('"', '"', $picture); $picture = str_replace("'", '''); // then on mouseover: onmouseover='ajax_popup("$picture", ...) Not complete code above, but you get the idea right? Just trim the quotes. Then escape the ones inside. And wrap that in quotes so ajax_popup will treat it as a string. Hey Kay nice job i got it to work like this below $picture = "<div class='planet_float'><img src='images/planets/$picture.png'></div>"; $picture = trim($picture, '\'"\n\r'); $picture = str_replace('"', '"', $picture); $picture = str_replace("'", '', $picture); onmouseover='popup(\"$picture\")' still had to use \ though Quote Link to comment https://forums.phpfreaks.com/topic/262850-syntax-issues/#findComment-1347485 Share on other sites More sharing options...
Shadowing Posted May 22, 2012 Author Share Posted May 22, 2012 i could use jquery to target all the anchor selectors with in one div so if any of them get moused over on ( what ever jquery uses for that) then i have to read the id of the link, seperate it so i only see the number then use that number to run my ajax if jquery can read href links then i could store data in the link bleh.php?info=1234 Quote Link to comment https://forums.phpfreaks.com/topic/262850-syntax-issues/#findComment-1347488 Share on other sites More sharing options...
kicken Posted May 22, 2012 Share Posted May 22, 2012 Use json_encode and htmlentities together when you want to echo out some variable to an on* parameter. One way to avoid doing a bunch of concatenation is to put all the parameters in an array and then implode that. $onmouseoverParams = array($picture, $detail_name, $stargate_address, $detail_owner, $time_conquered, $time_offset, $detail_siege, $alliance); $onmouseoverParams = array_map('json_encode', $onmouseoverParams); //Fix for JS $map_array = ' <a href="map.php?planet='.$address.'" onmouseover="ajax_popup('.htmlentities(implode(',', $onmouseoverParams)).')"> <img src="images/star.jpg" style="position:absolute; left:'.$b_x.'px; top:'.$b_y.'px; border-style:solid;border-color: yellow;"> </a> '; Quote Link to comment https://forums.phpfreaks.com/topic/262850-syntax-issues/#findComment-1347501 Share on other sites More sharing options...
silkfire Posted May 22, 2012 Share Posted May 22, 2012 i could use jquery to target all the anchor selectors with in one div so if any of them get moused over on ( what ever jquery uses for that) then i have to read the id of the link, seperate it so i only see the number then use that number to run my ajax if jquery can read href links then i could store data in the link bleh.php?info=1234 You can store that data in a separate attribute starting with 'data-': <a id="...." href ="......" data-number="1234">.......</a> Quote Link to comment https://forums.phpfreaks.com/topic/262850-syntax-issues/#findComment-1347507 Share on other sites More sharing options...
Shadowing Posted May 22, 2012 Author Share Posted May 22, 2012 oh wow I didnt know there was a data attribute thanks for knowledge of that silkfire. I have 15 of these inside a div a user can hover over any of these. When a user moves left and right on a map 15 more appear. So id have to have jquery listen for clicks on any of the 15 with out knowing their ID before hand. if i could get jquery to do that then that would be a suitable replacement so thats the largest deal having jquery know what has been clicked with out knowing its id Your quotes are the opposite of mine kicken so i tried this below which is acctually the newest code im using. changed my mind on a few things. When I use this "onmouseover" is ignored and $picture some how lays inside the img tag. so it just shows $picture on top of the img tag. $picture = "<div class='planet_float'><img src='images/planets/$picture.png'></div>"; $map_array .= "<img id='$address' onmouseover='popup(".htmlentities($picture).")' src='images/star.jpg' style='position:absolute; left:".$b_x."px; top:".$b_y."px; border-style:solid; border-color: yellow;'>"; Quote Link to comment https://forums.phpfreaks.com/topic/262850-syntax-issues/#findComment-1347513 Share on other sites More sharing options...
kicken Posted May 22, 2012 Share Posted May 22, 2012 if i could get jquery to do that then that would be a suitable replacement so thats the largest deal having jquery know what has been clicked with out knowing its id You can do that with jQuery using the .delegate method. Just use whatever css query would match your desired elements. For example: jQuery('#map').delegate('.star', 'mouseover', function(e){ //do whatever here //e.target will be a reference to the specific .star element if you need to extract data from it. }); When I use this "onmouseover" is ignored and $picture some how lays inside the img tag. so it just shows $picture on top of the img tag. You want to have it setup so your attributes are quoted with double-quotes, or use the ENT_QUOTES option to htmlentities(). Also you need the json_encode() call to make a proper JS string. $picture = '<div class="planet_float"><img src="images/planets/'.$picture.'.png"></div>'; $map_array .= '<img id="'.$address.'" onmouseover="popup('.htmlentities(json_encode($picture)).')" src="images/star.jpg" style="position:absolute; left:'.$b_x.'px; top:'.$b_y.'px; border-style:solid; border-color: yellow;">'; It'd probably be better/easier in the long run though to use jQuery+ajax rather than mess around with trying to echo stuff out to an on* attribute. Quote Link to comment https://forums.phpfreaks.com/topic/262850-syntax-issues/#findComment-1347522 Share on other sites More sharing options...
Shadowing Posted May 22, 2012 Author Share Posted May 22, 2012 yah for sure really appreciate all the input I got. thanks guys finally figured out a way to do this with jquery lol. make all links share the same class name then have each link have their own id then if any of those links with that class is clicked it turns the value of the id into a variable and trips the ajax function "planet_details" $(".planet_details").live('click' ,function(event) { event.preventDefault(); planet_details($(this).attr("id")); }); so glad i figured that out. Cause you guys are right much better way of doing it. now i just need to figure out how to do it with mouseover Quote Link to comment https://forums.phpfreaks.com/topic/262850-syntax-issues/#findComment-1347642 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.