Jump to content

AJAX send and recive info from php


westman

Recommended Posts

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

  • Replies 58
  • Created
  • Last Reply

Top Posters In This Topic

(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 by dsonesuk
Link to comment
Share on other sites

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

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

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

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

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

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

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

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

<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

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

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

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

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...