Jump to content

AJAX send and recive info from php


westman

Recommended Posts

See how easy it is now! Now you have to echo the_word in the javascript function call unless you echo out the whole html <a href="#" onclick="return false" onmousedown="javascript:text_info('<?php echo $the_word; ?>');">start</a> echo '<a href="#" onclick="return false" onmousedown="javascript:text_info(\''.$the_word.'\');">start</a>';

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8" /><title>Untitled Document</title><?php $the_word = " NOW ITS all clear now";?><script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script><script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.4/jquery-ui.min.js"></script><script language="javascript" type="text/javascript">function text_info( theword) {output = document.getElementById("text_output");var update = "update"var new1 = "see"var new2 = theword;var url = "test.php";$.post(url,{ update: update, new1: new1, new2: new2 }  ,  function(data) {output.innerHTML += data+'<br />';});var timer = setTimeout("text_info('"+theword+"')",5000); // every 1000 = 1 sec, so 5000 = 5sec}</script><style type="text/css">#text_output {display:block; }</style></head><body><div id ="text_output"></div><a href="#" onclick="return false" onmousedown="javascript:text_info('<?php echo $the_word; ?>');">start</a></body></html>

Link to comment
Share on other sites

  • Replies 58
  • Created
  • Last Reply

Top Posters In This Topic

i got a bug... PHP this is in a while loop so the value is difrent every time it is clicked and php will print this many times

<?php<a href="#" onclick="return false" onmousedown="javascript:text_info(\'' . $the_word . '\');">start</a>?>

JAVASCRIPT

function text_info(theword) {output = document.getElementById("text_output");$("#text_output").text("updating please wait...").show();var new1 = <?php echo $new1 ?>;var new2 = theword;var update = "update"var url = "../the_work.php";$.post(url,{ update: update, new1: new1, new2: new2 }  ,  function(data) {output.innerHTML = data;});var timer = setTimeout("text_info('"+theword+"')",5000); }

when i click on the 1st php link it works fine and timer works too. but when i click on the next php link the load works but the timer sends the wrong info to run every 5 sec and it loads the 1st link and then the second link every 5 sec if i could drop the old verible in..var timer = setTimeout("text_info('"+theword+"')",5000);and use the newest verible posted to the function it could work but how do i do that?

Edited by westman
Link to comment
Share on other sites

<script language="javascript" type="text/javascript">var timer="";function text_info( theword) {clearTimeout(timer);output = document.getElementById("text_output");var update = "update"var new1 = "hello"var new2 = theword;var url = "test.php";$.post(url,{ update: update, new1: new1, new2: new2 }  ,  function(data) {output.innerHTML += data+'<br />';});output.scrollTop=output.offsetHeight;timer = setTimeout("text_info('"+theword+"')",2000); // every 1000 = 1 sec, so 5000 = 5sec}</script>

Link to comment
Share on other sites

Yea! use the script i gave, BUT! correctly, I've got three links, with three different values for the word, it displays the word and other text sent to php, and returned back every set number of seconds, you click second link that 'the_word' value replaces the first, and starts listing that under the previous listing every set number of seconds. So it works, you are not applying it correctly somewhere, I don't know where cause you do not provide the source to determine where the code is not correctly functioning as it should! you just back with, 'it does not work for me! can we try another way' answer from me NO! this works fine.

Link to comment
Share on other sites

this script is working well without...var timer = setTimeout("text_info('"+chatuser+"')",5000); inside <div id ="text_output"> will be info sent from the DB (updated all the time) and the info will change depending on what word is sent to the_work.php.at the moment if a click on...<?php<a href="#" onclick="return false" onmousedown="javascript:text_info(\'' . $the_word . '\');">start</a>?>(echoed to the page by php) it will do what var timer = setTimeout("text_info('"+chatuser+"')",5000); with out the bug (but you need to click to update its not aoto) if i use var timer = setTimeout("text_info('"+chatuser+"')",5000); i can click on my 1st link and all is good. but when i click on the next link, the <div id ="text_output"> will show info from the DB for the second link for 5 sec then show info from the DB for the 1st link, repetedly. if the <div id ="text_output"> did not swich from 1 to the over it would work fine is they a way that we can stop the swiching from 1 to anuther?am geusing there is more than 1 verible been posted in...var timer = setTimeout("text_info('"+chatuser+"')",5000);

Edited by westman
Link to comment
Share on other sites

just tested it with more links...and it it sending moer that verible and thats way it swiches. if

var timer = setTimeout("text_info('"+chatuser+"')",5000);

only sends 1 verible it will workhow do i stop this saveing all the veribles from the links i click on? "theword" holds 1 word and one word verible only but at the moment "theword" is been apended to every time a new like is clicked

Link to comment
Share on other sites

  • 2 weeks later...

If you're talking about the element that shows the text on the page, this line tells it to add the data to the current text, not replace the current text with the data: output.innerHTML += data+'<br />'; The += operator tells it to append the data to the current text. By the way, the word is "variable", not "verible".

Link to comment
Share on other sites

but even when i tested it withoutput.innerHTML = data;});it was still appending. i think it is hapening fromvar timer = setTimeout("text_info('"+theword+"')",5000);and tryed to remove the plus sgin be would not work

Link to comment
Share on other sites

That's not going to append data to innerHTML, that's going to overwrite innerHTML with whatever is in data. Alert the value of data to check if that value has the extra text, which would be an issue in PHP.

Link to comment
Share on other sites

If you want to use the various string methods to split that value up you can do that, although that doesn't sound like it solves the actual problem. Is the value coming back from PHP with everything appended to it? Have you tested that?

Link to comment
Share on other sites

definetly no problem with php 100% safe ;)to summeris, i have this so fear... php (inside <?php ?>)

