Web Analytics Made Easy -
StatCounter Switching php variable in table with javascript? - CodingForum

Announcement

Collapse
No announcement yet.

Switching php variable in table with javascript?

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Switching php variable in table with javascript?

    I have a table that is built with php by retrieving data from a mysql DB like so:

    Code:
    while ($row = $stmt->fetch(PDO::FETCH_ASSOC)){
    
    
    echo '
        <tr>
    			<td>', $row['A'], '</td>
        </tr>
    I need to use javascript onclick to change $row['A] to $row['B'].


    Due to php being processed first, I'm pretty sure I can't just change the contents of the <td> from $row['A'] to $row['B'] using Javascript, or can I? If I store the php variable as Json, can I then just use javascript innerhtml to change the <td> contents from one Json variable to another?

    I'm not familiar with Ajax, if that's relevant. I guess I could just create all of the columns in the table with the php variables, and then use javascript to hide/unhide the correct columns, but that's going to be a lot of code, which I'm assuming will be slower.

    Any suggestions are much appreciated.

  • #2
    You need to think about what the outcome should actually be – I mean the outcome if no JavaScript is involved. JS should only serve as enhancement of already working code. Your site should work in and by itself already. If something makes no sense then it’s coded badly, and “fixing” it with JS is just like putting a patch on a broken leg.

    That said, you can certainly change the contents of the cell with JS after it has been printed. You wouldn’t change the variable but the cell content, though. However, you’d have to be more specific what you’re actually trying to do (design wise) for us to give you any specific help.
    Stop solving problems you don’t yet have!

    Comment


    • #3
      Originally posted by VIPStephan View Post
      You need to think about what the outcome should actually be – I mean the outcome if no JavaScript is involved. JS should only serve as enhancement of already working code. Your site should work in and by itself already. If something makes no sense then it’s coded badly, and “fixing” it with JS is just like putting a patch on a broken leg.

      That said, you can certainly change the contents of the cell with JS after it has been printed. You wouldn’t change the variable but the cell content, though. However, you’d have to be more specific what you’re actually trying to do (design wise) for us to give you any specific help.
      It's an interactive table showing the contents of something in a DB.

      It's not the case, but let's say the table is for pies. There are a series of buttons at the top denoting different types of pies, and when you select them, different ingredients are pulled from the DB to populate the table for each different pie. Except only the last 3 columns will need to change depending on which button is pressed.

      Does that help? I fI don't use JS, I will have to display all of the columns for all of the pies. I want it so that the user can select one pie at a time.

      Comment


      • #4
        First get it working the way you want it to for people who don't have JavaScript enabled and post the HTML code that produces - we will then be able to show you how to enhance that for the people with JavaScript.
        Stephen
        Learn Modern JavaScript - http://javascriptexample.net/
        Helping others to solve their computer problem at http://www.felgall.com/

        Don't forget to start your JavaScript code with "use strict"; which makes it easier to find errors in your code.

        Comment

        Working...
        X