0

how can i use the variable from my php-sql to become the message for my javascript alert?

heres my code

<?php

    $select = "SELECT * FROM post";
    $result = mysql_query($select) or die("couldn't select table");


    while ($rows = mysql_fetch_array($result))
    {
        echo"<input type='button' class=term onclick='return terms()' value=Terms >";

        echo"<input type='hidden' value='$rows[terms]' id='term' name='term'>";
    }
?>  

<script language="JavaScript">
    function terms() 
    {
        var readers = document.getElementById("term"); 
        alert(readers.value);
    }
</script>

It works, the alert message displays. But I got the problem on displaying the right message for specific fetch of row. All got the same message (message from the first row).

I tried to use getElementByName but the alert doesn't pop-up, so i dont have to try making the input name="term[]" --------(into its array form)

Help please!

6 Answers 6

1

I think it will be find if you change this line of script

 echo"<input type='button' class=term onclick='terms()' value=Terms >";

onclick='terms()' here, instead of onclick='return terms()'

EDIT:

<?php   echo"<input type='button' class=term onclick='terms(this)' data-result ='". $rows['terms'] ."' value=Terms >"; ?>
      /* Remove the hidden field */
<script language="JavaScript">
    function terms(e) 
    {
        var readers = e.getAttribute('data-result'); 
        alert(readers);
    }
</script>
Sign up to request clarification or add additional context in comments.

4 Comments

i changed, but alert doesn't showed when i used getElementByName
sorry i don't know what you mean console is... newbie here
by the way @JustinJohn is data-result a reserve word or just a variable? I googled it and there is no such result. :)
@ivory-santos: It's an attribute name. You can use any other word except the reserve words like name, class etc for that attribute. The data-result is data attribute. You can check about data attributes. You should learn basics of html elements.
1

Change your second echo statement inside the while loop to:

echo"<input type='hidden' value='". $rows['terms'] ."' id='term' name='term'>";

Comments

1

I usually do the following:

<?php
  function alert($text){
    echo '<script type="text/javascript">alert("'.$text.'")</script>';
  }

  alert("test");
?>

Hope that helps.

2 Comments

i dont get it, is "test" diff from "$text" ?
$text is a variable, "test" is the value that you set $text to.
0

You are overwriting the value or "term" in your HTML code after each iteration of While-loop. This way it will only get the last value

Comments

0

All of your input elements have the same id. If you want to distinguish them, give them different identifiers. And pass the identifier to the terms function.

Comments

0

You must give different id for each element. I think simple way to do that is by using auto increment for the id.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.