krishna kumar
krishna kumar

Reputation: 153

alert message displays in new window

I want to display alert message if an condition fails.so i used the following code to display the message

print '<script type="text/javascript">';
print 'alert("You cannot leave field empty")';
print '</script>'; 

It works fine but the problem is, it is showing message in new window with blank page as background.I need to show this message in same window.Please help to figure out a way to do this.

Thanks in advance.

Upvotes: 0

Views: 2073

Answers (3)

IJMorgado
IJMorgado

Reputation: 137

I think you need to do something like this in the same page of your form and don't redirect your form data to another page...(I supposed you're trying to validate a form by the alert message):

<?php
    if(isset($_POST) && count($_POST)){
        // do some validation for data...

        if(!valid){
               print '<script type="text/javascript">';
               print 'alert("You cannot leave field empty")';
               print '</script>';
        }

    }

    // as it's the same page of your form...all the code of form and other stuff goes           here....
    ?>


    <form action="" type="POST">
             <!-- all inputs and elements of form
    </form

In this way the alert will be shown in the same page of your form...if validation of submit is wrong....

Upvotes: 0

Arun P Johny
Arun P Johny

Reputation: 388316

show it on load

print '<script type="text/javascript">';
    print 'window.onload = function(){'
    print 'alert("You cannot leave field empty")';
    print '};'
print '</script>'; 

Upvotes: 1

Samy
Samy

Reputation: 632

Wrap up the code inside if condition. Then put it in the same page where you want to show the alert..

if (condition) {
  print '<script type="text/javascript">';
  print 'alert("You cannot leave field empty")';
  print '</script>'; 
} else {
   //Normal flow
}

Upvotes: 0

Related Questions