ExpressionEngine CMS
Open, Free, Amazing

Thread

This is an archived forum and the content is probably no longer relevant, but is provided here for posterity.

The active forums are here.

OnClick & SQL Queries

July 23, 2008 10:27am

Subscribe [3]
  • #1 / Jul 23, 2008 10:27am

    kungfumonkey

    15 posts

    Hi !

    Is there a simple way of running some SQL query, after the user clicks the button (using query module)? Apart from using a bit tricky solution involving AJAX ?

    Basically, let’s say I want to do sth like:

    <input type=sometype>

    I don’t care about getting any response back. I just want to update certain field in the table. Can anyone help me please?

    Luke S.

  • #2 / Jul 23, 2008 10:30am

    George Ornbo

    272 posts

    You would either need to use a form to post data to page that will process your script or an AJAX call. jQuery is a good framework for doing this via JavaScript - it can return data in a variety of formats too.

  • #3 / Jul 23, 2008 10:36am

    kungfumonkey

    15 posts

    Hi George!

    I just updated my original post;) I don’t want to return any data, just send.

    I know about both, but I’m thinking if there is anything else, which might do the job (just run simple UPDATE..SET..WHERE query)?

    Maybe some EE functionality I don’t know about yet, some module, or sth else?

  • #4 / Jul 23, 2008 10:40am

    George Ornbo

    272 posts

    Are you updating the EE database or a database somewhere else? In terms of the technology you are going to have to use some PHP scripting and maybe some JavaScript somewhere I’m afraid!

  • #5 / Jul 23, 2008 10:43am

    kungfumonkey

    15 posts

    I am updating a custom table in the EE database.

  • #6 / Jul 23, 2008 10:50am

    Mark Bowen

    12637 posts

    Be very very careful in doing this. If you are allowing people to type something into a field and then you are using that data / variable in a SQL query then you could theoretically get attacks to your system from allowing this to take place.

    People could type in parts of a SQL query and get all kinds of things to take place on your system, not a good thing that you want to have taking place really.

    What exactly is it that you are trying to achieve as there may be a different / better way of going about this. You would definitely need to check that whatever data is passed into your query is in a format that you are expecting though just to be on the safe side.

    Best wishes,

    Mark

  • #7 / Jul 23, 2008 10:53am

    kungfumonkey

    15 posts

    Yes, I assume that it’s gonna involve some scripting;)

    I am just wondering if sth as simple as that exist / would be possible:

    1)
    function my_function
    {
    the_query = UPDATE my_table SET my_field = '{title}’ WHERE my_id = ‘666’

    do_the_job_magic_command(run_the_query);
    }

    2)
    <input> with the on_click event..

    If there is sth like do_the_job_magic_command then I would like to know it;) Any clue?

  • #8 / Jul 23, 2008 10:59am

    kungfumonkey

    15 posts

    I think I am not letting anyone to do sth dangerous, cause the field would be updated with the {title} - entry title from a weblog. So the worst they could probably do would be putting some F-words in the Publish section 😉

  • #9 / Jul 23, 2008 11:10am

    kungfumonkey

    15 posts

    Mark, to make it shorter, what I currently have is:

    1) Page displaying weblog entries
    Each weblog entry has separate div (same class where styling is applied, but the div id is the actual entry_title)

    2) I am also using {title} for href / id in links, to navigate through the webpage comfortably

    3) In {exp:weblog:entries} loop I am loading a button, which is getting current {title} as its id

    What I want to have:
    4) When you click the button, it updates the database with the current {title} and redirects to another page

    Does it make any sense?

  • #10 / Jul 23, 2008 11:25am

    Mark Bowen

    12637 posts

    Sort of makes sense but not really knowing what you are doing this for?

    What exactly is the end result? Is this for some sort of favourites page or poll / survey kind of application?

    Still a bit hazy on what you are trying to do I’m afraid which makes it difficult to answer on this one.

    Best wishes,

    Mark

  • #11 / Jul 23, 2008 11:38am

    kungfumonkey

    15 posts

    Hmm.. I’ll try to say it in a different manner;)

    1) Two templates

    2) Template 1 has a button on it

    3) Template 2 has {exp:contact_form} on it with various fields, textareas, etc.


    Desired interaction between two templates:

    1) Button click on Template 1 sends some data to my custom table and redirects to Template 2

    2) Template two gets the data from my table and fills some fields on the contact_form

    The only thing I didn’t figure out is actually updating the database after you click the button.

  • #12 / Jul 23, 2008 12:09pm

    Mark Bowen

    12637 posts

    Ah I see now. Why don’t you just pass the title in to your form using a segment variable though? Either that or use the fantastic Session Variables plugin to save the value and then re-use it on your form page.

    Saving the data to the database just for that seems a little overkill to me.

    If it were me I would probably go with the plugin as it is a complete doddle to use and should do what you need admirably well.

    Hope that helps.

    Best wishes,

    Mark

  • #13 / Jul 23, 2008 12:13pm

    kungfumonkey

    15 posts

    Actually I was thinking about session variables before, don’t know why I just didn’t use them yet;)

    Thanks for the links, I’ll have a play with that, what should hopefully solve my problem.

  • #14 / Jul 23, 2008 12:44pm

    Mark Bowen

    12637 posts

    No problem. Let us know how you get on, should be a doddle though hopefully 😉

    Best wishes,

    Mark

  • #15 / Jul 24, 2008 7:53am

    kungfumonkey

    15 posts

    Hi everyone!

    Session variables are just perfect;)

    There is only one more thing which gives me some trouble..

    1) I have a template with weblog entries loop

    2) Each entry is displayed in a styled div

    3) Each div’s id is the entry {title}

    4) I have an input tag with onclick event for each div

    5) Onclick event is setting session variable

    Simplified, it looks like:

    {exp:weblog:entries weblog="my_weblog"}
    
    <div id="{title}">{body}<input id="{title}"></div>
    
    {/exp:weblog:entries}


    Basically, I want the session variable value to be the current entry’s title.

    Now, that’s the tricky bit;). If I put sth like {exp:session_variables:set name="vt_vatr" value="{title}”} in the onclick event, it’s always the title of the last entry which is stored to the variable.

    What I want to have insead is ‘the current one’. If I knew how to store the id of the actual input tag (being clicked on) to the variable, I think it would be solved.

    Can anyone help?

    Kind regards

    Luke S.

.(JavaScript must be enabled to view this email address)

ExpressionEngine News!

#eecms, #events, #releases