3

Hi I'm trying to send information to my local mysql server that is shown in Sequel pro. I have a form in html which looks like this:

    <a href="#openRegisterModal" id="main_header_register">register</a>
<div id="openRegisterModal" class="modalRegisterDialog">
    <section>
        <a href="#close" title="Close" class="close">X</a>
        <h1>Register</h1>
        <form name="registerform" class="registerform">
            <label for="usernamefield">Username: </label>
            <br>
            <input type="text" id="usernameregisterfield"></input>
            <br>
            <label for="emailfield">Email: </label>
            <br>
            <input type="text" name="emailfield" id="emailregisterfield"></input>
            <br>
            <label for="passwordfield">Password: </label>
            <br>
            <input type="password" name="passwordfield" id="passwordregisterfield"></input>
            <br>
            <label for="passwordrepeatfield">Repeat Password: </label>
            <br>
            <input type="password" name="passwordrepeatfield" id="passwordrepeatregisterfield"></input>
            <br>
            <!--Check field-->
            <br>
            <input type="checkbox" name="agreementbox" id="agreementbox"></input>
            <label for="agreementbox" id="termslink">I agree to the </label>
            <a href="" id="termslink">terms</a>
            <br>
            <input type="button" id="registerbutton" value="Register" onclick="registerUser()">

        </form>
    </section>
</div>

When I click the register button this javascript function is suppose to be called:

function initiate() {

}

function registerUser()
{

    var username = document.getElementById("usernameregisterfield").value;
    var email = document.getElementById("emailregisterfield").value;
    var password = document.getElementById("passwordregisterfield").value;


    console.log("Username: " + username);
    console.log("Email: " + email);
    console.log("Password: " + password);

    if (window.XMLHttpRequest)
    {
        // Code for IE7+, Firefox, Chrome, Opera, Safari
        xmlhttp = new XMLHttpRequest();
    }
    else
    {
        // Code for IE6, IE5
        xmlhttp = ActiveXObject("Microsoft.XMLHTTP");
    }

    xmlhttp.open("GET",
                 "RegisterUser.php?username=" + username
                 + "&email=" + email
                 + "&password=" + password,
                 true);

    xmlhttp.send();


}

initiate('load');

Which should then call this php file:

<?php

    $username = strval($_GET['username']);
    $email = strval($_GET['email']);
    $password = strval($_GET['password']);

    $connection = mysql_connect('localhost', 'root', '')
        or die("Unable to connect to database");


    $database = mysql_select_db("myDatabase")
        or die("Could not select database");

    $sql = "INSERT INTO registered_users VALUES ('$username', '$email', '$password');";
    $execute = mysql_query($sql);


?>

But when I try this nothing happens. I don't even get an error message.

Someone please help me!

Eksperiment626
  • 985
  • 3
  • 16
  • 30

2 Answers2

2

I think you need to add $connection to your query.

Firstly add your database to the connection statement

$connection = mysql_connect('localhost', 'root', '', 'myDatabase')
    or die("Unable to connect to database");

then after you build your query change the next line to this

$execute = mysql_query($connection, $sql);

This is all presuming that you're actually getting to the PHP file and that is where the problem actually is.

jdcookie
  • 121
  • 1
  • 6
  • are you sure it is actually getting to the PHP? try running exit("test"); at the top and seeing if you actually get there. – jdcookie Nov 12 '13 at 11:51
  • Agreed. It's probably not getting to the php file. What's more is there's no handler to come back and say "User Registered". Also, move away from mysql. Those statements as they currently stand are open to exploitation. – John Nov 12 '13 at 11:53
  • What should happen when writing exit("test"); ? – Eksperiment626 Nov 12 '13 at 11:57
  • It should just stop and print "test" on the screen. You might need to look up some tutorials on using AJAX. Also when I do work like this I use Firebug to see the javascript variables being sent to the PHP page and also any variables being echoed back to javascript as @John mentioned above about the handler coming back – jdcookie Nov 12 '13 at 13:19
2

You missed to add the database connection to select DB. You should write

$database = mysql_select_db("myDatabase", $connection)
    or die("Could not select database");
StreetCoder
  • 9,871
  • 9
  • 44
  • 62
  • what type of error you are getting ... did you see this text `Could not select database` anywhere in your browser? – StreetCoder Nov 12 '13 at 11:51