0

I have a javascript page that is supposed to send the username that the user enters to a php script on the server. The javascript page comes from http://192.168.1.4/login.html and it tries to access a php script at http://192.168.1.4/GetInfo.php. I think that I cannot access the username in the php script from the javascript page because of the same origin policy in firefox however, I'm not sure how to confirm this suspicion so please forgive me if I am wrong. I have only just begun to learn javscript and php. I was wondering if there is a different way to pass this information then. The code is below. Thanks!

The javascript:

<html>
    <head>
        <title>Login Page for SplitAuth</title>
    </head>
    <script language="javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.js"></script>
    <script language="Javascript">
        function getUsername()
        {
            var username = window.prompt('Please Type Your Username');
            var temp = document.getElementById('temp');
            temp.innerHTML = username;
            jQuery.ajax(
                {
                    type: "POST",
                    url:"GetInfo.php",
                    data: username,
                    success: function(msg)
                            {alert("data Saved: "+msg);}

                });//ends the jQuery send

        }//ends the GetUsername function
    </script>
    <body onLoad=getUsername()>
        <div id="temp">This will show text</div>
    <body>

</html>

The php script:

<?
$inFile="MyID.config.php";
$handle=fopen($inFile, 'r') or die ("No credentials could be gotten because the file MyID.config.php would not open.");

echo $_POST['msg'];

fclose($fh);

?>
6
  • What do you get if you visit 192.168.1.4/GetInfo.php in your browser? Commented Apr 20, 2012 at 21:53
  • is the php script working directly? Commented Apr 20, 2012 at 21:54
  • Since javascript runs client side, 192... will not work unless the machine you're running the ajax request on is the server itself. Also data: username is wrong -- should be data: "username=" + username I think -- but then you don't reference it in PHP either. Your PHP code makes no sense at all .. hard to tell what you're trying to do. Commented Apr 20, 2012 at 21:55
  • make one wonder .. how old that article was, from which you copy-pasted the script Commented Apr 20, 2012 at 21:57
  • 1
    @tereško actually I wrote them myself as I said I AM LEARNING so instead or making a stupid comment like that next time say something constructive or don't say anything at all Commented Apr 20, 2012 at 22:15

3 Answers 3

3

You should prepend your data: with "msg=".

...
data: "msg="+username,
...

and the reason is that jQuery.ajax expects a query string or an object, which means that

...
data: {msg: username},
...

would also work.

Take a look at the jQuery.ajax Documentation. Specifically the data-section

4
  • Thanks! That worked but I want to clarify something. I thought that Javascript was supposed to be smart enough to know when a variable is a string and when it is not so I thought having a variable that was a string (or so that I thought) was good enough. Commented Apr 20, 2012 at 22:10
  • It is not enough for it to be a string, it has to be a query string. And specifically a query string with the key msg and the contents of username as the value. The reason the key has to be msg is that your PHP code uses this key. Commented Apr 20, 2012 at 22:13
  • 2
    you are right that javascript is loosely typed and so usually picks up on implied data types by the values that you assign to a variable, but in this case it's not about what data type the variable is - it's about what the server expects to receive, if you want a bit more detail you can read up on querystrings here: en.wikipedia.org/wiki/Query_string Commented Apr 20, 2012 at 22:20
  • 1
    Thank you, I didn't realize I was building the part of the URL after the "?" which obviously denotes the variables being sent. Thanks for your help! Commented Apr 20, 2012 at 22:40
0

You are using POST method and data that you are sending is wrong. You need to build data to be sent. Take a look at jquery page in ajax method. This is what it says for data attribute.

http://api.jquery.com/jQuery.ajax/

Data to be sent to the server. It is converted to a query string, if not already a string. It's appended to the url for GET-requests. See processData option to prevent this automatic processing. Object must be Key/Value pairs. If value is an Array, jQuery serializes multiple values with same key based on the value of the traditional setting (described below).

0
-1

Your script should look like this.

 $.ajax({
  type: "POST",
  url: "GetInfo.php",
  data: "{name:"+ username + "}"
  }).done(function( msg ) {
 alert( "Data Saved: " + msg );
 });

And your php script isn't getting the username value. The "msg" used in your $_POST['msg'] will alert nothing because it is not having any value. The "msg" varaible is meant to store the value of the returned value to your html page. I will recommend you read more of from www.jquery.com

6
  • minus the backticks i assume? Commented Apr 20, 2012 at 22:24
  • actually wait a mo, the value for data in the ajax call looks wrong to me. that's a string, not an object. dont think that will work. Commented Apr 20, 2012 at 22:25
  • @jammypeach, the backticks is not part of the script. And the only object needed in the above script is username, the lable could be pass as string. On a second thought, "$.Ajax" is not for posting a form, you should use "$.Post". Commented Apr 20, 2012 at 22:32
  • sorry but that's not right. The curly braces here: "{name:"+ username + "}" result in a request that looks like this (if it were a GET, for instance): someUrl.com?{name:value} which is not a valid request. When you make the ajax function call, the input for data must be an object or a valid querystring, not a string that looks like an object. Don't beleive me? read this: api.jquery.com/jQuery.ajax Commented Apr 20, 2012 at 22:41
  • @jammypeach thanks for the correction. That was an overlook error. Commented Apr 22, 2012 at 14:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.