Krešimir Galić
Krešimir Galić

Reputation: 376

PHP or jQuery input changing text

I need help with PHP or jQuery.

What I need is when user type something in input, if that is valid comes another text. Specifically I need this:

This is my PHP code:

$ime = $_POST['ime'];
$godine = $_POST['godine'];
$poruka = $_POST['poruka'];
$nameErr = $old = $young = "";
if (isset($_POST['submit'])) {
    if(empty($_POST['ime'])) {
        $nameErr = "Upisite ime";
    }
    if ($godine < 18) {
        $young = "<span class=\"green\">Premladi ste za nastavak</span>";
    } elseif ($godine >= 18) {
        $old = "<span class=\"blue\">Čestitamo punoljetni ste<span>";
    } 
}

I don't know if that can be done with PHP but i am sure that jQuery did that.

Upvotes: 1

Views: 76

Answers (1)

Anoop Joshi P
Anoop Joshi P

Reputation: 25527

Try like this

$("#age").blur(function () {
    if (parseInt($(this).val()) >= 18) {
        $("#message").text("You are old enough");
    } else {
        $("#message").text("You are too young");
    }

});

Html

<span>Age:</span> 
<input type="text" id="age">
<span id="message"></span>

Fiddle

blur() event will fire whenever the focusout happens from the input element. Then you can use .text() method to give the message to user. Here i'm assigning the message to a span.

Upvotes: 3

Related Questions