0

I have this code:

 while($row = mysql_fetch_array($res)) {
        $name = $row['advertiser'];
        $id = $row['id'];
        if($row['id'] % 4 == 1 || $row['id'] == 1)
        {
            echo "<tr>";
        }
        if($row['availability'] == 0)
        {
            $td = "<td id='green'>";
        }
        if($row['availability'] == 1)
        {
            $td = "<td id='grey'>";
        }
        if($row['price'] == 0)
        {
            mysql_query("UPDATE booklet SET availability = 0 WHERE id = '$id'");
        }
        echo $td . $row['id'] . "<br/><p style='font-size: 6pt;'>" . $name[0] . "<p><img src=" . $row['image'] . "></td>";

        if($row['id'] % 4 == 0)
        {
            echo "</tr>";
        }
}

It creates a table. What I want to do is this: if you click on a td, I want to pass the value of that td, for example - the number one (1), if you clicked on the first td - I want to pass this value over to a hidden input so I may later use it elsewhere.

The table looks fine. I know what to do when I have the value in a hidden input. I just need to be able to have the table, when I click on a td, to pass the value over. Or to do anything. onClick doesn't work. Even the absolute simplest isolated JQuery statements don't even come close to parsing. The most complex Javascript that actually has worked on this page is a document.write(). Everything else stumps any known browser.

So, using absolutely any methods, is it a possibility, within the realm of current technology, to have code that does what I want?

Again, I need to have a table cell, when clicked on, pass a variable to a hidden input. I've tried everything.

1
  • The question is about ensuring a hidden value in an HTML form is submitted and/or interactions with jQuery, browser events, etc. However the code presented is to the (irrelevant?) PHP back-end. Consider cleaning up the question and including relevant code: only the JavaScript/HTML as it is presented to the client. Commented Jan 22, 2011 at 22:52

3 Answers 3

2

You'll need to add the click event with JQuery and then use Jquery to set the value... Something like this should work.

$(function() {
    $("table#mytable td").mouseover(function() {
    //The onmouseover code
    }).click(function() {
        //The onclick code
        $("#hiddenInputID").val(text);
    });
});
Sign up to request clarification or add additional context in comments.

Comments

1
<td onclick="document.forms[0]['nameofhiddenfield'].value = this.id"> ... </td>

2 Comments

I think my problem lies in quotes and double quotes, since the page is being generated by PHP. I use double quotes first in the echo statement and then single quotes everywhere else, correct?
That's not a problem. PHP, HTML and JavaScript all support both double and single quotes. You will need to escape them though. In PHP and JavaScript, you can escape them with a `: echo 'It\'s ok'` while in HTML, you need to make a HTML entity of them: <img alt="Check the &quot;WOW&quot; effect in this foto" />.
0

Try to validate your html, you might have mismatched quotes/square brackets somewhere, which breaks Javascript.

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.