Ask coding questions

← Back to all posts
How to access databases from another file
h
Baconman321

The reason I want a database is so I can access a database from another file, but I don't know how to. In test.html, I tried to access the database, but it didn't show up. I know I'm doing something wrong. How do I fix this?

Answered by Highwayman [earned 5 cycles]
View Answer
Voters
OscarFrancoB
Baconman321
Comments
hotnewtop
Highwayman

When you switch pages, your essentially running entirely different program. In order to make something like that work, you’d have to re-initialize the db. You cannot just run a js script in one page and expect that state to remain intact when you then load a second.

Baconman321

@Highwayman I thought that was the problem! But how do I access the database from that file?

Highwayman

@Baconman321 I’m not entirely sure. Try just using the same arguments to initialize the dB in test.html and see if you can access stuff you put in the dB.

Baconman321

@Highwayman Ok, since this is going into my phantomforcestools project, I can maybe be able to just have a login page and then see the info on the same page, but I'd like to know this for future projects.

Highwayman

@Baconman321 if your making a login page for something, then scrap this repl right now. You need a backend.

Baconman321

@Highwayman It is written in php, and yes I understand it doesn't have the highest security, I am just doing this for fun, not supposed to be a commercial business.
The login page is just for me to read the feedback, but prevent anyone else from doing so. The next step I am taking is cybersecurity measures, like preventing them from injecting code.

Highwayman

@Baconman321 oh I’m dumb oops. Sry bout that lol

Baconman321

@Highwayman It's fine, I didn't provide information on what I am using it for. If I am using a frontend browser, I know that people can just check out the page source and check out the password and username right?

Highwayman

@Baconman321 yes...?

Baconman321

@Highwayman Ok, well I'll try it and let you know if it works.

Highwayman

@Baconman321 alright :)

Baconman321

@Highwayman Ok, so if you look at script.js, I added functions. I am kind of lost though. It says reference error test is not defined, in the getdbinfo function I made.

Highwayman

@Baconman321 it’s because the scope of test is limited to only the created function.

Baconman321

@Highwayman How do I fix that?

Highwayman

@Baconman321 declare dB outside of the function and then initialize it in inside.

Baconman321

@Highwayman Thought so. Thanks! I'll see if it works

Highwayman

@Baconman321 cool :P

Baconman321

@Highwayman Hmmm. Still doesn't work. It says test isn't defined.

Baconman321

Never mind fixed it

Highwayman

@Baconman321 I’m not sure why that’s happening.. uhh...

Highwayman
Baconman321

@Highwayman SOrry. Testing it now though should work between files.

Baconman321

@Baconman321 Ummmm, whenever I try to output to the console, it closes when I switch to the second file. And when I try to document it, it does undefined in test.html, and [object, object] in index.html

Highwayman

@Baconman321 uhh... I don’t know,, hmm...

Highwayman

@Baconman321 what I
Exactly do you mean by document it?

Baconman321

@Highwayman document.write, or output it to the website sorry.

Highwayman
Highwayman

@Baconman321 I think there is a way to alert data to a different window in js, but
I forget how and the post for it is so old that I’d never find it...

Baconman321

@Highwayman I found a different way: return. I just document it, however I don't know how to return the right thing. If you look at script.js, I return test, which is what I put into the database, but it gets undefined.

Highwayman

@Baconman321 maybe your using the put function improperly??

Baconman321

@Highwayman I think I had to do something like json.stringify or whatever. It works now :>

Highwayman

@Baconman321 oh, yay! :)