MDobleZ Posted March 6, 2013 Share Posted March 6, 2013 I'm trying to do something which I've done before but for some reason (which has to be a silly one) it doesn't work now. This is the js: var userName; var password; function logIn(){ userName=document.f.un.value; password=document.f.pa.value; xmlhttp2=new XMLHttpRequest(); xmlhttp2.open("POST","li.php",true); xmlhttp2.setRequestHeader("Content-type","application/x-www-form-urlencoded"); xmlhttp2.send("q="+userName+"&r="+password); document.getElementById("iden").style.visibility='hidden'; } (I know the variables are working, because if I add alert I see them.) And this is the php: <?php $q=$_POST["q"]; $r=$_POST["r"]; $con2=mysql_connect("","",""); mysql_select_db("",$con2); $result=mysql_query("SELECT pass FROM chat WHERE nick='$q'"); $row=mysql_fetch_array($result); echo $row[0]; mysql_close($con2); ?> I know the function is working because if a change $_POST["q"] by $_POST["any nick"], it echoes the pass. Quote Link to comment https://forums.phpfreaks.com/topic/275318-odd-problem-passing-variabels/ Share on other sites More sharing options...
AyKay47 Posted March 6, 2013 Share Posted March 6, 2013 (edited) What exactly do you mean by "it doesn't work", what is happening? Have you tried simply outputting $_POST['q'] and $_POST['r'] to see if the values are correct? Also as a side note, I would recommend using the JavaScript framework JQuery especially when wanting to use AJAX, but also in general; turns 10 lines of code or so into 1 line. Make sure not to use arbitrary user data directly in an SQL statement. Make sure to pass the user input through mysql_real_escape_string to make it safe to pass into the SQL statement. Edit: Also, if you decide not to use JQuery, you will need a try block to determine which http request object the browser uses; IE until version 7 used an Active X Object. Edited March 6, 2013 by AyKay47 Quote Link to comment https://forums.phpfreaks.com/topic/275318-odd-problem-passing-variabels/#findComment-1417032 Share on other sites More sharing options...
MDobleZ Posted March 7, 2013 Author Share Posted March 7, 2013 I mean it doesn't echo anything and I know the reason is the variables are not getting to the php file. I simply don't know why. I'm using chrome, so the request is fine Quote Link to comment https://forums.phpfreaks.com/topic/275318-odd-problem-passing-variabels/#findComment-1417065 Share on other sites More sharing options...
Shadowing Posted March 7, 2013 Share Posted March 7, 2013 (edited) I've never used ajax with out jquery. But for kicks try /li.php With adding the / Edited March 7, 2013 by Shadowing Quote Link to comment https://forums.phpfreaks.com/topic/275318-odd-problem-passing-variabels/#findComment-1417066 Share on other sites More sharing options...
teynon Posted March 7, 2013 Share Posted March 7, 2013 I mean it doesn't echo anything and I know the reason is the variables are not getting to the php file. I simply don't know why. I'm using chrome, so the request is fine The request is fine because you saw it send the request in the developer / network tab? (In other words, is the network tab reporting the request as successful?) If so, click on the request in Chrome and see what the output is. Quote Link to comment https://forums.phpfreaks.com/topic/275318-odd-problem-passing-variabels/#findComment-1417070 Share on other sites More sharing options...
Shadowing Posted March 7, 2013 Share Posted March 7, 2013 (edited) Ahh I missed him saying that he successfully echoed it. But idk I think he meant if he gives _post a value on the php page the php function works and not that he retrieved anything via _post Edited March 7, 2013 by Shadowing Quote Link to comment https://forums.phpfreaks.com/topic/275318-odd-problem-passing-variabels/#findComment-1417071 Share on other sites More sharing options...
teynon Posted March 7, 2013 Share Posted March 7, 2013 Random thought, if you'd like, I can take a look and at the same time show you how to debug some of this in Chrome. (Chrome Remote Desktop: https://chrome.google.com/webstore/detail/chrome-remote-desktop/gbchcmhmhahfdphkhkmpfmihenigjmpp?utm_source=gmail) Quote Link to comment https://forums.phpfreaks.com/topic/275318-odd-problem-passing-variabels/#findComment-1417073 Share on other sites More sharing options...
MDobleZ Posted March 7, 2013 Author Share Posted March 7, 2013 You're right shadowing, that's what I meant, but adding "/" did not do it. It actually gave me an error message in the developer / network tab mentioned by teynon. I've already done this in the past with an almost identical code, so it has to be a stupid mistake. Quote Link to comment https://forums.phpfreaks.com/topic/275318-odd-problem-passing-variabels/#findComment-1417074 Share on other sites More sharing options...
Shadowing Posted March 7, 2013 Share Posted March 7, 2013 What is the error? Quote Link to comment https://forums.phpfreaks.com/topic/275318-odd-problem-passing-variabels/#findComment-1417076 Share on other sites More sharing options...
AyKay47 Posted March 7, 2013 Share Posted March 7, 2013 I don't see anything wrong with the AJAX POST request besides the fact that you are not checking for several key triggers, but this will not affect the actual call. You should be encoding any data that is to be passed through a url/uri via encodeURIComponent() Since you are using a relative path to the landing page, make sure that it is in fact in the same directory as the calling page. What errors if any are being displayed in chromes developer tools? Quote Link to comment https://forums.phpfreaks.com/topic/275318-odd-problem-passing-variabels/#findComment-1417079 Share on other sites More sharing options...
MDobleZ Posted March 7, 2013 Author Share Posted March 7, 2013 No errors at all, it's just the variables are not reaching the php. They are in fact in the same directory. Quote Link to comment https://forums.phpfreaks.com/topic/275318-odd-problem-passing-variabels/#findComment-1417181 Share on other sites More sharing options...
AyKay47 Posted March 7, 2013 Share Posted March 7, 2013 (edited) Try changing the AJAX call to a GET method and see if you are able to retrieve the data that way: xmlhttp2=new XMLHttpRequest(); xmlhttp2.onreadystatechange = function() { if(xmlhttp2.readyState == 4) { alert("success"); } } xmlhttp2.open("GET","li.php?q=" + userName + "&r=" + password, true); xmlhttp2.setRequestHeader("X-Requested-With", "XMLHttpRequest"); xmlhttp2.send(null); In the PHP landing page make sure to change $_POST to $_GET Edited March 7, 2013 by AyKay47 Quote Link to comment https://forums.phpfreaks.com/topic/275318-odd-problem-passing-variabels/#findComment-1417185 Share on other sites More sharing options...
Shadowing Posted March 7, 2013 Share Posted March 7, 2013 (edited) I would just use jquery include this link in the <head></head> of your page. <script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js" ></script> Then use jquery ajax frame work below. I filled you information in for ya. Also a tip when creating varaibles i would really use all lowercase letters instead of doing userName. Be better to do user_name $.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) { // anything you want return from php page } }); Edited March 7, 2013 by Shadowing Quote Link to comment https://forums.phpfreaks.com/topic/275318-odd-problem-passing-variabels/#findComment-1417193 Share on other sites More sharing options...
AyKay47 Posted March 7, 2013 Share Posted March 7, 2013 Using JQuery has already been suggested and the OP has chosen to ignore it, so we proceed with JavaScript. Quote Link to comment https://forums.phpfreaks.com/topic/275318-odd-problem-passing-variabels/#findComment-1417194 Share on other sites More sharing options...
Shadowing Posted March 7, 2013 Share Posted March 7, 2013 Oh well I guess i decided to push jquery on him even harder? Its possible he ignored it cause he didnt want to look up how to use jquery. So i brought it back up in the conversation with exactly what he needs incase he decided to use it. Quote Link to comment https://forums.phpfreaks.com/topic/275318-odd-problem-passing-variabels/#findComment-1417204 Share on other sites More sharing options...
MDobleZ Posted March 7, 2013 Author Share Posted March 7, 2013 As an amateur (a flagrant one), I try to avoid using anything unless I find it absolutely necessary, and until now, I had never had problems with javascript. I guess it's time for a change, but I would still like to know what is going wrong with the original code; I'm certain it has to be something silly. Quote Link to comment https://forums.phpfreaks.com/topic/275318-odd-problem-passing-variabels/#findComment-1417228 Share on other sites More sharing options...
Manixat Posted March 7, 2013 Share Posted March 7, 2013 (edited) as said above Have you tried simply outputting $_POST['q'] and $_POST['r'] to see if the values are correct?You didn't answer P.S. echo those, not your $row[0] Edited March 7, 2013 by Manixat Quote Link to comment https://forums.phpfreaks.com/topic/275318-odd-problem-passing-variabels/#findComment-1417271 Share on other sites More sharing options...
MDobleZ Posted March 7, 2013 Author Share Posted March 7, 2013 Manixat, I tried and it didn't work so I thought the variables were not getting to the php. But then I modified the php to insert the variables in the database and it worked, so this keeps getting better and better. Quote Link to comment https://forums.phpfreaks.com/topic/275318-odd-problem-passing-variabels/#findComment-1417275 Share on other sites More sharing options...
Manixat Posted March 7, 2013 Share Posted March 7, 2013 (edited) when you echo post Q and post R it is not supposed to "work". It's supposed to either return P and Q or not. If nothing is returned then clearly the problem is in delivering. If they get returned though, as I suppose, the problem is in your sql handling Edited March 7, 2013 by Manixat Quote Link to comment https://forums.phpfreaks.com/topic/275318-odd-problem-passing-variabels/#findComment-1417277 Share on other sites More sharing options...
MDobleZ Posted March 7, 2013 Author Share Posted March 7, 2013 Nothing is ever returned. But, as I said before, the variables do get to the php and from there to the mysql db. I agree the problem must be in the sql part of the code, but I can simply not spot it. Quote Link to comment https://forums.phpfreaks.com/topic/275318-odd-problem-passing-variabels/#findComment-1417303 Share on other sites More sharing options...
Shadowing Posted March 7, 2013 Share Posted March 7, 2013 (edited) lets make a few changes place this at the top of your page ini_set("display_errors", "1");error_reporting(-1); change your data base interaction from $result=mysql_query("SELECT pass FROM chat WHERE nick='$q'"); $row=mysql_fetch_array($result); to $query = "SELECT pass FROM chat WHERE nick='$q'"; $result = mysql_query($query) or die(mysql_error()); $row = mysql_fetch_array($result); echo "query string is $query <br />"; echo "db return is ".$row['pass']." <br />"; paste what you echo from that Edited March 7, 2013 by Shadowing Quote Link to comment https://forums.phpfreaks.com/topic/275318-odd-problem-passing-variabels/#findComment-1417308 Share on other sites More sharing options...
teynon Posted March 8, 2013 Share Posted March 8, 2013 What was the error you got in the Chrome Network tab? Do you still get the error? As for jQuery / javascript... jQuery is a library built on javascript. So "learning" jQuery is actually going to help you learn javascript and simultaneously make your life easier. Quote Link to comment https://forums.phpfreaks.com/topic/275318-odd-problem-passing-variabels/#findComment-1417376 Share on other sites More sharing options...
MDobleZ Posted March 8, 2013 Author Share Posted March 8, 2013 This is what is echoed:Notice: Undefined index: q in /home/vol1/getfreehosting.co.uk/getfh_6562567/htdocs/chat/li.php on line 3Notice: Undefined index: r in /home/vol1/getfreehosting.co.uk/getfh_6562567/htdocs/chat/li.php on line 4query string is SELECT pass FROM chat WHERE nick='' db return is Quote Link to comment https://forums.phpfreaks.com/topic/275318-odd-problem-passing-variabels/#findComment-1417383 Share on other sites More sharing options...
teynon Posted March 8, 2013 Share Posted March 8, 2013 That's what was in the Chrome output? In the Chrome network tab, click on the request and go to the headers tab and paste it here. Then, click on the "preview" tab and copy and paste that here. Quote Link to comment https://forums.phpfreaks.com/topic/275318-odd-problem-passing-variabels/#findComment-1417386 Share on other sites More sharing options...
MDobleZ Posted March 8, 2013 Author Share Posted March 8, 2013 No, teynon, sorry I didn't see your question. I was responding to shadowing's idea. I no longer get any error from chrome, it was only when I added "/" before "li.php" that I got an error. Quote Link to comment https://forums.phpfreaks.com/topic/275318-odd-problem-passing-variabels/#findComment-1417394 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.