0

i am new to the stackflow and m having some problems while using jquery' json ajax encoded values from an associative array, the jquery script is doing nothing!

its a script that just does nothing but gives you back both username and password back to you via json encode...

here is my code of the index.html file:

<html>
<head>
    <title>test jquery</title>
    <script type="text/javascript" src="js/jquery.js"></script>
    <script type="text/javascript">
    $(document).ready(function() {

        $("#button").click(function() {

            var sendu = $("#username").val();
            var sendp = $("#password").val();

            $.ajax({
                type: "POST",
                url: "ajax.php",
                data: "username="+sendu+"&password="+sendp,
                dataType: "json",
                success: function(msg,string,jqXHR){
                    $("#result").html(msg.name);
                }

            });         

        });

    });


    </script>

</head>
<body>

    Name: <input type="text" id="username" name=""username /> <br />
    Password: <input type="password" id="password" name="password" /><br />

    <input type="button" value="send" id="button" />
    <div id="result"></div></p>

</body>
</html>

and this is the php file:

<?php 

$name = $_REQUEST['username'];
$password = $_REQUEST['password'];

$a = array('name'=>$name, 'password'=>$password);

$c = json_encode($list);

echo $c;


 ?>

so when i removed the dataType: "json" and when i removed the associative array from the php file, all goes well, it worked!

here is the after removing json encode:

html:

<script type="text/javascript">
$(document).ready(function() {

    $("#button").click(function() {

        var sendu = $("#username").val();
        var sendp = $("#password").val();

        $.ajax({
            type: "POST",
            url: "ajax.php",
            data: "username="+sendu+"&password="+sendp,

            success: function(msg,string,jqXHR){
                $("#result").html(msg);
            }

        });         

    });

});


</script>

and the php file:

<?php 

$name = $_REQUEST['username'];
$password = $_REQUEST['password'];


$c = "your name is ".$name."<br />and your password is ".$password;

echo $c;


 ?>

and one more thing that, i am not an english man so if you find any common grammer mistakes, please ignore!

3
  • Your data is not in JSON format, but just a regular URL. The answer of David Jones is correct. Ps. use $_POST instead of $_REQUEST. Commented Sep 23, 2014 at 13:26
  • thanks GuyT for your answer but it worked using $_REQUEST and thanks to david jones Commented Sep 23, 2014 at 14:08
  • I know, but I only want to give you the advice to use $_POST. The problem with $_REQUEST is that it will accept every method if the name is the same. In other words; it also accepts $_COOKIE, $_SESSION, $_GET. For a hacker it will be easier because all methods are available(if you santinize the user input you will not be vulnerable). Commented Sep 24, 2014 at 6:34

1 Answer 1

1

One thing I noticed was this line in your AJAX call should be:

data: "username="+sendu+"&password="+sendp,

Should be:

data: {username: sendu, password: sendp}

That way you can access the variables when using dataType: JSON

Sign up to request clarification or add additional context in comments.

Comments

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.