0

I want to get the value of check box selected and pass it to a query for deleting the value from database. java script code

function deleteRows(){

isTable = document.getElementById('dataTbl');
nBoxes = document.getElementsByName('delBox');
for (i=nBoxes.length-1; i>=0; i--)
{
if (nBoxes[i].checked === true)
{
var a =nBoxes[i].value;


alert("Do you want to delete this row?"+a);
isTable.deleteRow(i+1);
}
}
}

i need the var a value in perl so that i can pass it to the delete query and delete the selected row.

html code

<Table id='dataTbl' border='1' >
<tr>
<td><input type=checkbox name='delBox' value=@data></td> 
<td bgcolor=#0099FF>$pid</td>
<td bgcolor=#99CCFF>$project_name</td>
<td bgcolor=#3399FF> $variant_no</td>
<td bgcolor=#99CCFF> $variant_name</td>
<td bgcolor=#3399FF>$vehicle_class</td>
<td bgcolor=#99CCFF> $vc_no</td>
</table>
<input type=button value="Delete Rows" onclick="deleteRows()" id="delbtn">

perl query

my $sth = $dbh->prepare("delete form table name col1,col2,col3 where id='$a'"); 
$sth->execute() or die "$!";
rss
  • 23
  • 8
  • You need to send that variable through POST or another HTTP request. Keep in mind that JS code executes in browser while Perl executes in server. And btw [SQL injection](http://en.wikipedia.org/wiki/SQL_injection) incoming... – m0skit0 Apr 07 '14 at 08:50
  • Thanks for the reply, but can you explain in more precised form. – rss Apr 07 '14 at 09:16
  • 1
    I think it's clear enough. If you didn't understand then I suggest you read more about HTTP client/server communication, you're lacking basic concepts like POST. – m0skit0 Apr 07 '14 at 11:36

2 Answers2

1

You have to do POST request (or DELETE to be precise) towards server where your perl script runs.

e.g.

After you get a variable set (let's say you are using jquery):

$.ajax({
  type: "POST",
  url: url, // where your script lives
  data: {'a' : a},
  success: function(data) {
      console.log(data);
  }
  dataType: 'json'
});

in your script you will then get 'a' variable from post request.

Slaven Tomac
  • 1,552
  • 2
  • 26
  • 38
0

Sample scenario:

  1. JS working on Your Client PC. Users selected some id.
  2. JS sends selected id to server side Perl CGI script.
  3. Perl parses GET request get id.
  4. Perl checks if id is number not some string to hack your server.
  5. Perl executes delete in MySQL.

Resourses: JS send GET request with Param: HTTP GET request in JavaScript?

Perl Read GET Parameter How can I read the URL parameter in a Perl CGI program?

Perl MySQL Tutorial: http://perl.about.com/od/perltutorials/a/perlmysql_3.htm

Community
  • 1
  • 1
Boris Ivanov
  • 4,145
  • 1
  • 32
  • 40