SitePoint Sponsor

User Tag List

Results 1 to 7 of 7

Hybrid View

  1. #1
    SitePoint Enthusiast
    Join Date
    Jul 2009
    Posts
    61
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question Using javascript to query SQL (Simple question)

    I have a DIV, and a .php file that will receive 2 arguments, like this:

    writesql.php?arg1=123&arg2=456

    And i have this DIV:

    PHP Code:
    <div style="width:100px;height:100px;border:solid 1px;"testing... </div

    And i'm trying to do one thing..
    When i click on that DIV, i want it to write those 2 arguments "123" and "456" to the .php file so instantaneosly it writes it in the database.

    How can i do that?

  2. #2
    SitePoint Wizard bronze trophy
    Join Date
    Jul 2008
    Posts
    5,757
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Well, I think the more typical way to do this is really to just use a form and send a post request. But if you don't want to reload the web page, you would need to either use an iframe, or javascript.

    Is it acceptable to you, that users without javascript enabled, cannot use this functionality?

  3. #3
    SitePoint Enthusiast
    Join Date
    Jul 2009
    Posts
    61
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by crmalibu View Post
    Well, I think the more typical way to do this is really to just use a form and send a post request. But if you don't want to reload the web page, you would need to either use an iframe, or javascript.

    Is it acceptable to you, that users without javascript enabled, cannot use this functionality?
    Yes, cuz i'll be the only one, i'm just building an aplication to draw a block based map, and i want it to save the blocks where i click in the database.
    I dont want to reload, neither use forms. Just to make a request to that .php file in realtime without needing for reload.

  4. #4
    SitePoint Wizard bronze trophy
    Join Date
    Jul 2008
    Posts
    5,757
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Ugly, but easy.
    Code:
    function requestUrl(url) {
        (new Image()).src = url;
    }
    Code:
    <div onclick="requestUrl('writesql.php?arg1=123&arg2=456')">...</div>

  5. #5
    SitePoint Enthusiast
    Join Date
    Jul 2009
    Posts
    61
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by crmalibu View Post
    Ugly, but easy.
    Code:
    function requestUrl(url) {
        (new Image()).src = url;
    }
    Code:
    <div onclick="requestUrl('writesql.php?arg1=123&arg2=456')">...</div>
    (new Image()).src = url;

    What does this new Image() do?

  6. #6
    SitePoint Wizard bronze trophy
    Join Date
    Jul 2008
    Posts
    5,757
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    It creates a javascript Image object. It's kinda like making an <img src="writesql.php?arg1=123&arg2=456"> tag in the html, except you won't ever see the image visually. If the syntax is confusing, it's equivalent to
    Code:
    var img = new Image();
    img.src = url;
    I'm kinda abusing it to get the browser to load a url. It doesn't matter if the url isn't actually an image.

    Usually you use ajax if you want the browser to request a url without reloading the page. But, the code would be quite a bit longer, and the benefits don't really seem important to me for your usage.

  7. #7
    SitePoint Enthusiast
    Join Date
    Jul 2009
    Posts
    61
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Ok, got it!!
    Thanks Works very nice!


Bookmarks

Posting Permissions

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