Question

I want to show a message which writing are you sure … I can show the message but user click NO return false; did not work

<script>
function askCar(){
  var answer = confirm ("Are you sure for deleting car ?");
  if (answer){

  }
  else{
    return false;
  }
}
</script>

I call function with onclick here

echo '<td><a onclick="askCar()" href="arac-sil.php?sid='.$data["Car_ID"].'" class="ico del">Sil</a><a href="arac-duzenle.php?did='.$data["Car_ID"].'" class="ico edit">Düzenle</a></td>';
Was it helpful?

Solution

You need to use the return value:

<a onclick="return askCar();" ... >

Just having it inside the onclick will execute the function but to actually cancel the click, you need to use the return value.

Also, the code can be slightly optimized:

function askCar(){
    return confirm ("Are you sure for deleting car ?");
}

No need in if..else when you can directly return the dialog result. (unless of course you want to perform exra actions)

OTHER TIPS

You need to add the return also:

echo '<td>
    <a onclick="return askCar()" href="arac-sil.php?sid='.$data["Car_ID"].'" class="ico del">Sil</a>
    <a href="arac-duzenle.php?did='.$data["Car_ID"].'" class="ico edit">Düzenle</a>
    </td>';

Your onclick function needs to use the return value:

echo '<td>
         <a onclick="return askCar()" href="arac-sil.php?sid='.$data["Car_ID"].'" class="ico del">
            Sil
         </a>
         <a href="arac-duzenle.php?did='.$data["Car_ID"].'" class="ico edit">
             Düzenle
         </a>
      </td>';

When you use return within your function definition, you must have to use return in the place where you are calling that. Otherwise, you will not get proper result. So, the best code would be below

echo '<td><a onclick="return askCar()" href="arac-sil.php?sid='.$data["Car_ID"].'" class="ico del">Sil</a><a href="arac-duzenle.php?did='.$data["Car_ID"].'" class="ico edit">Düzenle</a></td>';
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top