westman Posted March 17, 2012 Share Posted March 17, 2012 hi all,am trying to send info from and to php using AJAX i have this so faer... <script language="javascript" type="text/javascript">function text_info() {output = document.getElementById("text_output");var update = updatevar url = "the_work.php";$.post(url,{ update: update() } , function(data) {output.innerHTML = data;});}</script> and php... if ($_POST['update'] == "update") {echo "updating please wait...";} but its not working,any help? Link to comment Share on other sites More sharing options...
niche Posted March 17, 2012 Share Posted March 17, 2012 http://www.w3schools.com/php/php_ajax_php.asp Focus on "Example Explained - The HTML Page". Link to comment Share on other sites More sharing options...
westman Posted March 18, 2012 Author Share Posted March 18, 2012 i read this and am still no closer to corecting my codeany help? Link to comment Share on other sites More sharing options...
dsonesuk Posted March 18, 2012 Share Posted March 18, 2012 (edited) (1) first of all, since it is using JQuery, do you have <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> or something similar at the top of page to reference JQ libraries. (2) var update = update is equalling itself a variable this looks for string value of "update" if ($_POST['update'] == "update") { SO it should be var update = "update"; (3)update: update() the second 'update()' is set to run a function update(), but update is not a function, it is a variable, so it should be { update: update } the first is the reference given to $_POST['update'] Edited March 18, 2012 by dsonesuk Link to comment Share on other sites More sharing options...
westman Posted March 18, 2012 Author Share Posted March 18, 2012 ok i edited my code a bit and i came up with this.. <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(){output = document.getElementById("text_output");var update = "update";var url = "the_work.php";$.post(url,{ update: update } , function(data) {output.innerHTML = data;});}</script> but its still not geting the info from php and sendint it to <div id = "text_output"></div> Link to comment Share on other sites More sharing options...
dsonesuk Posted March 18, 2012 Share Posted March 18, 2012 How, where are you triggering the function call? Link to comment Share on other sites More sharing options...
westman Posted March 18, 2012 Author Share Posted March 18, 2012 <a href="#" onclick="return false" onmousedown="javascript:text_info();">start</a> Link to comment Share on other sites More sharing options...
dsonesuk Posted March 18, 2012 Share Posted March 18, 2012 (edited) Well, IF you have a element with exact id as id="text_output", it should work fine! I suppose the .php file is in the same location as file with this js/jquery code Edited March 18, 2012 by dsonesuk Link to comment Share on other sites More sharing options...
westman Posted March 18, 2012 Author Share Posted March 18, 2012 yer but am not too shore about the syntax in the last 2 lines $.post(url,{ update: update } , function(data) {output.innerHTML = data;}); is it ok? Link to comment Share on other sites More sharing options...
dsonesuk Posted March 18, 2012 Share Posted March 18, 2012 OK, from scratch html, or php file with jquery ajax code <!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><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 type="text/javascript">function text_info() {output = document.getElementById("text_output");var update = "update"var url = "the_work.php";$.post(url,{ update: update } , function(data) {output.innerHTML = data;});}</script></head><body><div id ="text_output"></div><a href="#" onclick="return false" onmousedown="javascript:text_info();">start</a></body></html> 'the_work.php' file will contain only <?phpif ($_POST['update'] == "update") {echo "updating please wait...";}?> Link to comment Share on other sites More sharing options...
westman Posted March 18, 2012 Author Share Posted March 18, 2012 omg its woring thank youthis scriped is a starting pont i will ad more $ laterto ad more $ to send to php do i set up like this?... var update = "update"var url = "the_work.php";var new1 = "hello"var new2 = "every one";$.post(url,{ update: update, new1: new1, new2: new2 } , function(data) {output.innerHTML = data;}); is that ok? Link to comment Share on other sites More sharing options...
dsonesuk Posted March 18, 2012 Share Posted March 18, 2012 Looks OK to me, and why did your script not work? compared to mine. Link to comment Share on other sites More sharing options...
westman Posted March 18, 2012 Author Share Posted March 18, 2012 i found that my URL needed "../" oppps sorry lats question how do i get this part of the script... $.post(url,{ update: update } , function(data) {output.innerHTML = data;});} to run every 5 sec after the 1st run? Link to comment Share on other sites More sharing options...
dsonesuk Posted March 18, 2012 Share Posted March 18, 2012 var timer = setInterval("text_info()",5000); // every 1000 = 1 sec, so 5000 = 5secfunction text_info() {output = document.getElementById("text_output");var update = "update"var url = "the_work.php";$.post(url,{ update: update } , function(data) {output.innerHTML = data;});} Link to comment Share on other sites More sharing options...
westman Posted March 19, 2012 Author Share Posted March 19, 2012 i tryed... function text_info() {output = document.getElementById("text_output");var update = "update"var url = "the_work.php"; var timer = setInterval("update()",5000);function update() {$.post(url,{ update: update } , function(data) {output.innerHTML = data;});}} to run the update every 5 sec and keep all the $ sent to text_info but it did not workis there anuter way? Link to comment Share on other sites More sharing options...
dsonesuk Posted March 19, 2012 Share Posted March 19, 2012 Well it wouldn't would it, since you have declared a function within a function, that is not allowed. return to my original script and change output.innerHTML = data;}); to output.innerHTML += data+'<br/>';}); Link to comment Share on other sites More sharing options...
thescientist Posted March 19, 2012 Share Posted March 19, 2012 you can't use a variable name for two different things. I'm sure you must have gotten some sort of error. Do you check your console? var update = "update" function update() {$.post(url,{ update: update } , function(data) {output.innerHTML = data;}); } Try something like this function text_info() { var output = document.getElementById("text_output"); var url = "the_work.php"; function update() { $.post(url, { update: "update" }, function(data) { output.innerHTML = data; }); } var timer = setInterval(update, 5000);}; Link to comment Share on other sites More sharing options...
dsonesuk Posted March 19, 2012 Share Posted March 19, 2012 ????$.post(url,{ update: update }update: = $_POST['update'];update = variable update,with the value of "update" passed to it. Link to comment Share on other sites More sharing options...
thescientist Posted March 19, 2012 Share Posted March 19, 2012 but also with a function called update? Link to comment Share on other sites More sharing options...
dsonesuk Posted March 19, 2012 Share Posted March 19, 2012 There's no conflict with function update(), they are all different so why should there be? true it might be better give it another name to understand what each does and avoid confusion, but other than that, there's no problem using as it is now. Link to comment Share on other sites More sharing options...
westman Posted March 19, 2012 Author Share Posted March 19, 2012 function text_info() { var output = document.getElementById("text_output"); var url = "the_work.php"; function update() { $.post(url, { update: "update" }, function(data) { output.innerHTML = data; }); } var timer = setInterval(update, 5000);}; is not working + iv try to change the veribels and still cant get the orto refrech from the_work.php any help? Link to comment Share on other sites More sharing options...
dsonesuk Posted March 19, 2012 Share Posted March 19, 2012 <script type="text/javascript">var timer = setInterval("text_info()",5000); // every 1000 = 1 sec, so 5000 = 5secfunction text_info() {output = document.getElementById("text_output");var update = "update"var new1 = "hello"var new2 = "every one";var url = "the_work.php";$.post(url,{ update: update, new1: new1, new2: new2 } , function(data) {output.innerHTML += data+'<br />';});}</script> php file with date to show it working. <?phpif ($_POST['update'] == "update") {echo "updating please wait...".$_POST['new1']." ".$_POST['new2'].date("d-m-Y-H-m-s");}?> Link to comment Share on other sites More sharing options...
westman Posted March 19, 2012 Author Share Posted March 19, 2012 that would work lovely if i did not have 1 of my veribles posted to function text_info(theword) and when i run the script on the timer the verible is not saved in the functionis they a way to save a verible in a function? if so your colde could work Link to comment Share on other sites More sharing options...
dsonesuk Posted March 19, 2012 Share Posted March 19, 2012 IF you explain where this variable value is coming from, where it is going to be used, then probably yes! what you keep giving is a function that passes no variables to anywhere but the ajax post script, and where only one function is required to do so, and that is why i give you the one function solution, so i have no idea what you are talking about and how to give you a clear answer. Link to comment Share on other sites More sharing options...
westman Posted March 19, 2012 Author Share Posted March 19, 2012 PHP <a href="#" onclick="return false" onmousedown="javascript:text_info(\'' . $the_word . '\');">start</a> javascript //var timer = setInterval("text_info()",5000);function text_info(theword) {output = document.getElementById("text_output");<!--document.output.value = "";-->$("#text_output").text("updating please wait...").show();var new1 = <?php echo $new1 ?>;var new2 = theword;var update = "update"var url = "../the_work.php";//var timer = setInterval("update()",5000);//function update() {$.post(url,{ update: update, new1: new1, new2: new2 } , function(data) {output.innerHTML = data;});//var timer = setInterval("update()",5000);} this is fully working so faer but if i add the timer the verible new2 is not savedhow do i save the verible new2 in the function so the timer will work ok? Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now