Page 1 of 1

php in javascript

Posted: Sun Sep 06, 2009 5:20 am
by satikas
This question is about using TinyMCEs ajax load. (http://tinymce.moxiecode.com/examples/example_06.php).
How can I execute a MySQL query on line 7 to get the new content WHERE pagenumber=-1
I cannot seem to use $pagenumber-1 in WHERE clause because it will always equal to 7-1=6 for example.

This is the button:

Code: Select all

echo "<img src='images/main/previous.png' border='0' onclick='ajaxLoadprevious();'>";
And this is the script:

Code: Select all

<script type="text/javascript">
function ajaxLoadprevious() {
 var ed = tinyMCE.get('content');
 ed.setProgressState(1);
 window.setTimeout(function() {
 ed.setProgressState(0);
 ed.setContent('MYSQL QUERY HERE');
}, 1000);
}
</script>
Cookie, 10 hugs and a big thanks for anyone, who helps me out :banghead:

Re: php in javascript

Posted: Sun Sep 06, 2009 5:38 am
by Ollie Saunders
It's not so much that you want to put the query in there but the result of the query. I guess you know how to query a database with PHP alone? If you do then you just need to output that result into the JavaScript string:

Code: Select all

<?php $queryOutput = 'blah blah';
?>
<script type="text/javascript">
ed.setContent('<?php echo htmlspecialchars(addslashes($queryOutput)); ?>');
//</script>
 

Re: php in javascript

Posted: Sun Sep 06, 2009 5:59 am
by satikas
Thanks for your reply.
Tried and it only works when "pagenumber" in the query equals to a plain number (1,2,3,4...).
As soon as i change it to "$pagenumber+1" a white empty textarea appears.
I need its value to change, so it would be like: 1+1, 2+1, 3+1 and so on.
Currently my "$pagenumber" comes from $_GET['pagenumber'];
and thats the reason it wont go higer than 2 (1+1, 1+1, 1+1).

So, is it possible to change $pagenumber`s value higher by 1 EACH time the function is loaded?

Code: Select all

function ajaxLoadnext() {
 
<?php $nextcontent = mysql_query("SELECT * FROM notepadpages
WHERE owner='$ename' AND notepadid='$id' AND pagenumber='$pagenumber+1'") or die(mysql_error());
while($data=mysql_fetch_array($nextcontent)){ 
$datanextcontent=$data["content"];} ?>
 
 var ed = tinyMCE.get('content');
 ed.setProgressState(1);
 window.setTimeout(function() {
 ed.setProgressState(0);
 ed.setContent('<?php echo $datanextcontent; ?>');
}, 1000);
}

Re: php in javascript

Posted: Sun Sep 06, 2009 9:26 am
by Ollie Saunders
Remove the quotes from around the pagenumber equality comparison in your query.

Re: php in javascript

Posted: Sun Sep 06, 2009 10:07 am
by satikas
Gets the first and the second page and wont go any further.
Basically its loading the stuff in the function section immediately when the page is loaded, instead of waiting for the user to trigger it.

Re: php in javascript

Posted: Sun Sep 06, 2009 10:29 am
by Ollie Saunders
Basically its loading the stuff in the function section immediately when the page is loaded, instead of waiting for the user to trigger it.
If you want that then we're going about this completely wrong. You need to do an AJAX call. For that you'll probably want a JavaScript framework. JQuery is nice.

What we're doing right now is generating the JavaScript with a string pre-inserted. No more sophisticated than <textarea><?php echo $stuff; ?></textarea>. Once the page has loaded nothing's gonna change unless the JavaScript is in charge.

Re: php in javascript

Posted: Sun Sep 06, 2009 10:52 am
by satikas
Well, I found something on the net:

Code: Select all

http://net.tutsplus.com/tutorials/javas ... th-jquery/
Yet its like chinese to me. Whats the estimated lenght of the missing code? I assume it requires some uber skills in Javascript and Ajax.
I have used jQuery before though.

Re: php in javascript

Posted: Sun Sep 06, 2009 2:47 pm
by Ollie Saunders
Yet its like chinese to me.
That's okay. Just don't try to get it right in too much of a hurry.
Whats the estimated lenght of the missing code?
Probably only about 30 lines.
I assume it requires some uber skills in Javascript and Ajax. I have used jQuery before though.
Not really. jQuery makes this stuff pretty straight-forward. There are be forums dedicated to it if you have trouble. A lot of people dive into JavaScript thinking they don't really need to understand the language. It's a silly assumption to keep with you for a long time but it's a great one for empowerment, often with surprisingly good results.