sasori
sasori

Reputation: 5445

how to add a javascript alert with a PHP script?

let's say i have an input form, wherein, it's use for an email address. this form is part of a long form so i use an alert box instead when other inputs got errors....my question now is, if I checked the email input string via php, if it has been taken , how will I put the message like e.g "this email has been taken" in an alert box if i am using PHP to check it from backend ?..i want an alert box, since I use it with the other input boxes that don't need a backend check e.g

alert("$errormessage");

Upvotes: 1

Views: 13295

Answers (3)

Paula George
Paula George

Reputation: 69

echo '<script>alert("ssss");</script>' ; just put it within echo it will work that way

Upvotes: 0

Pehmolelu
Pehmolelu

Reputation: 3564

You can echo any kind of javascript with PHP. The question is however, are you sure you want to do this? You can also do like this:

<?php if(emailExists): ?>
<script>alert('Email exists!')</script>
<?php endif; ?>

But you could also use jQuery and Ajax requests to check if email exists with ajax when user has typed the email.

Upvotes: 2

genesis
genesis

Reputation: 50966

PHP is server-side language. you can output it to user with

echo "<script>alert('".mysql_real_escape_string($errormessage)."');</script>";

or you can write your own function

function alert($a){
    echo "<script>alert('".mysql_real_escape_string($a)."');</script>";

}

alert("test");

Upvotes: 5

Related Questions