Jump to content

Hey all!

 

I am doing my advanced higher computing in my last year of school and as part of the course we have to complete a project of our choosing. I have chosen to do an Online Course Choice Selection system which basically lets you pick your subjects for school online compared to the old paper system that schools across Scotland currently do. However I have come to a problem. I have a working Log In system however I think i have done it a crazy way. I have it so when you press the submit it redirects to a PHP file does the MySQL work then redirects back to the next javascript page. However thats not the problem as that all works fine.

 

My issue is that I have created a page that displays all of the information of everyone who has used the system to the admin. To do this I need to save get all the data from MySQL using PHP then i have set up the javascript so it takes in lists and displays them and i cant do the redirecting because i cant have it send hundreds of cookies. So basically i want to know how i can make a page read data from the MySQL server then output it to a list i can read in Javascript.

Sorry for the long post i just thought it would make more sense if I gave some background.

 

Any help will be appreciated.

Thanks

Gavin

Link to comment
https://linustechtips.com/topic/285194-use-php-within-javascript-phpjavascript/
Share on other sites

Link to post
Share on other sites

Nope, still didn't make much sense... Unfortunately I'm on my phone, so I can't use code tags.. At least, not well. Anyways, let's break what I think you want down.

Part 1: make a page read data from the MySQL server

I'm going to assume you can initiate a database connection and connect successfully. Store the mysqli_connect in a variable. The default boring option of $con is what I'll use.

Then, still before any of the HTML, do something like this in the php code block you use to create the database connection

$sql = "your query";$res = mysqli_query($con, $sql);while($row = mysqli_fetch_assoc($res){$array[] = $row;} //Then down in your JavaScript where you need the results from the query, use this <?php echo $json_encode($array);?>

To summarise, that code should get the results from the database, Stores the results array created by the while loop in the array $array, creating a multidimensional associative array that's easy to work with. Then down where you need the array for your JavaScript, it encodes the array to JSON, which your JavaScript can use...

At least, that's how it should work. I'm just on my phone during a break at work. I haven't tested anything, it's off the top of my head

I am good at computer

Spoiler

Motherboard: Gigabyte G1 sniper 3 | CPU: Intel 3770k @5.1Ghz | RAM: 32Gb G.Skill Ripjaws X @1600Mhz | Graphics card: EVGA 980 Ti SC | HDD: Seagate barracuda 3298534883327.74B + Samsung OEM 5400rpm drive + Seatgate barracude 2TB | PSU: Cougar CMX 1200w | CPU cooler: Custom loop

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×