1

Alright so I am trying to basically take what's in $row['cardid'] and set it into the value of my div tag. Then on the html file this is echoing to, it would run my function which uses that value from the div tag. Right now when I use the onclick, it pulls up but says my value is undefined. So my question is why is my variable undefined when I pull it up using html?

<?php
$q=$_GET["q"];
$con = mysql_connect("*", "*", "*");
if (!$con)
    {
    die('Could not connect: ' . mysql_error());
}
mysql_select_db("*", $con);
$sql="SELECT cardset, cardname, cardnumber, cardid FROM cards WHERE cardname LIKE '%".$q."%' OR cardset LIKE '%".$q."%'"; 
$result = mysql_query($sql) or die ('Error: '.mysql_error ());

echo "<table border='1'>
<tr>
<th>#</th>
<th>Cardname</th>
<th>Card Set</th>
</tr>";
while ($row = mysql_fetch_array($result)) 
{
echo "<tr>";
echo "<td>" . $row['cardnumber'] . "</td>";
// This is the troubled line.
echo '<td><div value="'.$row['cardid'].'" onclick="changeimage(this.value)">' . $row['cardname'] . '</div></td>';
echo "<td>" . $row['cardid'] . "</td>";
echo "<td>" . $row['cardset'] . "</td>";
echo "</tr>";
}
echo "</table>";
mysql_close($con);
?>
3

4 Answers 4

1

I don't fully understand your question, but you mean just something like this?

echo '<td><div onclick="changeimage(this.id)" id="'.$row['cardid'].'">' . $row['cardname'] . '</div></td>';

Use ID instead of value basically..

Example: jsFiddle

Sign up to request clarification or add additional context in comments.

8 Comments

That won't even activate my function with your supplied information.
That is bad @edwardmp. id can't be just numbers.
Thank you @edwardmp ! Changing it to ID made it work just like it should. Many thanks. And I will most defiantly look into changing everything to mysqli_* instead of mysql_*. Thanks again!
@edwardmp: Oh really? w3schools.com/tags/att_standard_id.asp - "Must begin with a letter A-Z or a-z"
I see. Thanks, you're right. He could use echo '<td><div onclick="changeimage('.$row['cardid'].'">'. $row['cardname'] . '</div></td>'; though which is correct and should work the same.
|
1

div elements doesn't have a value property, but you can get the value of the attribute named value - you should use .getAttribute("value"):

 echo '<td><div value="'.$row['cardid'].'" onclick="changeimage(this.getAttribute(\"value\")">' . $row['cardname'] . '</div></td>';

An example: http://jsfiddle.net/vaSYh/

Also, if you need to store data in DOM elements, use data- attributes. (Or just name them that way even if you don't have to support HTML5 right now; It will make the transition easier)

Comments

1

Don’t use this.value. It’s not a form.

Use this.innerText or this.innerHtml to get the content of the div

Comments

0

If I understand correctly your answer, you want to alert the value of $row['cardid']? With your code, you can't get the correct value, because you're mixing up javascript code with php code. The 'this' keyword in your 'onclick' event handler, is the javascript function itself, then it has not a 'value' property. Try this code, to alert the right value:

echo '<td><div value="'.$row['cardid'].'" onclick="changeimage('.$row['cardid'].'">'. $row['cardname'] . '</div></td>';

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.