Jump to content

Odd problem passing variabels


MDobleZ

Recommended Posts

If you can't figure chrome developer tools out can try this with jquery. "Even though you really need to use developer tools :p"

 

 

 

$.ajax({ url: "li.php", 
type: 'POST', 
dataType: 'json', 
data: { 
q: userName, 
r: password }, 
error: function(){ 
console.log("Oops... seems to be a problem retrieving data") 
}, 
success: function(response) {
var r = response.r;
var q = response.q;
alert("r is "+r+" and q is "+q);
} 
});
 

 

Then on your php page get rid of all echo's and insert at the very bottom of all your code

 

echo json_encode(array("r" => $_POST['r'], "q" => $_POST['q']));

 

This will return the Post back to your Dom

Link to post
Share on other sites

uhh thats wierd

 

Is that with the jquery code?

 

cause that error is telling you that your php file isnt in your chat folder

 

Is all your folder names and file names in lowercase letters cause its probably case sensitive

Link to post
Share on other sites

I decided to take some time out and test your code out

 

works fine for me in IE9, Firefox and chrome

 

 

so lets start from basics

 

save your javascript and php page somewhere

 

now on the orginal two pages get rid of everything accept

 

this on your javascript page

 

 

 
<script>
 
$(function(){
 
var user_name= 'ted';
 
var password= 'frog';
 
 
 
xmlhttp2=new XMLHttpRequest();
 
xmlhttp2.open("POST","li.php",true);
 
xmlhttp2.setRequestHeader("Content-type","application/x-www-form-urlencoded");
 
xmlhttp2.send("q="+user_name+"&r="+password);
 
});
 
</script>
 

 

 

and this on your php page

 

 

 

echo $_POST['r']." and ".$_POST['q'];
 

 

 

 

now open up chrome developer tools by clicking options at the very top right of your browser go down and click on tools and then developer tools

 

now refresh the page

click on network tab in the developer tools

and click on li.php on the list on the left

should display frog and ted

Link to post
Share on other sites

That means its working.

 

so we need to figure out whats differant on your other pages thats not making it work

 

are you calling your javascript with on document load

 

wraping your java script with

 

$(function(){ 
 
 
}); 
Link to post
Share on other sites

I'm beginning to think the mistake is even more stupid than I'd thought, but based on some phps I'd created before I expected to see the echoed string if I opened the php itself. I thought that was a good way of checking if it was working. Am I wrong (and stupid)?

Link to post
Share on other sites

what we are trying to do is send data from your java script page to the li.php page

 

 

so simply going to chat/li.php in your browser you are not sending any data from the java script page. You are simply just opening up li.php page.

 

Thats why you have to use developer tool to see what gets echoed on the li.php page

 

When you load the page with your java script on it. it sends that ajax call which opens up the li.php page for you but also sends your data to that page via POST. Developer tools lets you read whats on that page when it opens.

 

 

so if you use the code i just had you test and you just simply open chat/li.php it wouldnt echo anything. Infact you would get a warning saying variables doesnt exist.

Link to post
Share on other sites

Yeah, I knew that much. But I didn't know it was still the case after I'd sent the variables with the js function. I thought that if I either opened or refreshed the php after executing this function I would see the variables echoed. In any case, I'll try to do what I was originally trying to do and see if it works. For the time being, I appologize to everyone for having wasted your time and thank you very much for your help.     

Link to post
Share on other sites

Yah the information will be gone because the information was never on that page for starters.

 

when you use ajax it opens up its own instance of li.php

 

so having another li.php open isnt the same page.

 

 

Did you really use developer tools to see ted frog?

if so then it really did send that information to that page and you can use it in your query for your data base.

Link to post
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.