Jump to content

syntax issues


Shadowing

Recommended Posts

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

 

 

Link to comment
Share on other sites

have you tried using heredoc :wtf:

 

 


$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 {} .

 

 

 

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

have you tried using heredoc :wtf:

 

 


$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>")'

 

 

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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>
';

 

Link to comment
Share on other sites

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>

Link to comment
Share on other sites

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;'>";

 

Link to comment
Share on other sites

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.

 

Link to comment
Share on other sites

 

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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.