473,382 Members | 1,348 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,382 software developers and data experts.

php and javascript question.

So I am confused on where to put things in my code to work right. I found a simple javascript tutorial to add a function so that i can hide and show my php tables. but when i put it all together i cant get it to work. It works fine displaying the table without the javascript added and adding the "<tr id="table"> line of code. I dont know if the javascript is in the wrong place or not.any help would be great.thanks

Expand|Select|Wrap|Line Numbers
  1. <?php
  2.  
  3. <script type="text/javascript">
  4. function displayRow(){
  5. var row = document.getElementById("table");
  6. if (row.style.display == '') row.style.display = 'none';
  7. else row.style.display = '';
  8.  
  9. }
  10. </script>
  11. $term = $_POST['term'];
  12. if ($_POST){
  13. $sql = mysql_query("select * from table where Name like '%$term%'");
  14.  
  15. echo "TABLE:";
  16. echo "<table border='1'>
  17.  
  18. <tr id="table">
  19. <th>Name</th>
  20. </tr>";
  21. while ($row = mysql_fetch_array($sql)){
  22. echo"<tr>";
  23. echo "<td>"  .$row['Name']. "</td>";
  24. echo "</tr>";
  25. }
  26. echo "</table>";
  27.  
  28.  "<p><button onclick="displayRow()" >Show sd</button></p>";
  29.  
  30. ?>
Dec 2 '09 #1
16 2053
Dormilich
8,658 Expert Mod 8TB
there is no such thing as
Expand|Select|Wrap|Line Numbers
  1. element.style.display = '';
(the display property always has a value)

further your php tags are out of place (I suppose it’s just a leftover)
Dec 2 '09 #2
Frinavale
9,735 Expert Mod 8TB
Since you're hiding and showing table rows your display CSS should probably be "table-row". See the valid values you that you can set the display property to on w3c: CSS Display.

-Frinny
Dec 2 '09 #3
i still get nothing..is it a problem that i have my php code inside of my html brackets. because i can place the javascript code anywhere but the second I change <tr> to <tr id=" something"> it wont work.
Dec 3 '09 #4
Frinavale
9,735 Expert Mod 8TB
Well it looks like your JavaScript is not right. This is what you have:
Expand|Select|Wrap|Line Numbers
  1. <script type="text/javascript">
  2. function displayRow(){
  3.   var row = document.getElementById("table");
  4.   if (row.style.display == '') row.style.display = 'none';
  5.   else row.style.display = '';
  6.  
  7. }
  8. </script>
Notice that you have a variable "row' and that you are assigning it to the table. This is very confusing because the name of the variable does not match what it contains.

If you want to hide/show a specific row then you have to get the row by either looping through all of the rows in the table until you find the one that you' want, or by using the document.getElementById method to retrieve the row itself.

If you want to use the document.getElementById method then you will have to give each <tr> an id so that you can retrieve it (for example <tr id="theIdForRow1">)

What do you want to do exactly? Hide/show the table? Or hide/show a specific row?

-Frinny
Dec 3 '09 #5
i want to hide/show my whole table.. this is my code for my working table that displays fine when a search is preformed.

Expand|Select|Wrap|Line Numbers
  1. $term = $_POST['term'];
  2. if ($_POST){
  3. $sql = mysql_query("select * from sequence_data  where name like '%$term%'");
  4. echo "SEQUENCE_DATA TABLE:";
  5. echo "<table border='1'>
  6.  
  7. <tr>
  8. <th>name</th>
  9. </tr>";
  10.  
  11. while ($row = mysql_fetch_array($sql)){
  12. echo"<tr>";
  13. echo "<td>"  .$row['name']. "</td>";
  14. echo "</tr>";
  15. }
  16. echo "</table>";
  17.  
  18.  
now i want to implement the javascript code to be able to have a button to click and have the table hide/show but i just cant get it to work
Dec 3 '09 #6
Frinavale
9,735 Expert Mod 8TB
It's pretty simple really, just change the style as you were changing it...

Give the table an ID so that you can get a reference to the element in your JavaScript method that's executed when you click the button.

Once you have a reference to it set the display property of the table element to either "none" or "table":
Expand|Select|Wrap|Line Numbers
  1. <script type='text/javascript'>
  2.  
  3. var hideTable = true;
  4. function HideDisplayTable(){
  5.   var tableElement= document.getElementById('tableID');//where tableID is the ID of the table element
  6.   if (tableElement)
  7.   { //if the table element exists, then set the style property
  8.     if(hideTable == true){
  9.       //if we want to hide the table then setting the table element's
  10.       //style to display:none...this will force the element not to display 
  11.       tableElement.style.display = 'none';
  12.  
  13.       //next time we want to show the table so we need to set the
  14.       //variable "hideTable" to false
  15.       hideTable = false;
  16.  
  17.     }else{
  18.       //In this case we want to show the table.
  19.       //In order to do this we have to specify that the table's display style
  20.       //is set to 'table' instead of 'none'. 
  21.       tableElement.style.display = 'table';
  22.  
  23.       //next time we want to hide the table so we need to set the
  24.       //variable "hideTable" to true
  25.       hideTable = true;
  26.     }//close check whether or not to hide the table
  27.   }//close the check to see if the table element exists
  28. }//close the function
  29. </script>
See? It's pretty straightforward once you understand how to set the style of an element using JavaScript :)

Now all you have to do is call the function on the onclick event of the button.

-Frinny
Dec 3 '09 #7
thank you. yes i understand that a little bit more. so in my table that i have in my php code should i set the id this way??

Expand|Select|Wrap|Line Numbers
  1. echo "<tableborder ='1'>
  2. echo "TABLE";
  3.  
  4. <tr id ="tableID">
  5. <th>"table"</th>
  6.  
becuase if i do it like above it wont work
Dec 3 '09 #8
and also does it make a difference that I have my php inside of my <body> brackets. this is my set up for my .php file

it goes

Expand|Select|Wrap|Line Numbers
  1. <html>
  2. <body>
  3.  
  4. *this is where i placed my javascript code
  5.  
  6. <?php
  7.  
  8. *inside here i have my tables being echoed in a loop when a search is done.
  9.  
  10.  
  11. ?>
  12. </body>
  13. </html>
  14.  
Dec 3 '09 #9
Dormilich
8,658 Expert Mod 8TB
didn’t you say you want to hide the whole table?
Dec 3 '09 #10
Samishii23
246 100+
Nope it doesn't matter if you have <?php ?> Tags inside the <body> tags. You can have as many <?php ?> tags are you want, where ever you want. On the user's end they'll never see any of them unless theres a problem on the server.

Ok I'm going to break down your original post, and give you some hints about how to code this stuff. I've seen a few errors you need to know about if the code from post #1 is exactly your code.

Problem 1:
Expand|Select|Wrap|Line Numbers
  1. <?php
  2. <script type="text/javascript">
Do not have the ANY HTML tags enclosed within the <?php ?> tags. They will never run and will cause PHP errors.

Problem 2:
Expand|Select|Wrap|Line Numbers
  1. $sql = mysql_query("select * from table where Name like '%$term%'");
In my experience, its good practice, and sometimes nessecary to encase the names of the SQL tables, and rows within ' ' and ` ` like so...
Expand|Select|Wrap|Line Numbers
  1. $sql = mysql_query("SELECT * FROM `table` WHERE `Name` LIKE '%".$term."%'");
Although I admit I don't think I've seen the "LIKE" term used in SQL before... *shrug*

Suggestion 1:
Personally I always have all the Javascript <script> tags within the <head> tags at the top to declare functions. While it shouldn't be a problem to put them anywhere, its good practice, and makes the code more readable.

Suggestion 2:
Expand|Select|Wrap|Line Numbers
  1. echo "<table border='1'>
  2. <tr id="table">
Since you want to Show and Hide the table, it would be a better idea to litterally hide the whole table but puting the ID in the <table> tag, and hiding that. Generally tables have alot of <tr> tags so you might end up hiding part of the table, and it might look weird after hiding.

Suggestion 3:
Expand|Select|Wrap|Line Numbers
  1. <script type="text/javascript">
  2. function displayRow(){
  3. var row = document.getElementById("table");
  4. if (row.style.display == '') row.style.display = 'none';
  5. else row.style.display = '';
  6. }
  7. </script>
As pointed out above there is no blank setting for the display style. Its either "block" for on, and "none" for off. So, I would personally have the code like this:
Expand|Select|Wrap|Line Numbers
  1. <script type="text/javascript">
  2. function displayRow() {
  3. var dRow = document.getElementById("table").style;
  4. if ( dRow.display == "block" ) { dRow.display == "none"; }
  5. else { dRow.display == "block"; } </script>
Now. I noticed that you have multiple rows. I can suggestion some other code if you would like to hide each row. Good Luck.
Dec 3 '09 #11
Dormilich
8,658 Expert Mod 8TB
As pointed out above there is no blank setting for the display style. Its either "block" for on, and "none" for off.
"block" is not the correct choice for tables, use "table" (as Frinny posted earlier)

**edit**
Further discussion about this can be found in the following thread:
http://bytes.com/topic/html-css/answ...-display-style

Although I admit I don't think I've seen the "LIKE" term used in SQL before... *shrug*
LIKE is a valid SQL operator, nothing wrong there.
Dec 3 '09 #12
Frinavale
9,735 Expert Mod 8TB
thank you. yes i understand that a little bit more. so in my table that i have in my php code should i set the id this way??

Expand|Select|Wrap|Line Numbers
  1. echo "<tableborder ='1'>
  2. echo "TABLE";
  3.  
  4. <tr id ="tableID">
  5. <th>"table"</th>
  6.  
becuase if i do it like above it wont work
I know it's a little hard when you're first learning web development but there are a couple of things that you should be aware of.

First of all, ID attributes of HTML elements are used to identify a particular HTML element. No two HTML elements can have the same ID or else your HTML will be invalid (your web page will still be displayed but it will not be valid HTML).

You can retrieve a reference to an HTML element in your JavaScript code. You can then use JavaScript to work with the HTML element (for example you can change the style of the element to hide or show it).

You use the JavaScript document.getElementById() method to retrieve a reference to the HTML element.

Say you have the following tables (note that each table and each row have a unique ID):
Expand|Select|Wrap|Line Numbers
  1. <table id="tableID">
  2.   <tr id="row1"><td></td></tr>
  3.   <tr id="row2"><td></td></tr>
  4. </table>
  5.  
  6. <table id="2ndTableID">
  7.   <tr id="2ndTableRow1"><td></td></tr>
  8.   <tr id="2ndTableRow2"><td></td></tr>
  9. </table>
  10.  
When you use the document.getElementById("tableID") you will be returned a reference to the the table element that has the ID "tableID".

If you use document.getElementById("2ndTableID") then you will be returned
the table element that has the ID "2ndTableID".

If you use document.getElementById("row1") then you will be returned the table-row element that has the ID "row1"....

I think you get the point.

In your case you want to hide the Table not a Table-Row.
So, grab a reference to the table element...not the row element...

A table element is different from a table-row element. Likewise, a button element is different from a div element.

Each have particular properties that you can work with in JavaScript and each have particular styles that should be used to display them.

To display a table you should use the CSS style display:"table"....to display a table row you should use the CSS style display:"table-row".

Now, regarding PhP.

Actually, let's step back a bit....

HTML is a markup language that lets you describe what content should look like when it is displayed in a web browser. You create HTML elements that contain information. You can retrieve references to these elements in JavaScript and work with them in the web browser (client side)

PhP code is executed on the server.
It is not accessible client side (you probably already know this but a lot of people get confused by this).

You can use PhP code to generate HTML based on whatever you're doing on the server.

In your case, you need to retrieve things from a database. This can't be done client side, so you have to do it in your PhP code.

Now you've figured out how to retrieved the information from your database... you want to display it in your web page.

So what do you do?

You do exactly what you were doing: loop through the rows of data retrieved and generate HTML code that "marks up" how the data should be displayed in the browser.

Because PhP code is executed on the server, it doesn't matter where you put it within your HTML (so long as it's outputting HTML into in a valid place).

So it's perfectly valid to have:
Expand|Select|Wrap|Line Numbers
  1. <html>
  2. <head></head>
  3. <body>
  4.  <?php echo "  <h1>Hello World</h1>" ?>
  5. </body>
It is valid because the php code is executed on the server...the above php code just prints "<h1>Hello World</h1>". Now if you were to take a look at the source for the HTML page in the web browser you will see:
Expand|Select|Wrap|Line Numbers
  1. <html>
  2. <head></head>
  3. <body>
  4.    <h1>Hello World</h1>
  5. </body>
However, if you have:
Expand|Select|Wrap|Line Numbers
  1.  <?php echo "  <h1>Hello World</h1>" ?>
  2. <html>
  3. <head></head>
  4. <body>
  5. </body>
It would generate the following HTML:
Expand|Select|Wrap|Line Numbers
  1.   <h1>Hello World</h1>
  2. <html>
  3. <head></head>
  4. <body>
  5. </body>
Which is not valid because you cannot have an <h1> element above the <html> element...

So, you're doing things correctly.

I know it can be overwhelming when you have so many components to work with (HTML, JavaScript, php)...but they all work together to give you your end result.


-Frinny
Dec 3 '09 #13
Thanks Frinny for that great help. I will see what I can do from here.
Dec 3 '09 #14
Oh just thought of something...
Expand|Select|Wrap|Line Numbers
  1. document.getElementById("table");
  2.  
does this code take multiple parameters so that I could do
Expand|Select|Wrap|Line Numbers
  1. var getelement = document.getElementByid("table1",table2","table3")
  2.  
or will i have to set a new variable for each button i make to close each table. so will i need to do this
Expand|Select|Wrap|Line Numbers
  1. var getelement1=document.getElementByid("table1")
  2. var getelement2=document.getElementByid("table2")
  3. etc....
  4.  
Dec 3 '09 #15
Frinavale
9,735 Expert Mod 8TB
:) there is another method that will retrieve all HTML elements that match a specific HTML tag.

This method is called document.getElementsByTagName.

Or you could pass the ID of the table into the JavaScript function.
In that case your JavaScript function would take a parameter: tableID (which is a string containing the ID of the table that you want to hide or display)

For example your JavaScript method would have to be modified to:
Expand|Select|Wrap|Line Numbers
  1.  function HideDisplayTable(tableID){
  2.   var tableElement= document.getElementById(tableID);
  3.   //.......
And your buttons would look like:
Expand|Select|Wrap|Line Numbers
  1. <button onclick="displayRow('table1');" >Show table1</button>
  2. <button onclick="displayRow('table2');" >Show table2</button>
The thing is that you're going to have to figure out whether or not to display the particular table that the function is managing (just check if it's display style is 'none'...if so change it to 'table'...if not change it to 'none').

Expand|Select|Wrap|Line Numbers
  1.  function HideDisplayTable(tableID){
  2.   var tableElement= document.getElementById(tableID);
  3.   if(tableElement)
  4.   { var hideTable = true;
  5.     if(tableElement.style.display == 'none')
  6.     { hideTable = false;}
  7.   //.................
:)

-Frinny
Dec 3 '09 #16
Markus
6,050 Expert 4TB
@yeshello54
The second way. If in doubt, check the documentation (MSDN, MDC - take your pick).
Dec 3 '09 #17

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.