A little JS help please ~ nevermind i figured it out.

Discussion in 'JavaScript' started by -jay-, Dec 3, 2013.

  1. #1
    I really stink at javascript. I have 2 functions. Can someone help me combine into 1. Individually they work fine seperately.

    first I want to prompt a confirmation..

    function confirmAction(){
    var confirmed = confirm("Are you sure you want to delete?");
    return confirmed;
    }

    function remove_this(id) {
    var string_post = "UPDATE mytbl SET status='0' WHERE Id='" + id+"'";
    row_deactivate(string_post, function(){sel_prod()});
    }

    I tried doing this but doesnt work.

    function remove_this(id) {

    var confirmed = confirm("Are you sure you want to delete?");
    return confirmed;

    var string_post = "UPDATE mytbl SET status='0' WHERE Id='" + id+"'";
    row_deactivate(string_post, function(){sel_prod()});
    }

    thanks.
     
    -jay-, Dec 3, 2013 IP
  2. -jay-

    -jay- Well-Known Member

    Messages:
    2,311
    Likes Received:
    47
    Best Answers:
    0
    Trophy Points:
    160
    #2
    nevermind, i figured it out haha. thanks
     
    -jay-, Dec 3, 2013 IP
  3. PoPSiCLe

    PoPSiCLe Illustrious Member

    Messages:
    4,623
    Likes Received:
    725
    Best Answers:
    152
    Trophy Points:
    470
    #3
    The smart thing to do would then be to post your solution to this thread - that way, if people search for stuff, and happens upon your thread, they would benefit from your solution. Also, if your solution is inneficient or error-prone, or something akin those lines, somebody might see it and say "hey, you, you might wanna do it this way instead".
     
    PoPSiCLe, Dec 3, 2013 IP
  4. -jay-

    -jay- Well-Known Member

    Messages:
    2,311
    Likes Received:
    47
    Best Answers:
    0
    Trophy Points:
    160
    #4
    ok. Im glad you posted because actually my solution did not work correctly.

    I ended up just doing this :

    function remove_this(id) {
    confirmAction();
    var string_post = "UPDATE mytbl SET status='0' WHERE Id='" + id+"'";
    row_deactivate(string_post, function(){sel_prod()});
    }

    but what happens is regardless of whether i click yes or no the update query still runs. I need a way to stop both functions from running if no is clicked.
     
    -jay-, Dec 3, 2013 IP
  5. deathshadow

    deathshadow Acclaimed Member

    Messages:
    9,732
    Likes Received:
    1,999
    Best Answers:
    253
    Trophy Points:
    515
    #5
    Step 1, lose the "function for nothing" crap -- it's a waste of code, execution and download time.

    Step 2, you don't actually USE the result from that extra function for nothing... "well there's your problem..." -- if you don't do a IF, you're confirm dialog isn't gonna do jack.

    function remove_this(id) {
    	if (confirm("Are you sure you want to delete?")) {
    		row_deactivate(
    			"UPDATE mytbl SET status='0' WHERE Id='" + id + "'",
    			function() { sel_prod(); }
    		); 
    	}
    }
    Code (markup):
    You have to IF the confirm's result, and put what you want done on 'yes' inside the IF. Also avoid making functions and extra variables when you don't have to...

    Though I'm absolutely horrified from a security standpoint seeing a query in JavaScript...
     
    deathshadow, Dec 3, 2013 IP