1

I have a PHP script that checks if a user has been booked in for the last year. if they haven't then they cant be booked in, however I have an alertify JavaScript which will prompt if the user wishes to proceed/override with the booking. I know they're client and server side. So here what i have done so far.

if the $retemp is 1 then its has not been booked in for the last year, i want an alert with an override option. Im not fully understanding, I think I'm in the right direction with POST and ajax.

if($retemp == 1)
                {
                    ?>
                    <script type="text/javascript">
alertify.set({ labels: {
    ok     : "Yes",
    cancel : "No"
} });

// confirm dialog
alertify.confirm("Proceed With Booking", function (e) {
    if (e) {
        $.ajax({
    data: {confirm: 'yes'},
    url: 'phpurl.php',
    method: 'POST', // or GET
    success: function(msg) {
        alert(msg);
    }
});
    } else {
        // user clicked "cancel"
    }
});

</script>
2
  • 2
    So what exactly do you need help with? Commented May 23, 2016 at 15:03
  • im struggling to understand, how to trigger the rest of the php script from a JavaScript alertify? For example if yes continue with the rest of php script, else do nothing. Commented May 23, 2016 at 15:31

2 Answers 2

1

Yes, you're on the right track. The only way to communicate between server and client in this context (with no reloading pages) is ajax.

Your php file "phpurl.php" needs to receive $_POST['confirm'], plus whatever identifying information you want to pass to it via other data points in your "data:" object. It will then need to at minimum do the following

1) send a http header status code 2) output a string as output

The http header status code can be set with http://php.net/manual/en/function.header.php

//i.e. for a minimal success setting
header(200);
echo 'Whatever Message You want the Ajax call to see';

see http://jsonapi.org for best practices using json responses to ajax requests. Always a reasonable option to go with for this type of thing.

BTW, the header might be set automatically for 200, you'd have to do some testing. I always make it a habit to set it intentionally myself.

EDIT WARNING: Unsolicited advice follows.

1) You may want to consider your jQuery success function to receive the all clear in one function, and implement the failure function to handle "not all clear" on certain status codes to identify which use cases you're responding to. See https://en.wikipedia.org/wiki/List_of_HTTP_status_codes

2) jQuery $.post is an accessible shorthand version of $.ajax -- see notes on https://api.jquery.com/jQuery.post/

3) from the above link

The jqXHR.success(), jqXHR.error(), and jqXHR.complete() callback methods introduced in jQuery 1.5 are deprecated as of jQuery 1.8. To prepare your code for their eventual removal, use jqXHR.done(), jqXHR.fail(), and jqXHR.always() instead.

*EDIT 2 because OP asked about POST not GET

Sign up to request clarification or add additional context in comments.

16 Comments

Thanks, I will have a go and let you know how I get on.
Sounds good - FYI I updated my answer with some other potentially helpful thoughts. Depends on how deep down the rabbit hole you want to go
Really appreciate your help, i have an If statement that checks if variable $confirm equals yes. You mention a GET, like this? $confirm = (isset($_GET['overridedate']) ? $_GET['overridedate'] : NULL);
AGH! I apologize, and will clarify my post for future users. I should have referred to $_POST or $_REQUEST, because you're using post in your ajax call. To answer your question though, you'll be checking $confirm = (isset($_POST['confirm']) ? $_POST['confirm]: null;
Thats okay, so how can this trigger a php script?
|
0

In response to the broader question: "How to implement confirm alert within PHP script?"

<?php

/**
 *      file: confirm_action-example-PHP.php
 *     usage: php  confirm_action-example-PHP.php
 * reference: https://stackoverflow.com/questions/37393662/how-to-implement-confirm-alert-within-php-script
 */

echo "\n\tAbout to execute script - continue [y|n]? ";
$input = rtrim(fgets(STDIN));
// echo "$input: " . $input;

if ($input == 'y') {
  echo "\n\tPlease type \"continue\": ";
  $input2 = rtrim(fgets(STDIN));
  if ($input2 == 'continue') {
    echo "\n\t... continuing script ...\n";
  } else {
    echo "\n\twrong input\n\n";
    exit();
  }
} elseif ($input == 'n') {
  echo "\n";
  exit();
} else {
  echo "\n\twrong input: require 'y' or 'no'\n\n";
  exit();
}

// use double quotes to allow \n :
echo "\n\tThis statement should only appear if user input is both { \"y\" | \"continue\" }:\n\n";

exit();
?>

[victoria@victoria]$ php confirm_action-example-PHP.php

    About to DROP TABLES - continue [y|n]?    ## <Enter> pressed

    wrong input: require 'y' or 'no'

[victoria@victoria]$ php confirm_action-example-PHP.php

    About to DROP TABLES - continue [y|n]? n

[victoria@victoria]$ php confirm_action-example-PHP.php

    About to DROP TABLES - continue [y|n]? y

    Please type "continue": apple             ## << explicitly requires: continue

    wrong input

[victoria@victoria]$ php confirm_action-example-PHP.php

    About to DROP TABLES - continue [y|n]? y

    Please type "continue": continue

    ... continuing script ...

    This statement should only appear if user input is both { "y" | "continue" }:

[victoria@victoria]$ 

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.