2

I know this site doesn't like "spot my mistake" code, but I'm desperate. I have a website that needs to access user-specific data from a database (PHP), convert the data into a JSON file, and then change a HTML header to display that specific data. The database table has the user email, password, and class name, among other things. I have a login page that establishes the session variables for the email and the password. When the user logs in, I want their class name to be entered into HTML text. I've used dozens of sources, mostly W3schools, and came up with this code:

PHP:

<?php
session_start();

 header("Content-Type: application/json; charset=UTF-8");
 $obj = json_decode($_GET["x"], false);

 if (!$obj) {
 die(mysqli_error());
 }

    $servername = "localhost";
$username = "id5143969_enviroquest1";
$password = "codeteam1";
$database = "id5143969_enviroquest1";
$link = mysqli_connect($servername, $username, $password, $database);

$result = $link->query("SELECT UserClassName FROM ".$obj->UserInfo1." WHERE ".$obj->UserEmail."= '". mysqli_real_escape_string($link, 
$_SESSION['useremail']) . "' and ".$obj->UserPassword." = '" . mysqli_real_escape_string($link, $_SESSION['userpassword']) . "'");

if (!$result) {
die(mysqli_error());
}

$_SESSION['classname'] = $result->fetch_assoc();

if (!$_SESSION['classname']) {
die(mysqli_error());
}

     echo json_encode($_SESSION['classname']);

Javascript:

function getclassname() {
var obj, dbParam, xmlhttp, myObj, x, txt = "";
obj = { "UserInfo1":"UserClassName"};
dbParam = JSON.stringify(obj);
xmlhttp = new XMLHttpRequest();
xmlhttp.onreadystatechange = function() {
    if (this.readyState == 4 && this.status == 200) {
    myObj = JSON.parse(this.responseText);
    document.getElementById("UserClassName").innerHTML = myObj;
    }
};
xmlhttp.open("GET", "php2.php" + dbParam, true);
xmlhttp.send();
}

HTML:

<h1 class="text-center" id="UserClassName" name="UserClassName" onload= 
"getclassname()"> </h1>

I have no idea what's going wrong, and am too new to coding to figure it out by myself.

2
  • Welcome to the site! Check out the tour and the how-to-ask page for more about asking questions that will attract quality answers. You can edit your question to include more information. What is happening, and what do you expect should be happening? Also, would you please add the relevant parts of your HTML? I note your PHP code is never using $obj after decoding it. Commented Mar 24, 2018 at 16:12
  • I added the relevant html and more data on what I need it to do. To be completely honest, I don't understand what the obj variable is supposed to do, I've just pieced everything together from existing code on this site and w3schools and it seems to be necessary. There's also an obj variable in the javascript Commented Mar 24, 2018 at 16:53

1 Answer 1

1

Try this (I can't test it, but)—

PHP:

  • Remove the ?> at the end of the file. Pure-PHP files should always leave off the closing tag.
  • Change (MYSQLI_ASSOC) to just () - per this and the docs, you don't need it.

Javascript:

  • Remove the session_start() call
  • Change

    for (x in myObj) {
        txt += myObj[x].name + "<br>";
    }
    

    to

    txt = myObj.UserClassName
    

    The fetch_assoc() call in PHP gives you a mapping that uses the database field names ("each key in the array represents the name of one of the result set's columns" per the docs) for a single row. Therefore, if the JSON encode/decode worked OK, you should be able to refer directly to the field.

To test this, in the developer tools, set a breakpoint at the txt = ... line and see what myObj is.

I don't think you need $obj, dbParam, or ?x=, but I would not suggest changing them unless the above doesn't help.

Good luck!

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

7 Comments

Thank you for the suggestions, unfortunately the code is still not working.
@Mekade24 What errors are you getting in the PHP or JavaScript? Did you try inspecting myObj in the browser debugger?
I'm not getting any errors on the PHP or Javascript end. I don't know how to set a breakpoint or use the browser debuggger. I have been coding for less than a week.
@Mekade24 Ah! Take a look at this tutorial for Chrome or this for Firefox. I would suggest leaving this question open until you have learned the tools and have a bit more information. Good luck!
Even with the tutorial, I can't get the browser debugger to work and I really don't have the time to learn a whole new program. I mean, I got this code practically letter-for-letter from w3schools. There has to be an identifiable error I'm making in this code. Websites get information like this all of the time, so it the method of doing it must be popular and easily reproducible. I can't wrap my head around why there's no answer anywhere to this seemingly simple function.
|

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.