Shadowing Posted March 8, 2013 Share Posted March 8, 2013 (edited) 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 March 8, 2013 by Shadowing Quote Link to comment https://forums.phpfreaks.com/topic/275318-odd-problem-passing-variabels/page/2/#findComment-1417395 Share on other sites More sharing options...
Shadowing Posted March 8, 2013 Share Posted March 8, 2013 Is li.php is in your chat folder? or is it in your htdocs folder? Quote Link to comment https://forums.phpfreaks.com/topic/275318-odd-problem-passing-variabels/page/2/#findComment-1417397 Share on other sites More sharing options...
MDobleZ Posted March 8, 2013 Author Share Posted March 8, 2013 The php is in my chat folder. Same folder as the js and the html. Quote Link to comment https://forums.phpfreaks.com/topic/275318-odd-problem-passing-variabels/page/2/#findComment-1417402 Share on other sites More sharing options...
Shadowing Posted March 8, 2013 Share Posted March 8, 2013 (edited) I always take ajax links from the document root so in your case chat/li.php Edited March 8, 2013 by Shadowing Quote Link to comment https://forums.phpfreaks.com/topic/275318-odd-problem-passing-variabels/page/2/#findComment-1417412 Share on other sites More sharing options...
MDobleZ Posted March 8, 2013 Author Share Posted March 8, 2013 I actually get an "not found" error in the developers console is I add "chat/". Quote Link to comment https://forums.phpfreaks.com/topic/275318-odd-problem-passing-variabels/page/2/#findComment-1417511 Share on other sites More sharing options...
Shadowing Posted March 8, 2013 Share Posted March 8, 2013 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 Quote Link to comment https://forums.phpfreaks.com/topic/275318-odd-problem-passing-variabels/page/2/#findComment-1417513 Share on other sites More sharing options...
MDobleZ Posted March 8, 2013 Author Share Posted March 8, 2013 Still the js code, there are still a few things I'd like to try before giving up on it. Quote Link to comment https://forums.phpfreaks.com/topic/275318-odd-problem-passing-variabels/page/2/#findComment-1417515 Share on other sites More sharing options...
MDobleZ Posted March 8, 2013 Author Share Posted March 8, 2013 Sorry I didn't asnwer all the questions. I have been able to used the same file to pass a variable to the db, so everything is in place. Quote Link to comment https://forums.phpfreaks.com/topic/275318-odd-problem-passing-variabels/page/2/#findComment-1417516 Share on other sites More sharing options...
Shadowing Posted March 8, 2013 Share Posted March 8, 2013 echo this on the php file echo ('path to file li.php is '.__FILE__); tell me what you get is chrome the only browser you have tried? Quote Link to comment https://forums.phpfreaks.com/topic/275318-odd-problem-passing-variabels/page/2/#findComment-1417522 Share on other sites More sharing options...
MDobleZ Posted March 8, 2013 Author Share Posted March 8, 2013 path to file li.php is /home/vol1/getfreehosting.co.uk/getfh_6562567/htdocs/chat/li.php Got the same results with IE 8. Quote Link to comment https://forums.phpfreaks.com/topic/275318-odd-problem-passing-variabels/page/2/#findComment-1417525 Share on other sites More sharing options...
Shadowing Posted March 8, 2013 Share Posted March 8, 2013 (edited) 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 March 8, 2013 by Shadowing Quote Link to comment https://forums.phpfreaks.com/topic/275318-odd-problem-passing-variabels/page/2/#findComment-1417528 Share on other sites More sharing options...
MDobleZ Posted March 8, 2013 Author Share Posted March 8, 2013 I do get frog and ted, Shadowing. Quote Link to comment https://forums.phpfreaks.com/topic/275318-odd-problem-passing-variabels/page/2/#findComment-1417544 Share on other sites More sharing options...
Shadowing Posted March 8, 2013 Share Posted March 8, 2013 (edited) 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 March 8, 2013 by Shadowing Quote Link to comment https://forums.phpfreaks.com/topic/275318-odd-problem-passing-variabels/page/2/#findComment-1417547 Share on other sites More sharing options...
MDobleZ Posted March 8, 2013 Author Share Posted March 8, 2013 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)? Quote Link to comment https://forums.phpfreaks.com/topic/275318-odd-problem-passing-variabels/page/2/#findComment-1417553 Share on other sites More sharing options...
Shadowing Posted March 8, 2013 Share Posted March 8, 2013 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. Quote Link to comment https://forums.phpfreaks.com/topic/275318-odd-problem-passing-variabels/page/2/#findComment-1417554 Share on other sites More sharing options...
MDobleZ Posted March 8, 2013 Author Share Posted March 8, 2013 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. Quote Link to comment https://forums.phpfreaks.com/topic/275318-odd-problem-passing-variabels/page/2/#findComment-1417555 Share on other sites More sharing options...
Shadowing Posted March 8, 2013 Share Posted March 8, 2013 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. Quote Link to comment https://forums.phpfreaks.com/topic/275318-odd-problem-passing-variabels/page/2/#findComment-1417557 Share on other sites More sharing options...
Solution MDobleZ Posted March 8, 2013 Author Solution Share Posted March 8, 2013 I did, it did and I already echoed the password for my username. Now I have to try the whole code and see what happens, so I'm tagging this as solved. Thanks again for your help. Quote Link to comment https://forums.phpfreaks.com/topic/275318-odd-problem-passing-variabels/page/2/#findComment-1417629 Share on other sites More sharing options...
Shadowing Posted March 9, 2013 Share Posted March 9, 2013 One thing though make sure you escape WHERE nick='".mysql_real_escape_string($q)."'"); Quote Link to comment https://forums.phpfreaks.com/topic/275318-odd-problem-passing-variabels/page/2/#findComment-1417665 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.