Jump to content

Converting PHP Form Handling to JS


TamiJo
 Share

Recommended Posts

Hi there. I have a local app that I have made using http and php code. I want to get this online, and want to do the stuff my php code is doing in JS instead. (Partly as a way to help me learn JS.)

I currently have a form on a page `advanced.html` that submits to `search.php`. advanced.html is essentially just a form, Search.php has things like ``$as= $_GET([`as`])`` in order to set variables, then does things with them to contact a database with an SQL query and uses those results for different outputs. (0 results redirects to an error page, 1 result directs to the page for that specific result, multiple results uses the aforementioned 'as' variable to output either in a list or grid format.)

Under my new system I am attempting to change the form to submit to `search.js`, where I will simply replace the php code with js. (Plus some additional changes to redirect to a html page for displaying the results, probably? I've not got that far.) I have three questions:


1) What equivalent would I use to `$_GET`? I have used W3 school's tutorial on JavaScript and am not finding anything that helps with form handling at all. I see the User Inputs section, but that doesn't seem like it is for this kind of thing.

2) Do I need to use a different system entirely? Doing away with submitting to search.js and simply having the js code on the advanced.html page and then directing the user to the appropriate page from there?

3) PHP has a built in function `mysqli_connect()`. Can this be replicated in js?

I am still fairly new to programming, so apologies if my questions are obvious or nonsensical. I can try to provide extra context etc. if it would help. I have provided my original (working) advanced.php and search.php file below.

search COPY.php advanced COPY.php

Link to comment
Share on other sites

Javascript is not an equivalent to PHP, they serve two different functions and can't replace each other.

Forms cannot be submitted to a Javascript file. To make something similar to form handling with Javascript, you would have to have code running on the form page which prevents the form from being submitted and reads values from the inputs.

Unless you're using Node.js server software, Javascript runs on the user's browser, so it does not have access to the server's database. You're allowed to save data on the user's computer using localStorage, but only that user can see the data, other people cannot. You can read about localStorage on this page: https://www.w3schools.com/js/js_api_web_storage.asp

Javascript is able to read query string values using location.search but you need to do a lot of string manipulation to identify the variable names and values. Its not as simple as in PHP.

Link to comment
Share on other sites

Given that I'm communicating with a central database, I can't exactly expect the user to download that just to use the website. Looks like I'll have to go back to the old method. Thanks, anyway. :)

Edited by TamiJo
Link to comment
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
 Share

×
×
  • Create New...