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.
Page 1 of 2 12 LastLast
Results 1 to 15 of 30
  1. #1
    Senior Coder
    Join Date
    May 2004
    Posts
    1,464
    Thanks
    15
    Thanked 0 Times in 0 Posts

    works onclick from link but not from form

    I have the 2 scripts below

    the first works when I call the ajax from link

    the second doesn't

    can anyone spot this isssue - i have been spending hours on this

    PHP Code:

    <!DOCTYPE html>
    <
    html>
        <
    head>
            <
    script type="text/javascript" src="http://code.jquery.com/jquery-2.1.0.min.js"></script>

            <script language="Javascript">
                function successCallBack(returnData) {
                    // the main process have to be here<br>
                    alert(returnData);
                    confirm('Are you sure you want to delete' +returnData);
                }

                function getcityvalue(cityid)
                {

                    // this will generate another thread to run in another function
                    jQuery.ajax({
                        url: 'city.html',
                        type: 'get',
    //                    dataType: 'text/html',
                        success: successCallBack
                    });
                }
                function cityconfirm()
                {

                    alert("here");
                    // var cityid = document.getElementById('city').value;
                    var cityid = 1;
                    getcityvalue(cityid);
                }

            </script>
        </head>
        <body>
            <a href=# onClick="cityconfirm()">click here</a>
        </body>
    </html>



    and the second is 

    <!DOCTYPE html>
    <html>
        <head>
            <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>

            <script language="Javascript">
                function successCallBack(returnData) {
                    // the main process have to be here<br>
                    alert(returnData);
                    confirm('Are you sure you want to delete' +returnData);
                }

                function getcityvalue(cityid)
                {

                    // this will generate another thread to run in another function
                    $.ajax({
                        url: 'city.html',
                        type: 'post',
                        dataType: 'text/html',
                        success: function(){
                            successCallBack();
                        }
                    })
                }
                function cityconfirm()
                {

                    alert("here");
                    // var cityid = document.getElementById('city').value;
                    var cityid = 1;
                    getcityvalue(cityid);
                }
                

            </script>
        </head>
        <body>
          <form  name="x" method="post" ><br><b>Delete City: </b><select name="city" id="city"><br><option value='554'>chicago</option><option value='1'>New York</option><option value='2'>Albany</option></select>&nbsp;&nbsp;<input type="button" name="submit" onClick="cityconfirm()" value="Edit">&nbsp;&nbsp;<input type="button" name="submit" value="Delete" onClick="cityconfirm()"></form>
      
        </body>
    </html> 
    and here is city.html for easy testing

    PHP Code:
    chicago 

  • #2
    Senior Coder
    Join Date
    Dec 2010
    Posts
    2,398
    Thanks
    12
    Thanked 570 Times in 563 Posts
    The second one doesn't work because you didn't add the necessary parameter to the success callback
    Code:
                        success: function(data){ 
                            successCallBack(data); 
                        }

  • #3
    Senior Coder
    Join Date
    Jan 2011
    Location
    Missouri
    Posts
    4,438
    Thanks
    23
    Thanked 630 Times in 629 Posts
    Code:
    <!DOCTYPE html>
    <html>
    <head>
    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
    <script>
    function successCallBack(returnData) {
    alert(returnData);
    confirm('Are you sure you want to delete  ' +returnData);
    }
    function getcityvalue(cityid){
    	$.ajax({
    	url: 'city.html',
    	type: 'post',
    	success: function(data){
    	successCallBack(data);
    	}
    	});
    }
    function cityconfirm(){
    	alert("here");
    	var cityid = 1;
    	getcityvalue(cityid);
    }
    </script>
    </head>
    <body>
    <form  name="x" method="post" ><br>
    	<b>Delete City: </b>
    		<select name="city" id="city">
    			<option value='554'>chicago</option>
    			<option value='1'>New York</option>
    			<option value='2'>Albany</option>
    		</select>&nbsp;&nbsp;
    	<input type="button" name="submit" onClick="cityconfirm()" value="Edit">&nbsp;&nbsp;
    	<input type="button" name="submit" value="Delete" onClick="cityconfirm()">
    </form>
    </body>
    </html>
    Evolution - The non-random survival of random variants.

    "If you leave hydrogen alone, for long enough, it begins to think about itself."

  • #4
    Senior Coder
    Join Date
    May 2004
    Posts
    1,464
    Thanks
    15
    Thanked 0 Times in 0 Posts
    I tried your code but it deson't work for me

    only on a link not on a form?

  • #5
    Senior Coder
    Join Date
    Jan 2011
    Location
    Missouri
    Posts
    4,438
    Thanks
    23
    Thanked 630 Times in 629 Posts
    You know that "it deson't work" don't tell me anything? It really don't hemp me or you. WHAT is not working? Did you add code to the above? did you change anything thing?
    and what did you think it was going to do that you think it isn't doing?

    The code should run through all three functions and end with the
    confirm('Are you sure you want to delete chicago'); message. Does it?
    Evolution - The non-random survival of random variants.

    "If you leave hydrogen alone, for long enough, it begins to think about itself."

  • #6
    Senior Coder
    Join Date
    May 2004
    Posts
    1,464
    Thanks
    15
    Thanked 0 Times in 0 Posts
    when I run that code (and I can send you files if that helps) with the select box it doesn't return to the confirm message - it ignores showing there is an error

  • #7
    Senior Coder
    Join Date
    Jan 2011
    Location
    Missouri
    Posts
    4,438
    Thanks
    23
    Thanked 630 Times in 629 Posts
    Post the entire code your running
    Evolution - The non-random survival of random variants.

    "If you leave hydrogen alone, for long enough, it begins to think about itself."

  • #8
    Senior Coder
    Join Date
    May 2004
    Posts
    1,464
    Thanks
    15
    Thanked 0 Times in 0 Posts
    <!DOCTYPE html>
    <html>
    <head>
    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>

    <script language="Javascript">
    function successCallBack(returnData) {
    alert(returnData);
    confirm('Are you sure you want to delete ' + returnData);
    }
    function getcityvalue(cityid) {
    $.ajax({
    url: 'city.html',
    type: 'post',
    success: function (data) {
    successCallBack(data);
    }
    });
    }

    function cityconfirm()
    {

    alert("here");
    // var cityid = document.getElementById('city').value;
    var cityid = 1;
    getcityvalue(cityid);
    }


    </script>
    </head>
    <body>
    <form name="x" method="post" ><br><b>Delete City: </b><select name="city" id="city"><br><option value='554'>chicago</option><option value='1'>New York</option><option value='2'>Albany</option></select>&nbsp;&nbsp;<input type="button" name="submit" onClick="cityconfirm()" value="Edit">&nbsp;&nbsp;<input type="button" name="submit" value="Delete" onClick="cityconfirm()"></form>

    </body>
    </html>

  • #9
    Senior Coder
    Join Date
    Jan 2011
    Location
    Missouri
    Posts
    4,438
    Thanks
    23
    Thanked 630 Times in 629 Posts
    @esthera,
    It does what I said it would It give three alerts
    1. here
    2. chicago
    3. Are you sure you want to delete chicago

    "chicago" is the returned data from city.html It does this when Edit or Delete button is pressed.
    WHAT do you want it to do?
    Evolution - The non-random survival of random variants.

    "If you leave hydrogen alone, for long enough, it begins to think about itself."

  • #10
    Senior Coder
    Join Date
    May 2004
    Posts
    1,464
    Thanks
    15
    Thanked 0 Times in 0 Posts
    that's exactly what I want it to do but it's not working for me

    it's just trying to submit the form when I try it

  • #11
    Senior Coder
    Join Date
    Jan 2011
    Location
    Missouri
    Posts
    4,438
    Thanks
    23
    Thanked 630 Times in 629 Posts
    Don't know why. Your <form> don't have an action, you JS don't have a document.x.submit(); And your not using a return.

    You really don't need a form for this at all. And you should be using buttons instead of an input.

    See if this makes a difference
    Code:
    <body>
    
    <b>Delete City: </b>
    <select name="city" id="city">
    	<option value='554'>chicago</option>
    	<option value='1'>New York</option>
    	<option value='2'>Albany</option>
    </select>&nbsp;&nbsp;
    <button onclick="cityconfirm();">Edit</button>&nbsp;&nbsp;
    <button onclick="cityconfirm();">Delete</button>
    
    </body>
    Also make sure your 'city.html' file is where it is suppose to be.
    If still not working post error message your getting and where it jumps out of the js
    Evolution - The non-random survival of random variants.

    "If you leave hydrogen alone, for long enough, it begins to think about itself."

  • #12
    Senior Coder
    Join Date
    May 2004
    Posts
    1,464
    Thanks
    15
    Thanked 0 Times in 0 Posts
    thanks but I need to have a form submit

    as if the confirm is pressed it goes to the action to delete

    I am not getting any error - it just submits the form when I press delete (without activating the call back js)

  • #13
    Senior Coder
    Join Date
    Jan 2011
    Location
    Missouri
    Posts
    4,438
    Thanks
    23
    Thanked 630 Times in 629 Posts
    How are submitting the form without an action attribute? Think there is code your telling us about.
    Evolution - The non-random survival of random variants.

    "If you leave hydrogen alone, for long enough, it begins to think about itself."

  • #14
    Senior Coder
    Join Date
    May 2004
    Posts
    1,464
    Thanks
    15
    Thanked 0 Times in 0 Posts
    I have an action

    Maybe I can change the delete to a button but then if they press confirm how do I get it to call my server side code to delete?

    here is the page uploaded (please remove the spaces) http://www.dbni hul.com/testajax3.html#

  • #15
    Senior Coder
    Join Date
    Jan 2011
    Location
    Missouri
    Posts
    4,438
    Thanks
    23
    Thanked 630 Times in 629 Posts
    The program does not work on your site, but I downloaded it and it works on my computer. What I would suggest is to stop function jumping and do what you want in a single function and see what that does for you.
    Evolution - The non-random survival of random variants.

    "If you leave hydrogen alone, for long enough, it begins to think about itself."


  •  
    Page 1 of 2 12 LastLast

    Posting Permissions

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