0

I am just trying to submit two input fields, username and password. I am not able to do that because my code won't submit my data to the PHP database. The code is below, also please note I am trying to use ajax:

getData.php

<?php
    $db = mysql_connect('localhost', 'wont tell', 'never'); //not real username or password
    $db_select = mysql_select_db('test');

    $query = "SELECT * FROM info";
    $result = mysql_query($query);

    while($row = mysql_fetch_array($result)){
        echo "Username is: {$row[0]}, and Password is: {$row[1]} <br />";
    }

    mysql_close($db);
?>

<html>
    <head>
            <title>Store your username and password over here</title>
            <script type="text/javascript" src="update.js"> </script>
    </head>

    <body>
            <form name="form">
                <span>Enter username: </span><input type="text" id="username" name="username" /> <br />
                <span>Enter password</span> <input type="password" id="password" name="password"> <br />
                <button id="click"> Click me </button>
            </form>
    </body>

    <span id="update"> </span>
</html>

update.js

function insertAfter(referenceNode, newNode) {
    referenceNode.parentNode.insertBefore(newNode, referenceNode.nextSibling);
}

window.onload = function(){
    var clickMe = document.getElementById("click");
    clickMe.onclick = submit; 
};

function submit(){
    var output = document.getElementById("update");
    var username = document.getElementById("username");
    var password = document.getElementById("password");
    var e = document.createElement("span");
    e.innerHTML = "Please enter a valid value!";
    e.style.color = "red";
    if(username.value == "" || username.value == null){
            insertAfter(username, e);
            return false;
    }

    else if(password.value == "" || password.value == null){
        insertAfter(password, e);
        return false;
    }
    else{
        var ajax;
        if(window.XMLHttpRequest){
            ajax = new XMLHttpRequest();
        } 
        else {
            ajax = new ActiveXObject("Microsoft.XMLHTTP");
        }

        ajax.onreadystatechange = function(){
            if(ajax.readyState == 4){
                update.innerHTML = ajax.responseText;
            }
        };

        ajax.open("GET", "getFromSQL.php?username=" + username.value + "&password=" + password.value);
        ajax.send(null);
        return true;
    }

}

getFromSQL.php

<?php
    $username = $_GET['username'];
    $password = $_GET['password'];    
    $db = mysql_connect('localhost', 'wont tell', 'never');
    $db_select = mysql_select_db('test');

    $query = "INSERT INTO info (Username,Password)
VALUES ('{$username}', '{$password}')";
    $result = mysql_query($query);
    echo "$username , $password";
    mysql_close($db);
?>
2
Contributors
1
Reply
3
Views
4 Years
Discussion Span
Last Post by Clanstrom
0
    $username = mysql_real_escape_string($_GET['username']);
    $password = mysql_real_escape_string($_GET['password']);  

    $db = mysql_connect('localhost', 'wont tell', 'never');
    $db_select = mysql_select_db('test');

   mysql_query("INSERT INTO info (Username,Password)
    VALUES ('$username', '$password')") or die ("Error something");

I would try the above.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.