Jump to content

posting text to a page element


iceregent

Recommended Posts

Hello. I am in need of some help. I am trying to display a line of text in a specific location on my page, and them read a new line of text form my database, and replace the old text with the new text.. and on until the end of the table is reached. I am not having a problem getting the data.. but i need to know how, using javascript.. that i can place the text into that one element, without refreshing the web page. There are other things going on in the web page, and i only want that one single element updated with the new text each time the database is read. The scrip i am posting, is included in the page, and is only a portion of what is going on.Here is the code so far..And please remember, this is a include file, not the entire set of code..

$queryname = "SELECT tsid FROM text_save WHERE myprog='$textname'";$resultname = mysql_query($queryname) or exit(mysql_error());		if($rowname = mysql_fetch_array($resultname, MYSQL_ASSOC)) {  $_SESSION['textid'] = $rowname['tsid'];}									$tsid = $_SESSION['textid']-1;?>									<span id="textline"> </span>																										<?php$query = "SELECT * FROM text_lines WHERE tid='$tsid'";$result = mysql_query($query) or exit(mysql_error());$timepause = 6;																		while($row = mysql_fetch_array($result, MYSQL_ASSOC)) {  									$textline = $row['text'];?><script>document.getElementById("textline").innerHTML = $textline;</script><?php											 }	} ?>

I still haven't gotten the timer worked out yet, but i need to be able to set a delay time in seconds, and only ready and display that line of text every X number of seconds. I need to display the text in the <span> element. I need ONLY this element to be updated, and the rest of the webpage to not be disturbed, thusly I am looking at javascript to update only that one <span>

Link to comment
Share on other sites

I would recommend getting all of the data from the database, and using PHP to write the data into a Javascript section as a Javascript array. You can use JSON to make that easier. Once all of the data is printed on the page in a Javascript array then it's pure Javascript from there, you can set up a timer to loop through the array and display each item.

Link to comment
Share on other sites

Look it up, it's a way to move data structures like arrays and objects between things like PHP and Javascript. You can use PHP's json_encode function to encode a PHP array as a string of JSON, print that into a Javascript section, and end up with a Javascript array.

Link to comment
Share on other sites

Ok, lets put it this way.. The code i have so far, works flawlessly to read the text line by line from the database. You are telling me to go on a completely different long learning journey to learn something new, which mean, begin learning a whole new way of doing things, which would take me, how long to learn a new language? I find this forum not very friendly, and doubt that I will find any help here. Thank you for not helping.

Link to comment
Share on other sites

Let's put it this way, the code you have doesn't work. Rendering a JSON string is a simple way to accomplish the necessary Php-to-Javascript communication, unless you want to implement AJAX and even with AJAX JSON would be the best approach. As it is this simple approach does all the queries at the time the page is rendered so if you really need to delay the queries then you will need to implement AJAX or reload the page.

 

JSON Encode...

http://php.net/manual/en/function.json-encode.php

<!DOCTYPE html><html lang="en"><head><meta charset="utf-8"><title>php to javascript</title></head><body><h1>Php to Javascript Test</h1><p id="out1"></p><script><?php$arr1 = array('a','b','c','d','e','f');$str1 = json_encode($arr1);echo "var a = $str1;";?>var out1 = document.getElementById("out1");var i = 0;function put(){if (i<a.length){out1.innerHTML += a[i] + '<br/>';i++;setTimeout(put,1000);}}put();</script></body></html>
Link to comment
Share on other sites

Ok, lets put it this way.. The code i have so far, works flawlessly to read the text line by line from the database. You are telling me to go on a completely different long learning journey to learn something new, which mean, begin learning a whole new way of doing things, which would take me, how long to learn a new language? I find this forum not very friendly, and doubt that I will find any help here. Thank you for not helping.

 

You seem to have assumed (without doing the suggested research) that JSON is another programming language. It is nothing more that a way of converting data to and from a string representation and is exactly what you need.

Link to comment
Share on other sites

I think this is what he was trying to do. It works -- but view source reveals how primitive it is.

<!DOCTYPE html><html lang="en"><head><meta charset="utf-8"><title>php to javascript</title></head><body><h1>Php to Javascript Test</h1><p id="out1"></p><script>var a;var out1 = document.getElementById('out1');<?php$arr1 = array('a','b','c','d','e','f');for ($i=0 ; $i<count($arr1) ; $i++){echo "a = '{$arr1[$i]}';";echo "out1.innerHTML += a + '<br/>';"; }?></script><p>A primitive method</p></body></html>
Link to comment
Share on other sites

I find this forum not very friendly, and doubt that I will find any help here.

A new language? Which new language? You're already using PHP and Javascript. That's all you need. JSON is not a new language, in fact guess what the "JS" stands for. That tells me you haven't bothered to do any research. Have you looked up the PHP documentation for json_encode? No, probably not. You want people to hold your hand and walk you through everything. That's not what we do here, we're here to help people learn how to program. If that's not what you're here for then you're in the wrong place.
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...