[RESOLVED] HALP Needed with Omni Directional Flow of Data - mySQL to JSON to HYPE inner text, hype inner.html to MYSTERY to php to MySQL

I have seen this discussed and explained about in how you handle forms, and please excuse me if its already been answered, I will look at any example again ( pretty sure I have looked at each of them)

My issue is simply that i am confused in this outline of steps in the place where inner.html becomes a variable in javascript and then I can open a php file and pass that javascript variable to the php ones, and thusly write data to my table.

I was super careful, I wrote three functions for cleaning in the php file that does the writing, plus I started using mysqli to connect to the database.

in a nutshell - i could really use an example of how to pass a variable from javascript to php

outline of project | whats working:
open connection to database √
gather data and display in table √
use php to open connection and write into mySQL table √
** this is hard coded, now I need to make it dynamic by passing values to variable in the php side of things
sanitizing before sending √
what I am stuck on: how to make inner.html a variable that PHP can see and take action on

this is probably super simple but I cannot see it at the moment.

It depends on what you are trying to do and your “inner.html” is, as this doesn’t represent any standard convention. Perhaps you have an iframe somewhere in this setup?

There are two basic setups for server-side communication with PHP:

  1. Have your PHP act “standalone” and only respond to dynamic requests. In this setup, your javascript code will use AJAX requests for everything. The HTML page itself is not PHP and you have a separate PHP file that handles receiving and responding to specific requests. The response sent should typically be JSON, so it can easily be handled by javascript code making the request.
  2. Intermingle your PHP and HTML code. Here, you would have a .php file that renders the HTML content. This would include the hype document code, but to set variables it would probably render out some javascript code. This could be writing out global variables, which are then read by javascript and acted upon.

Commonly with setup #2, you would use form actions (POST) or redirections with GET requests that would trigger a complete page reload. But you can also mix and match techniques from #1 and #2.

Does that help?

that is odd, because I thought it was fairly common. i probably need to clarify a bit - this is what I am referring to:
document.getElementById(“resultBox”).innerHTML = txt;

and this is working. i have no problem getting data to display into the HYPE document from mySQL at all, i get it through json as described above.

a bit and I think I am close! I think the part about the scope of the variables is what i needed to see. the inner.HTML values are all being set to variables in javascript, as I described. so then, i was trying to figure there must be a way to pass an array from Javascript to PHP, but then I thought - OK how about a long string passing values. then i could use get to pull values off the URL string like usual. they are sanitized after being stripped, so its safe.

gives me something like:
var passAllValues = “call_to_insert.php?first_name=” + first_name + “&last_name=” + last_name + “&email_address=” + email_address;

and i have that variable applied in the XMLHttpRequest open

var request = new XMLHttpRequest();
request.open('POST', passAllValues , true);

result is: Error in undefined: ReferenceError: passAllValues is not defined
maybe because its not global!?

i am calling this resolved because i got it to work. some things that i noticed what may help someone else - the fact that i had external JS holding that XMLHTTP request and was passing a value to it - at some point splitting that up and putting code outside of hype was the reason it was failing. once I moved that request into the same function block (right after building the long URL string with all the values on it) it was fine.

and yes its working and i am able to write to the database now - but i do have one additional question about this. does anyone got a better way than for me to build this incredibly long URL string? I mean, its not broken. it works. but is it elegant?

AND more importantly, is there a way to have this happen under the hood so i don’t show the world or anyone with access to the CONSOLE that url string?


1 Like

Gotchya; I thought when you wrote “inner.html” you were referring to a filename “inner” with the “.html” extension.

Disclaimer that I’ve never actually done this…
While you state the method as POST, you are composing a URL that is effectively acting as a GET. Instead, you can use a POST body with the XMLHTTPRequest, basically like what you’ve done but the call is slightly different.

See the POST example on this page:

Just make sure your PHP uses the $_POST variable if you are using $_GET instead. $_REQUEST will work fine without changes (though some folks frown upon using $_REQUEST, I am not one of them).

1 Like