Jump to content

can I create alert message for MySQL error when user click on checkbox


monim20

Recommended Posts

I am using given bellow php code for inserting data into database ,i have made "eid" and "rid" columns unique so that user is restricted to insert duplicate entry,I am getting error when user try to insert duplicate values ,but i want to alert user at the time of check-box selection itself,so how can i achieve this

 

<?php //insert selected candidate in table 
//echo "checkcandi".$_POST['check_candi']; 
if(isset($_POST['select_candidate']) && logged_in())
{
 $checkcandi_sel = $_POST['check_candi'];
 if(empty($checkcandi_sel)) 
 {
   echo("You didn't select any Candidate.");
 } 
 else
 {
   $N = count($checkcandi_sel);

   echo("You selected $N Candidate(s) ");
   /*for($i=0; $i < $N; $i++)
   {
     echo($checkcandi_sel[$i] . " ");
     $eid=$checkcandi_sel;
      echo "candi id".$eid;
   }*/
   if($N > 0) {
foreach($checkcandi_sel as $csel)
 {
$excsel = explode(",",$csel); 
   //echo($checkcandi_sel[$i] . " ");
   $eid=$excsel[0];
   $emprid=$excsel[1];
   //echo($emplyrid . " ");
   $selected_val="1";
   $query = "INSERT INTO selected_candidate (
                       eid,rid,selected_val
                   ) VALUES (
                        '{$eid}','{$emprid}','{$selected_val}'
                   )";
   $result = mysql_query($query, $connection) or die(mysql_error());
 }
}
} 
}
?>

and this is the chechbox code

 

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

Link to comment
Share on other sites

Yes, you can.

 

You'll need to use AJAX to check with the server. Which means having a PHP script that receives this request and checks with the DB, before sending a reply back. This reply must then be processed by the JS, and used to determine whether or not to show said alert.

I recommend using jQuery for this, as it'll make it a lot easier to handle the JS part. Without running into browser incompatibilities.

Link to comment
Share on other sites

I suggest you get a book on JQuery and AJAX. It's quite an expansive topic with multiple ways to solve most given problems. What works well for ChristianF would likely not make much sense to you from simply looking at the code and this will not help you get your head around AJAX calls and the power that they can provide. I was given an old book on AJAX and PHP from 2007 and it was about 235 pages(ish), without touching on JQuery. It's not a subject you will learn from a single, simple code example. Although ChristianF may just write the code for you, that normaly incurrs a charge.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.