Side
Side

Reputation: 1751

Javascript alert php result

First of all sorry im a really big beginner

I have a bit of a problem

i have a mysql query what select data from my users

the code looks like this

$r = mysql_query(" SELECT reader FROM users WHERE username = 'Tom' ") or die (mysql_error());
$result = mysql_fetch_array($r);

$vv = $result['reader'];

echo '<div id="reader">'.$vv.'</div>';

so it selects tom is a reader, the reader field contains yes or no, i echo it out it says yes, thats ok.

and i would like to alert this value with javascript.

i added an id reader

the javascript

<script>
var readers = document.getElementById("reader"); 
        alert(readers.value);



</script>

and im keep getting an alert window undefined.

could please someone point me to te right path what im missing? and is it possible to alert the value this way?

Upvotes: 0

Views: 696

Answers (3)

Rajasekar Gunasekaran
Rajasekar Gunasekaran

Reputation: 1827

Try this in jquery

<script>
      alert($("#readers").html());    
</script>

Upvotes: 0

tcnarss
tcnarss

Reputation: 595

.value will look for the value="" attribute of an element and therefore will throw back undefined if this attribute is not applied to the element in question.

So you don't want .value you want .innerHTML

Upvotes: 1

Baversjo
Baversjo

Reputation: 3716

element.value will try to read the "value" attribute for the element. Use innerHTML and you will get the elements including text inside of the element <el>this is the inner html</el> in a string:

alert(readers.innerHTML);

Upvotes: 4

Related Questions