Page 1 of 3

javascript code within the FIELD to be read by a sql query

Posted: Sun Jul 26, 2009 12:10 pm
by straightman
Hello everybody and thanks for reading

Can I include this javascript code in the field of the database so that it will be read by the sql query?

definitively i think it needs some php wrapping but, the core of the question is "can one include javascript code within a field of a DB?


<a href="#" onmouseover="Tip('Image 1 example of tips<br /><img src=images/stories/posters/poster2.jpg width=370 height=246>',SHADOW, true, BGCOLOR, '#000000', FADEIN, 400, FADEOUT, 400, SHADOWCOLOR, '#000000', SHADOWWIDTH, 2, BORDERCOLOR, '#000000',OPACITY, 90,FONTCOLOR, '#aadddd')">Example 2 Image Tip</a> <br /><br />

would it just be a matter of putting it within the echo '< thing ..?

Re: javascript code within the FIELD to be read by a sql query

Posted: Sun Jul 26, 2009 12:12 pm
by jackpf
Wait...so you want to insert that block of html into the database?

Re: javascript code within the FIELD to be read by a sql query

Posted: Sun Jul 26, 2009 12:30 pm
by straightman
yes, i mean imagine a field can contain values for name, age, etc. Well, within a field I want to include that code of javascript. What that actually does is allow for tool tips, you know this kind of labels that float when you hover over a text. In principle the sql query would read out all of the fields and would be unaware that that is javascript and should simply display the code so that the browser can transform it into what this js does,

jackpf wrote:Wait...so you want to insert that block of html into the database?

Re: javascript code within the FIELD to be read by a sql query

Posted: Sun Jul 26, 2009 12:34 pm
by jackpf
Right....ok :D

Well, to answer your first and second question, yes. Anything can be inserted into a database as long as the column is the right data type.

I don't really understand your third question...

Re: javascript code within the FIELD to be read by a sql query

Posted: Sun Jul 26, 2009 12:40 pm
by straightman
to be more exact, this code is actually the value in the database, not the field as a header of the column. But the browser does not interprete it well and gives me things like %3 etc where there is actually <h ref etc..


well i think that by saying "i dont understand your third question" you have already answered it because it makes me realize that it was a little absent minded. You know that we put echo ' when we are embedding php withing html, so it looked like i went stranded in my thoughts. Well, I have included that code within the field and assigned medium text as the data type, but when the sql query reads it, it actually displays it with % so as to fill in the empty spaces. Somehow is not displaying it ok, so that is why I asked if i had to wrap it in anyway. The code does work because I am using it in another context. The sql works perfectly because I have been using it for years in this database but today i had that idea and wanted to try it but i am not coming on ...

jackpf wrote:Right....ok :D

Well, to answer your first and second question, yes. Anything can be inserted into a database as long as the column is the right data type.

I don't really understand your third question...

Re: javascript code within the FIELD to be read by a sql query

Posted: Sun Jul 26, 2009 12:43 pm
by jackpf
It replaces spaces with %s?

That's weird...are you running mysql_real_escape_string() when inserting it into the database?

And also, does it have percentage symbols in the database, or is it only when you echo it to the page?

Re: javascript code within the FIELD to be read by a sql query

Posted: Sun Jul 26, 2009 1:09 pm
by straightman
well in this specific case I am actually inserting directly into the database from myphpadmin, i am not doing it through a form. I am simply pasting this very code <a href="#" onmouseover="Tip('Image 1 example of tips<br /><img src="images/stories/posters/poster2.jpg" width="370" height="246" />',SHADOW, true, BGCOLOR, '#000000', FADEIN, 400, FADEOUT, 400, SHADOWCOLOR, '#000000', SHADOWWIDTH, 2, BORDERCOLOR, '#000000',OPACITY, 90,FONTCOLOR, '#FFFFFF')">Example 2 Image Tip</a> <br /><br />

directly into the cell as a value, as I could have entered Peter or 16. So, no need for the real_scape

