Durga Durga - 2 months ago 4x
PHP Question

Storing values in a database via AJAX not working

I have this function which uses ajax, but this function doesn't work. I tried a lot, but I'm not able to figure out where the problem is. I am trying to create an alert, when the user inserts a duplicate entry into the database using a check-box selection.

function func(e,eid,emprid) {
var dataString = 'eid=' + eid + '&emprid='+emprid;
url: "mylistcheck.php",
data: dataString,
success: function(result){
if(result!='0') {
modal.open({content: "<span style=\"color:red\" ><h2>You have already selected candidate </h2></span>"});

looks like this:

<?php require_once("includes/session.php"); ?>
<?php require_once("includes/connection.php"); ?>
<?php require_once("includes/functions.php"); ?>

$sqlchecklist="SELECT * FROM selected_candidate WHERE eid='{$eid}' AND rid='{$emprid}' ";
echo "numrows listcheck".$list_check;
if($list_check>0) {
echo "1";
} else {
echo "0";

The check-box code is like this:

echo "<td><input id=\"select_candi{$i}\" onclick=\"javascript:func(this.id,{$data_set['eid']},{$emprid})\" type=\"checkbox\" name=\"check_candi[]\" value=\"{$data_set['eid']},{$emprid}\"/></td>";


In your code you have

echo "eid".$eid;
echo "rid".$emprid;

Since you are already echoing the result your ajax functie will never be just '0'. Its always something like eid{value}rid{value}0 or eid{value}rid{value}1

Also switch to mysqli or pdo for security reasons. Also check the values of $eid and $rid to match what you expect. Your code is vulnerable for SQL injection.

In your script code you have onclick="javascript:func(...)". onclick is already a javascript function, so you dont need the javascript: