0
$updateLog = LOG_HISTORY($objOld,$objNew,"Voucher",$newVoucherNo,'UPDATE','REUSE');
echo "<script> if(confirm('Voucher has already been used! Proceed reuse?')){".$updateLog.";}</script>";

I'm trying to make a confirm box using php where if user clicks ok, the data will be inserted in function LOG_HISTORY, and if the user clicks cancel nothing will happen.

The problem is when I click cancel, the data still gets inserted to the function. Please help

Sarah
  • 19
  • 4
  • 3
    You're calling `LOG_HISTORY` when you assign `$updateLog`, not when the user answers the confirm box. – Barmar Jan 16 '20 at 05:09
  • 3
    PHP runs on the server when the page is being created. By the time the user answers the confirm box, PHP has done all its work. You need to use AJAX to send requests back to the server. – Barmar Jan 16 '20 at 05:10
  • how do I do that? – Sarah Jan 16 '20 at 05:11
  • Read AJAX tutorials. That's more than we can help you with here. – Barmar Jan 16 '20 at 05:19
  • 1
    Does this answer your question? [What is the difference between client-side and server-side programming?](https://stackoverflow.com/questions/13840429/what-is-the-difference-between-client-side-and-server-side-programming) – Dharman Jan 16 '20 at 07:44

1 Answers1

1

You are mixing php & javascript (client & server side). LOG_HISTORY function is called before user even sees your confirm dialog as it runs on server. Below is the execution flow

Php execute the php script (LOG_HISTORY called) => HTML generated and sent to client browser => Confirm dialog shown

Prabath Perera
  • 240
  • 2
  • 9
  • You're absolutely correct. To put it differently: even though the code appears together in the same source file, the PHP is getting executed on the server, the Javascript is executed in a *COMPLETELY DIFFERENT PLACE* (on the user's browser) at a *MUCH LATER TIME* (long after PHP has sent its response). – FoggyDay Jan 16 '20 at 06:46