<a href="#" onclick="return false" onmousedown="javascript:text_info(\'' . $the_word . '\');">start</a>

javascript

<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script><script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.4/jquery-ui.min.js"></script><script language="javascript" type="text/javascript"> //var timer = clearTimeout();//var timer = "";function text_info( theword) { output = document.getElementById("text_output"); var update = "update"var new1 = <?php echo $new1 ?>;var new2 = theword; var url = "test.php";$.post(url,{ update: update, new1: new1, new2: new2 }  ,  function(data) {output.innerHTML = data;});var timer = setTimeout("text_info('"+theword+"')",5000); // every 1000 = 1 sec, so 5000 = 5sec//var timer = clearTimeout();//var timer = "";}</script> 

somewhere in here the value coming from php is been appended to, but where?if the javascript could forget the old variable ($the_word from php) every time the butten in php is clicked, and uses the new variable ($the_word from php) it would work fine, but it is not so. also when the same variable ($the_word from php) is clicked more than ones you can see it load twice, and the more you click the same variable ($the_word from php) the more you can see it load on the page looking like a strob light (flashing).

Edited by westman
Link to comment
Share on other sites

personally, I find quite a few flaws in the logic, or I just don't get what you are trying to do. you are hardcoding values to always be sent as static with new1, and new2. I don't see how you would expect the response to be different if you keep making the same request;

also when the same variable ($the_word from php) is clicked more than ones you can see it load twice, and the more you click the same variable ($the_word from php) the more you can see it load on the page looking like a strob light (flashing).
well that makes sense, you start a new timer everytime you call the text_info function. the more you click, the more request you make, and the more intervals get triggered to call the function again. Honestly, I think you should step back and think about exactly what you are trying to accomplish, and write it out in short, specfic steps. Steps that you could code against. Think very carefully about what kind of request you are making, and what the response should be. Carefully think out the logic and draw it if you have to. You should be debugging at every step to make sure that what you are sending and getting is exactly what you want. It seems like you are making a lot of assumptions. Edited by thescientist
Link to comment
Share on other sites

somewhere in here the value coming from php is been appended to, but where?
I can't tell, you didn't show the code that creates $the_word.
if the javascript could forget the old variable ($the_word from php) every time the butten in php is clicked, and uses the new variable ($the_word from php) it would work fine, but it is not so.
Javascript doesn't "forget" anything, it does exactly what you tell it to do and nothing else. When you click on a link to run a Javascript function it is going to pass whatever value you are telling it to pass. If you want that value to change then you need to change it. It sounds like you're not doing a lot of debugging in PHP to figure out where the problem is.
also when the same variable ($the_word from php) is clicked more than ones you can see it load twice, and the more you click the same variable ($the_word from php) the more you can see it load on the page looking like a strob light (flashing).
Right, that's because that's what you're telling Javascript to do. Every time you click on the link you're telling it to send an ajax request to the server to update the page, and then you're telling it to run the same function again 5 seconds later. So you create a new 5 second loop every time you click on the link. If you click on the link 10 times then every 5 seconds you're running the function another 10 times. That's what the code is telling it to do. You're also not telling it to use different data, you're telling it to run the function with the same value of the_word that the first one used. Just add some debugging statements to help you figure out what the thing is doing. It sounds like you're confused.
function text_info( theword) {  output = document.getElementById("text_output");  var update = "update"  var new1 = <?php echo $new1 ?>;  var new2 = theword;  var url = "test.php";  alert('sending request to ' + url + ' with update="' + update + '"; new1="' + new1 + '"; new2="' + new2 + '"');  $.post(url,{ update: update, new1: new1, new2: new2 } , function(data) {    alert('got response from server, updating page with ' + data);    output.innerHTML = data;  });  alert("scheduling text_info('"+theword+"') to run in 5 seconds");  var timer = setTimeout("text_info('"+theword+"')",5000); // every 1000 = 1 sec, so 5000 = 5sec}

Link to comment
Share on other sites

i see wow thank you for the alert coding that was help full to see.i am now understanding a lot beter on how this code is working and the logic in it.there is no appending in the code it just runs the timer every time it is asked to and never drops the 1st timer. if i could drop the 1st timer each time the button in php is clicked that would work well, coz the 1st timer holds the old variable ($the_word from php).as you can see from my code above i tryed to use

timer = clearTimeout();

but with no luck the function of this javascript that i am trying to debug is a live chat refresh <div> (the live chat is in a div) and it needs atuo refreshing every 5 sec. $new1 is the users id from php, and $new2 is the variable ($the_word from php the users id you whould like to talk to) what am trying to do is restart all the data in the javascript each time the button is clicked, i think to clear out the timer would help a lot however i tested many times with

timer = clearTimeout();

in different places in the code but it still did not work

Link to comment
Share on other sites

Honestly, you should really just take a step back and work this thing out one step at a time. A chat system isn't that complex depending on your expectations, but you have to be clear and direct about the steps that you execute in order to accomplish it. First step would be to get something that a user input onto the page to get sent to the server and confirm that it was successful. (i.e. getting a response in the form of an updated text file, or something similar, whatever mechanism it is that you are using to persist the conversation). Second would be to have a function that gets the output (like in step one) of the chat and updates a section of the page with it when you call it (say for now just on a button click) Now confirm that when you submit something, you get the correct response, and that you see the chat being updated accordingly on the page. Three, you can integrate your timer. You can switch it from a dummy button click to having the timer start when the page loads, and verify that it loads the chat every few seconds. If you want to reset the timer when someone submits, then you need to assign the original timer's id return value to a variable so you can clear it out, and then initiate a new timer that returns it's id to that same variable.

Edited by thescientist
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...