View Full Version : Resolved sending a value to the database

Paul Williams
01-14-2012, 05:01 PM
seems easy yet frustrating trying to learn how.

i set up a column in the MySql database that holds an integer, lets call it IMG_INDEX; easy enough.

in my prgm.php, i need to use the values stored in IMG_INDEX (in the database) to contol which image is currently displayed in the browser.

i need to be able to update to value of IMG_INDEX in the database from the code.
i need to be able to retrieve the value of IMG_INDEX from the database with the code.

note: this is NOT a user input thing, or a display of database info thing.
i'm using IMG_INDEX as a switch to control which image is displayed at different times during a web browing session.

?? so here is the question ??
how do i send a new integer (like 2) to the database and store it in IMG_INDEX ?
how do i retrieve the value of IMG_INDEX from the database ?
i need to do this all from the javascript area in the head of the php code, and occasionally from the body area of the php code.

if you have a tip, i'd really appreciate your help.

thank you,
Paul Williams

todays weather in Kalispell, MT
i say again COLD, COLD, COLD. brrrrrrrrrrrr

Philip M
01-14-2012, 05:04 PM
For security reasons Javascript is not able to create, modify or delete any file. It can only read files (using AJAX).

All advice is supplied packaged by intellectual weight, and not by volume. Contents may settle slightly in transit.

Paul Williams
01-14-2012, 06:26 PM

not sure what you mean.
i have been studying AJAX to get this done and it seems the GET() and POST() methods are the way to go, yet the book i have on that subject sez 'get' and 'post' sends and retrieves info from a script file on the server.

is that not manipulating the database?
plenty of user 'input' data goes to and from the database using 'get' and 'post' commands, but that is all user input and simply displaying db.info.

surely i error somewhere in here.
but there has to be a way to send a value to the database and then retrieve that value for use elsewhere?

could you explain please or refer me to a good book on this subject.


Paul Williams
01-14-2012, 07:14 PM
Maybe I should explain a bit on what I’m trying to do.

Building a CMS (content management source).

Picture a webpage that has been divided into four quadrants.
Each quad can display one of 4 pictures, all preloaded and stored into an array.

When a viewer enters the site and sees the main page, they will see the pictures that were last selected to be displayed by the admin.controller . The admin. controller has access to the page that manipulates which image is to be shown in each quad. No one else but the admin.controller has this privilege (user access).

So my approach is to code the admin/controller page such that when the admin. controller chooses to display picture #3 in quad #1, then a switch is stored in the database such as this:

quad1_IMG_INDEX = 3 set the variable in the code where needed
post(quad1_IMG_INDEX) send the value of the variable to the database

once this is done, then the code looks at the variable stored in the database to display the correct image in the correct quadrant.

x= get(quad1_IMG_INDEX) retrieve the value of the variable from the db
<div id=”quad1”>
<img src=”quad1_array[x]”> display the correct image (my shorthand expression)

My thought is that this way, the images that always appear are the ones that the admin.controller has set up, and when a user comes in to the site with “guest” privileges only, then they will see the correct images that have been set up to be displayed. Kinda like a blog site; when a viewer/user comes to a blog site, they immediately see the latest entries from the database. The only difference here is that an admin.controller sets up which images are to be viewed, not just the latest image stored.

Maybe there is a better way to handle this, or a good reference out there somewhere.
Changes in logic?


Philip M
01-14-2012, 07:19 PM
You must user server-side scripting - not Javascript which as I say can only read from a file, and then only using Ajax.

Old Pedant
01-15-2012, 12:15 AM
And the server-side scripting can be of any kind. PHP, ASP, JSP, etc.

And the server-side scripting can be as simple or as complex as you want it to be.

Yes, you could use an AJAX get or post to send information to the server, or you could use it just to request data from the server.

The server isn't even aware that you are using AJAX. For all it knows, you are using a <form> posting or a simply <a href="..."> hyperlink to hit the page(s). But the server code *can* be asked to return the data in whatever form you prefer: A new web page, a simple string of text, or perhaps an entire network of JavaScript objects, encoded via JSON. Or. or or...too many possibilities to list them all.

Paul Williams
01-15-2012, 04:48 AM
thanks OP,
i appreciate the post.

however, this still puzzles me a bit.

i have a form in my browser code that sends data to the database and another form that displays data from the database; and when i look at that code, it is clear that the PHP determinants <?php....?> are right there in the browser code, it looks to me like the code is going straight to the database (?). it does not appear that any server is standing in the way of the commo between the browser code and the database(?).

if the browser code is "supposedly" sending all this PHP code to a "server" then how in the world does this happen?
browser code ==> server ==> database ==> server ==> browser

this just seems crazy?
maybe i'm just trying to see to far to deep into all this.
do i need to NOT concern with the server/database communication?
and only concern with send/recv from the server?

whats up with all this?
thought i understood all this, now it seems confussing?
as i studied up on server-script there are alot of references to using ajax in your browser code to access the server-script file, but.....where in the world is the server-script file stored? i look on my hosting account and there is nothng in existance but my website browser files and the database files? what's up?

maybe you have some good references or tutorials i can study up.


Old Pedant
01-16-2012, 02:02 AM
Maybe you just need to get a good PHP tutorial.

What you describe is quite normal web page behavior.

It's pretty common to have a single web page that serves as both a display mechanism and as a <form> data processor. I might even go so far as to say that may be the MOST common way to allow updating of existing DB data by way of a web page.

The logic of such a page might look like this:

Is this page receiving data from a posted <form>? If so:
read all the <form> data
update the data in the database

Was this page asked to display data from a specific record in the database? If so:
go get the data from the database
create the display for the browser to see

If not asked to display data about a specific record, then:
ask the user what data he/she wants to see

That is, there are three separate processes that might go on, based on the code and based on what information is received by the server.


None of the above is directly related to AJAX. Because normally with AJAX you are being pretty specific about what information you are asking for and the AJAX server-side code will only be returning exactly that information, in a format the is useful to and usable by JavaScript code. And, yet, the mechanics of the AJAX response are 100% the same as the mechanics of a full web-page response: HTTP request cause an HTTP response. One for one. Simple. Clean.