As per the % symbols it is only when I echo it into the page. I have also backslashed the ' but it made no difference.
I get to see this: %3Chtml% so I dont know...
--------------------

jackpf wrote:It replaces spaces with %s?

That's weird...are you running mysql_real_escape_string() when inserting it into the database?

And also, does it have percentage symbols in the database, or is it only when you echo it to the page?

Re: javascript code within the FIELD to be read by a sql query

Posted: Sun Jul 26, 2009 1:14 pm
by jackpf
What's your code for displaying the html?

phpMyAdmin does not add anything to the data...also, you shouldn't need to escape it; phpmyadmin should do that for you.

Re: javascript code within the FIELD to be read by a sql query

Posted: Sun Jul 26, 2009 1:23 pm
by straightman
well,
within joomla I just include what I pasted and it works great, but within the cell of the DB I thought of adding the
<html>
and </html>
alright, i will repaste it now without the backslash but i think it will make no much of a change



jackpf wrote:What's your code for displaying the html?

phpMyAdmin does not add anything to the data...also, you shouldn't need to escape it; phpmyadmin should do that for you.

Re: javascript code within the FIELD to be read by a sql query

Posted: Sun Jul 26, 2009 2:01 pm
by straightman
I have added other code, the most simple helloworld one and it doesnt work either, same display with this
<html>
<body>
<script type="text/javascript">
document.write("Hello World!");
</script>
</body>
</html>
the only time it does not display it is when i remove the code from the cell







straightman wrote:well,
within joomla I just include what I pasted and it works great, but within the cell of the DB I thought of adding the
<html>
and </html>
alright, i will repaste it now without the backslash but i think it will make no much of a change



jackpf wrote:What's your code for displaying the html?

phpMyAdmin does not add anything to the data...also, you shouldn't need to escape it; phpmyadmin should do that for you.

Re: javascript code within the FIELD to be read by a sql query

Posted: Sun Jul 26, 2009 2:29 pm
by jackpf
Oh, you're doing this with joomla?

Idk anything about joomla, maybe that's what's adding the symbols?

Re: javascript code within the FIELD to be read by a sql query

Posted: Sun Jul 26, 2009 3:27 pm
by straightman
No, no, it is in joomla where it works fine. where by adding that snip of code i get that floating tooltip alright.

The other web is a very well coded web that has been working flawless for 4 years. Today I had that occurrence of seeing whether that was possible but now I am going to be stubborn, it has to work, that simple bit of code hello world should display in the browser just Hello World and not %3Chmtl etc.

The sql and php query goes like this, and it is flawless because it does work fine. But when it comes across that field with the value of the javascript code, it messes it up

<?


include('include.php');
$result=mysql_query("SELECT * FROM zdb_bilder ORDER BY id DESC");
echo '<book>';

while($row = mysql_fetch_array($result)){
echo '<entry id="'.$row['id'].'"';
for($i=1; $i<mysql_num_fields($result);$i++){
$field= mysql_field_name($result,$i);
echo ' '.$field.'="'.urlencode($row[$field]).'"';
}
echo ' />';
}

echo '</book>';

mysql_close ();

?>

i am thanking you for dedicating time and thought to this



jackpf wrote:Oh, you're doing this with joomla?

Idk anything about joomla, maybe that's what's adding the symbols?

Re: javascript code within the FIELD to be read by a sql query

Posted: Sun Jul 26, 2009 3:37 pm
by jackpf
You're running it through urlencode()!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

Re: javascript code within the FIELD to be read by a sql query

Posted: Sun Jul 26, 2009 3:54 pm
by straightman
but why would that affect the thing? if it picks up a term like Ana, 16, why can it not pick <html> etc

what should I do then? It looks like you have bursted in exasperation :(((((((((((((( sorry a million times but I am baffled




jackpf wrote:You're running it through urlencode()!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

Re: javascript code within the FIELD to be read by a sql query

Posted: Sun Jul 26, 2009 3:59 pm
by jackpf
Omg...why do you think!??!? Sorry....sorry...

Think about it. You're running html through urlencode(). You are url encoding it!!!