<?php session_start(); include 'forms.php'; include 'config.php'; include 'lib.php'; ?>

<html>
<head>
<meta charset="UTF-8">
<title>Login</title>
<link href="css/main.css" rel="stylesheet" />

</head>

<body>

<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"> </script> 

     <script type="text/javascript">

        $(document).ready(function(){



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

                var username = ("<?= $username ?>");

                $.ajax({
                    url:"ajax.php",
                    data :'username=' +username,
                    success: function(ajax){
                        $("#ajax").append(ajax);


                    }
                });
                })
            });
     </script>

</body>

</html>
<?php

    if ($_SESSION['username'])
        echo "welcome, ".$_SESSION['username']."!<br><a href='logout.php'>LOGOUT</a>";
    else 
        die("You must be logged in");


    mysql_connect(localhost,$username,$password);
    @mysql_select_db($database) or die( "Unable to select database");
    $rpassword  = $_GET["rpassword"];
    $password  =$_GET["password"];
    $firstname =$_GET["firstname"];
    $secondname =$_GET["secondname"];
    $username =$_GET["username"];

    if (isset ($_GET['username'])) 
    {

                if($password==$rpassword){  //checks if passwords match             


                        $query = "INSERT INTO users VALUES (NULL,'$firstname','$secondname','$username','$password')";
                        mysql_query($query) or die("$query failed with error ".mysql_error());
                    }


                    else
                        echo"PASSWORD DONT MATCH";

    } 

        loginform();
        get();
?>

need to transfer php variable $username into javascript varible var username
for some reason the above "var username = ("<?= $username ?>");" line dont work
How ever if i replaced ("<?= $username ?>") with "username" it works please helk

thnks

Recommended Answers

All 2 Replies

Could you try running the not-working code, and use the browsers source-viewer to check what was echoed to JavaScript?

also, is this the chronological order of the file? because it seems you are defining $username AFTER using it. which should result in an error message.

commented: yes +14
Member Avatar for diafol

agree with phoenix - you need to set username before placing it in the js code.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.