1

I am in a pickle right now... I have this function that gets 2 variables from a page then run a sql query after it grabs the data.

function delete(id,date){
    confirmdel=confirm('Are you sure?');
    if(confirmdel){
      var curid = id;
      var when = date;

      //sql code should go here   

    }
    else{
      //don't do anything
      return false;
    }
  } 

How do I run an sql query from the function or send the data to a php file? I tried using jquery but it would just ignore it, so if I can use jquery please explain/give an example of how to use it.

Funk Forty Niner
  • 74,450
  • 15
  • 68
  • 141
cbr0wn
  • 361
  • 2
  • 6
  • 15

4 Answers4

4

JavaScript can't deal with databases on its own, it just can tell the browser what to do and is only loaded after the server has submitted the page to the browser. So I guess you'll have to work with AJAX. This is quite simple using jQuery:

...

$.post('process.php', {id:curid, date : when}, function(data) {

  alert(data);
  //data contains all output from process.php, 
  //either in json-format if you jsonencode a results-array
  //or just a simple string you echo in the php

  return false; //prevent from reloading the page
});

...

Your process.php could look something like:

<?php

$curid = $_POST['id'];
$when = $_POST['date'];

//run the query

echo jsonencode($results_array);

//or check if query succeeded and echo e.g. 'ok' or 'failed'
?>

You get the idea... ;)

//EDIT:

You should probably use the jQuery UI for the dialog-box to avoid the message as described in the comments. It could be something like this:

<div id="dialog_box" style="display: none;">
    Really delete?
</div>

$('#dialog_box').dialog({
  title: 'Really delete this stuff?',
  width: 500,
  height: 200,
  modal: true,
  resizable: false,
  draggable: false,
  buttons: [{
  text: 'Yep, delete it!',
  click: function(){
      //do the post
    }
  },
  {
  text: 'Nope, my bad!',
  click: function() {
      $(this).dialog('close');
    }
  }]
});
Quasdunk
  • 14,944
  • 3
  • 36
  • 45
  • 2
    Man, two Cartmans are too much for one post :D (Totally unrelated comment to the question) – tugberk Sep 03 '11 at 14:27
  • 1
    @tugberk LOL, didn't even notice that, but that's... well... 'Screw you guys, I'm going home!' :-D – Quasdunk Sep 03 '11 at 14:31
  • When I did this it works, but I get a message saying whether to show extra alert boxes anymore. How do I get rid of that? does anyone else get a message like that? – cbr0wn Sep 03 '11 at 17:25
0

Make a webservice. jQuery has functions that will allow you to call a webservice, but you can't build the webservice with jQuery.

Halcyon
  • 57,230
  • 10
  • 89
  • 128
0

AFAIK, you cannot directly run sql queries through JavaScript on the web page. Even if so, this would be the worst thing to do.

The best way is implementing is to write a service (REST service would be a good fit) which will connect to db, work with the data. Then working with that service through JavaScript would be a better approach.

have a look at the JQuery.ajax() API to find out how you can make Http posts to server.

tugberk
  • 57,477
  • 67
  • 243
  • 335
  • Can I use this jquery method in javascript? – cbr0wn Sep 03 '11 at 14:22
  • jQuery is written in JavaScript for JavaScript, of course you can use it. How else would you use it? – Halcyon Sep 03 '11 at 14:23
  • @cbr0wn JQuery is not another seperate language. It is a JavaScript library and a good one (I guess this is the reason why lots of people see it as another language). You can work with JQuery inside `` element on your web page. – tugberk Sep 03 '11 at 14:25
  • I meant can I use jquery methods in javascript functions? I ask because it does not always work for me. I know jquery and javascript are basically the same. – cbr0wn Sep 03 '11 at 15:20
  • of course it will work. check a sample : http://jsfiddle.net/tugberk/suYRL/ I guess you need to understand what JavaScript is at the first place or you will face a lot of troubles if you dive further. – tugberk Sep 03 '11 at 15:27
0

You should send the data to a php file.

$.post("delete.php", { id: id, date: date }, function(){alert('delete success!');} )
.error(function() { alert("error"); });
xdazz
  • 158,678
  • 38
  • 247
  • 274