none
Return code from php to HTML RRS feed

  • Question

  • This may be a silly question. I have a php file that receives form input from an html page. I want to pass a return code to another html page, which will take action depending upon the return code. Here are the details

    The html page that contains the form is at https://JolyonHallows.com/Unsubscribe.html. It gets an email address which it passes to the php file at .../Unsubscribe.php. The php file scans a text file, registrants.txt, searching for the email address. I want to return a code 0 if the email address was found, 1 if it wasn't. If it wasn't, I want to issue a Javascript alert to the user. The php code follows. Each line of registrants.txt contains an email address and a name, separated by a space.

    How do I get the variable $Match back to .../Unsubscribe.html and handle it there?

    Thanks.

    <?php
    $email = strtolower($_POST['email']);
    $myfile = fopen("registrants.txt", "r") or die("Unable to open file!");
    $tempfile = fopen("tempreg.txt","w") or die ("Unable to create file!");
    $Match=0;
    While (!feof($myfile)) {
    $line = fgets($myfile);
    $mail = strtolower(strtok($line," "));
    if ($email != $mail) {
    fwrite ($tempfile,$line);
    }
    else {
    $Match=1;
    }
    }
    fclose($myfile);
    fclose($tempfile);
    rename ("registrants.txt", "oldfile.txt");
    rename ("tempreg.txt", "registrants.txt");
    echo "<script>location.href='Unsubscribe.html';</script>";
    ?>

    Saturday, November 17, 2018 2:12 AM

Answers

  • You can append the value as a query string parameter to your 'Unsubscribe.html" href, then in the Unsubscribe page, use JavaScript to parse the query string values and pop up the alert.   Modify your PHP page:

    echo "<script>location.href='Unsubscribe.html?m={$Match}';
    

    And inside Unsubscribe.html,

    <script>
    var qs = window.location.search.substring(1).replace("m=","");
    if(qs == "0")
    {alert("Say the thing for 0");}
    else if(qs == "1")
    {alert("Say the thing for 1");}
    </script>
    I'm assuming you don't do anything if no parameter is passed, or if wrong parameters are passed to the page.   It's up to you to decide how robust it needs to be.


    Kathleen Wilber
    BrightWillow - Asp.Net Applications

    • Marked as answer by Jolyon Hallows Tuesday, November 20, 2018 4:45 PM
    Sunday, November 18, 2018 4:35 PM