-1

I made a form for newsletter subscription in my footer website. The code for this is:

<h4>Newsletter Subscribe:</h4> 
<form method="POST" action="<?php URL::show("Newsletter", "create") ?>">
    <div class="col-md-12">
        <div class="input-group">
            <input type="text" class="form-control" name="subscriber" placeholder="john.doe@domain.com">
            <input type="submit" class="btn btn-primary" value="Subscribe">
        </div>
    </div>
</form>

When I click on the submit button will call the createAction method from NewsletterController that looks like this:

public function createAction(){
    if($_SERVER["REQUEST_METHOD"] == "POST"){
        $validation = true;
        $error = "";

        if(trim($_POST["subscriber"]) == ""){
            $validation = false;
            $error = "The field 'subscriber' is required!";
        }

        if(trim($_POST["subscriber"]) != ""){
            if(!preg_match('/^([a-z0-9]+([_\.\-]{1}[a-z0-9]+)*){1}([@]){1}([a-z0-9]+([_\-]{1}[a-z0-9]+)*)+(([\.]{1}[a-z]{2,6}){0,3}){1}$/i', $_POST["subscriber"])){
                $validation = false;
                $error = "The field 'subscriber' must contain a valid input!";
            }
        }

        if($validation){
            $checkSubscriber = $this->newsletterRepository->checkSubscriber($_POST["subscriber"]);
            if(!$checkSubscriber){
                $newsletter = new Newsletter();
                $newsletter->setEmail($_POST["subscriber"]);
                $result = $this->newsletterRepository->insert($newsletter);
                if(!$result){
                    $error = "There was an error in the newsletter 
                }
            }else{
                $error = "The e-mail already registered for newsletter!";
            }
        }
    }else{
        URL::redirect("Restaurants", "listAll");
    }
}

Now, after I made all the validations and inserting the data in database, I don't want to go to another webpage, I want to display a popup with confirmation message if everything was ok or an error message if there was an error. Something like this:

<div class="modal fade bd-example-modal-lg" tabindex="-1" role="dialog" aria-labelledby="myLargeModalLabel" aria-hidden="true">
    <div class="modal-dialog modal-lg">
        <div class="modal-content">
            <div class="modal-header">
                <h5 class="modal-title">Newsletter Subscription</h5>
                <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                    <span aria-hidden="true">&times;</span>
                </button>
            </div>
            <div class="modal-body">
                <div class="container-fluid">
                    <?php if($result): ?>
                        <p style="color:green">Newsletter subscription successfuly!</p>
                    <?php elseif($error): ?>
                        <p style="color:red"><?php echo $error; ?></p>
                    <?php endif; ?>
                </div>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
            </div>
        </div>
   </div>
</div>

But I don't know how to trigger the modal after all the operations and validation are done. Can anyone help please?

tereško
  • 58,060
  • 25
  • 98
  • 150
Cipryst
  • 223
  • 1
  • 13
  • The best solution is probably to do that with javascript but than you have to send the form with javascript too, anyway without changing too much in your code you could reload the same page and in the second time you should render the message part instead of the form. – Sheki Dec 28 '17 at 11:43
  • do you get any notification when records are inserted into the table? – Prateik Darji Dec 28 '17 at 11:46
  • I asked how to display the modal, not how to insert the data in the table – Cipryst Dec 28 '17 at 11:48
  • i got your point but when you are inserting the records is there any message or toaster which can be displayed on the page after page is loaded? – Prateik Darji Dec 28 '17 at 11:50
  • if(!$result){ $error = "There was an error in the newsletter } esle { ?> – Prateik Darji Dec 28 '17 at 11:51
  • if you are looking on the modal window code, you will see there is an if statement that will display a confirmation message or an error – Cipryst Dec 28 '17 at 11:52
  • can u please try the else part in your controller and let me know if you get an alert message or not? – Prateik Darji Dec 28 '17 at 11:54
  • @PrateikDarji I unsered your question, what's wrong with my suggestion? – Sheki Dec 28 '17 at 11:54
  • It works! @PrateikDarji – Cipryst Dec 28 '17 at 11:56
  • @PrateikDarji I unsered your question, what's wrong with my suggestion? – Sheki Dec 28 '17 at 11:58

1 Answers1

0

One of the easiest ways I can think is using javascript. After the validation is done successfully you can call a javascript function like this:

 echo "<script type="text/javascript">the_function_you_want_to_call_from_javascript_file();
 </script>";

Than in your javascript file you can call the modal and make your own customization. Hope it helps :)

Idev Dev
  • 184
  • 17