www.webdeveloper.com
Results 1 to 6 of 6

Thread: PHP Confirmation Box on Delete

  1. #1
    Join Date
    Feb 2010
    Posts
    41

    PHP Confirmation Box on Delete

    I have a Javascript function call on a link which pops open a ui dialog confirmation box and on confirmation uses ajax to delete a row in the database. This all works fine.

    However, I am questioning how to also code in some sort of php confirmation box IF javascript is turned off?

    I have this link
    Code:
     <a href="#" onclick="delete_row('districts', '''. $district_id . "', 'district_id')">Delete</a>
    Then of course the js function which opens the dialog box and sends to ajax if delete is confirmed
    Code:
    	function delete_row (db_table, id, field) {
    
    		$( "#dialog-confirm" ).dialog({
    			  resizable: false,
    			  height:220,
    			  width: 400,
    			  modal: true,
    			  buttons: {
    				"Delete": function() {
    					
    						var parm_vals = 'table:'+db_table+',id:'+id+',field:'+field;
    						var datastring = 'func=delete_row&parms='+parm_vals;
    					
    				 		/*call to ajax to delete the row*/
    					   $.ajax({
    						 type: 'POST',
    						 url : 'resources/ajax/ajax.php',	
    						 data: datastring,
    						 success : function(){
    							location.reload();
    						 }
    					  });				 
    
    				},
    				Cancel: function() {
    				  $( this ).dialog( "close" );
    				}
    			  }
    		});
    		
    		$("#dialog-confirm").dialog("open");
    The above code all works fine when using javascript and ajax.

    My questions are.....

    1) How do I code the delete link to go to the ajax.php page IF javascript is not turned on

    AND

    2) How do I get PHP to open the dialog box for confirmation?

    Thanks in advance for clarity on this.

  2. #2
    Join Date
    Jul 2013
    Location
    Voorheesville NY USA
    Posts
    663
    Without JS you have no client control. That said, I would include a hidden field on your first page, put some js code in there to set that field to a value onload and then when the form is submitted for a delete, check (in php) if that field was set by JS. If not, you will have to retain the info re: the delete to be done, send a confirmation page via php and await its return and proceed as necessary. A bit more traffic than if JS was enabled, but it gets the job done. You might include some code in your php confirmation page to tell the user that JS would have made this process easier, in case he/she has deliberately disabled JS and can opt to re-enable it.

  3. #3
    Join Date
    Feb 2010
    Posts
    41
    I understand what you are saying, but I think there is some simple way in the link that I am missing.

    I think it should go something like this....

    Code:
    $data .= '<a href="resources/ajax/ajax.php?func=delete_row&parms=table:user,id:6,field:id" onclick="if( delete_row(\'users\', \''. $user_id .'\', \'id\') ) { return false; }">
    So onclick, if javascript is on, the js function will be called which then will reload to the proper url.

    If javascript is not on, the js function will be ignored and not do anything so the href url will then be used and go to the appropriate page to parse the get variables.

    I hope this makes sense. Basically, make a js function call on click. If js is not enabled, the js function will be ignored, so then use the href to redirect the page.

    I've been trying forever to make this work.

    Is this possible?

    Thanks in advance.

  4. #4
    Join Date
    Jul 2013
    Location
    Voorheesville NY USA
    Posts
    663
    Sure - that approach works just fine. But - your original post asked how to get a PHP confirmation box when JS was not present. Your last post leaves that out of the equation.

    PS - I really don't know if your posted 'onclick' code was verbatim or some pseudo-code - it doesn't look viable to me. I also don't know how an onclick would respond if JS was not enabled.

  5. #5
    Join Date
    Feb 2010
    Posts
    41
    I appreciate your reply but my question wasn't answered.

    If a link looks like this,

    Code:
    <a href="resources/ajax/ajax.php?func=delete_row" onclick="if( delete_row('users') is not ran because js is off ) { 'go to href link' }">
    If javascript is on, it goes to the href link and ignores the js function call.

    If javascript is off, it doesn't use the 'href' url.

    All I want to do is call a javascript function if javascript is on, and use the href url if javascript is off.

    How do I code that to do this?
    Last edited by waMP; 11-19-2013 at 03:56 PM. Reason: mis wording

  6. #6
    Join Date
    Jul 2013
    Location
    Voorheesville NY USA
    Posts
    663
    I thought you had answered your own question with your last post - except for the part about a PHP confirmation box which I pointed out that you hadn't addressed. I also pointed out that your onclick= doesn't look like anything I've seen.

    I'd use something like:
    Code:
    <a href="(my url)" onclick="return deleteRow('somekeyvalue')">Click to Delete</a>
    where deleteRow is a js function in your page and 'somekeyvalue' is something to identify the row to be deleted by JS/AJAX.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



Recent Articles