Jump to content

Odd problem passing variabels


MDobleZ
Go to solution Solved by MDobleZ,

Recommended Posts

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.
Link to comment
Share on other sites

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

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.

Link to comment
Share on other sites

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

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. 

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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

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

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.

Link to comment
Share on other sites

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.   

Link to comment
Share on other sites

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

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

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.

Link to comment
Share on other sites

This is what is echoed:
Notice: Undefined index: q in /home/vol1/getfreehosting.co.uk/getfh_6562567/htdocs/chat/li.php on line 3

Notice: Undefined index: r in /home/vol1/getfreehosting.co.uk/getfh_6562567/htdocs/chat/li.php on line 4
query string is SELECT pass FROM chat WHERE nick='' 
db return is

Link to comment
Share on other sites

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.