Jump to content

Odd problem passing variabels


Go to solution Solved by 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

Edited by Shadowing

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

Edited by Shadowing

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(){ 
 
 
}); 
Edited by Shadowing

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)?

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.

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.     

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.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

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