Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 8 of 8
  1. #1
    New Coder
    Join Date
    Dec 2011
    Posts
    59
    Thanks
    51
    Thanked 1 Time in 1 Post

    sending a value to the database

    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
    COLD
    i say again COLD, COLD, COLD. brrrrrrrrrrrr
    Last edited by Paul Williams; 01-16-2012 at 04:35 AM. Reason: typo

  • #2
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,928
    Thanks
    203
    Thanked 2,531 Times in 2,509 Posts
    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.

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.

  • Users who have thanked Philip M for this post:

    Paul Williams (01-15-2012)

  • #3
    New Coder
    Join Date
    Dec 2011
    Posts
    59
    Thanks
    51
    Thanked 1 Time in 1 Post

    ???

    Phillip

    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.

    thanks,
    Paul

  • #4
    New Coder
    Join Date
    Dec 2011
    Posts
    59
    Thanks
    51
    Thanked 1 Time in 1 Post
    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)
    </div>

    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.
    Ideas?
    Changes in logic?

    Thanks,
    Paul

  • #5
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,928
    Thanks
    203
    Thanked 2,531 Times in 2,509 Posts
    You must user server-side scripting - not Javascript which as I say can only read from a file, and then only using Ajax.

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.

  • Users who have thanked Philip M for this post:

    Paul Williams (01-15-2012)

  • #6
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,211
    Thanks
    75
    Thanked 4,343 Times in 4,309 Posts
    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.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • Users who have thanked Old Pedant for this post:

    Paul Williams (01-15-2012)

  • #7
    New Coder
    Join Date
    Dec 2011
    Posts
    59
    Thanks
    51
    Thanked 1 Time in 1 Post
    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.

    Paul

  • #8
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,211
    Thanks
    75
    Thanked 4,343 Times in 4,309 Posts
    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:
    Code:
    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.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • Users who have thanked Old Pedant for this post:

    Paul Williams (01-16-2012)


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •