Using Javascript to change style of elements in PHP - javascript

I have a problem with javascript in PHP. I want to use Javascript function to change ccs atribute display to block for only those div elements that contain certain text that is in php variable which is taken from column in MySQL table. I don't know if it is actually possible this way but here's the code:
PHP:
$sql="SELECT * FROM Filter1";
$result=$conn->query($sql);
while($row = $result->fetch_assoc()) {
$meno=$row['meno'];
echo '<script type="text/javascript"> filterTovar() </script>';
JavaScript:
function filterTovar()
{
var meno='<?php echo $meno; ?>';
var produkt=document.getElementsByClassName("tovar");
produkt.style.display="none";
for(var i=0;i<produkt.length;i++)
{
produkt[i].style.display="none";
}
for(var i=0;i<produkt.length;i++)
{
if(produkt[i].innerHTML==meno)
{
produkt[i].style.display="block";
}
}
}
I am trying to make some kind of filter that makes only certain divs shows depending on result of table but the code above doesn't work and I'm not sure if it is possible to make work this way.
MySQL table I'm taking data from:
id meno znacka cena op format
425 H81M-S2V GIGABYTE 46.03 DDR3 mATX
426 H110N GIGABYTE 83.05 DDR4 mITX
EDIT: Better explanation of my problem: User uses checkboxes to filter products on my website, in database php creates table with corresponding products. My code above is supposed to take that table and use column "meno" in which is name of product to show only those div that contain string "meno".
EDIT2: I want to ask if it is possible to change or work with elements css via DOMDocument(). If yes it would answer my question.

Yes, it is possible but there is no need to use javascript/jQuery - just use HTML and CSS.
Basically, you have your database result set and you loop through it with a while loop, right?
I guess you are constructing HTML in that while loop.
In each iteration of the loop, test your MySQL column value and add a class to the desired HTML element.
Here is an example:
$out = '<table><tbody>';
while($row = $result->fetch_assoc()) {
$theclass = ($row['something']=='hideme') ? ' hiddenTD' : '';
$out .= '<tr><td class="' .$theclass. '">' .$row['something']. '</td></tr>'
}
$out = '</tbody></table>';
echo $out;
Of course, you would have CSS defined in the document, like:
<style>
.hiddenTD{display:none;}
</style>
This is a terrible example because you can't "hide" a TD, but it's an example.

Related

How to add php variable/function into a javascript function [duplicate]

This question already has answers here:
What is the difference between client-side and server-side programming?
(3 answers)
Closed 3 years ago.
I cant seem to add a php function / variable into my javascript. I need it to set options into a dynamic select form I have in javascript.
I tried "" inside the javascript which everyone recommended.
<?php
function options(){
include "connection.php";
$sql="SELECT DISTINCT PartID, PartName FROM parts";
$result = mysqli_query($conn,$sql);
if ($result) {
while ($row=mysqli_fetch_assoc($result)){
echo "<option value='{$row['PartID']}'>{$row['PartName']}</option>";
}}}
?>
<script>
$(function(){
$('p#add_part').click(function(){
counter_part += 1;
$('#container').append(
'<strong>Part No.'+ counter_part + '</strong><br />');
$('#container').append(
'<select><option>Select a Part</option>((((((HERE)))))
</select>');
});
});
</script>
The whole function does not work in relation to the methods I keep trying. When ever I add a '' inside the script to add a variable inside the option, the whole function stops working. However its possible to add a counter variable. Btw I have some other functions above the script etc that was irrelevant.
You can try storing it in an HTML element in PHP, then you can read it from JavaScript and then delete it from JavaScript. You can also set it at the bottom of the page and set the text color the same as the background color so you can't see it. I don't know much about PHP, but I do for JavaScript and HTML, so, sorry but I can only add the JavaScript and HTML.
HTML:
<span style="background-color:white; color:white;" id="phpVariable"></span>
JavaScript:
var element = document.getElementById("phpVariable"); // Get the element
var phpString = element.textContent; // Get a string
var phpInteger = parseInt(element.textContent) // Get an integer
var phpFloat = parseFloat(element.textContent) // Get a float
element.parentNode.removeChild(element); // Delete the element
For the PHP, you just have to append the HTML element above to the bottom of the page. If you're background is a different color, then you can just set both the background color and the text color to the background color of the page. Doing this will make the text invisible by just looking at it until the JavaScript picks it up and deletes the element.
Just echo your JavaScript function with PHP, and you can easily insert function variables with string concat. In example echo 'alert("'.$var.'"); or something like this!
I'm not sure that is what you want, but you can use PHP values of variables in your JS with simple echo to text. I just edited your to work. Declaring options into $options variable and return it in function is giving possibility to write them into JS with echo. You could do it with $options directly, without options() function.
PHP code works only once, then your echo values counted as hardcoded text in html file. Use this only if you need to get value once.
<?php
function options(){
include "connection.php";
$sql="SELECT DISTINCT PartID, PartName FROM parts";
$result = mysqli_query($conn,$sql);
if ($result) {
$options = '';
while ($row=mysqli_fetch_assoc($result)){
$options .= "<option value='".$row['PartID']."'>".$row['PartName']."</option>";
}
}
return $options;
}
?>
<script>
$(function(){
$('p#add_part').click(function(){
counter_part += 1;
$('#container').append(
'<strong>Part No.'+ counter_part + '</strong><br />');
$('#container').append(
"<select><option>Select a Part</option><?php echo options() ?></select>");
});
});
</script>

PHP altering HTML on first connection

Currently I have a php-page using include to show an html-page.
What I want to do is use a variable the php-page received when being called and project data into a field of the html-page depending on this variable. The problem is that I can't seem to pass the variable from php to html without using html to call php.
I know that I can simply echo the php-part, but because I can't echo straight into one of the html-page's divs it ends up putting the echo at the beginning or end of the page.
What I want to accomplish is for the html-page to load the php-part into one of the div's the first time the php includes the html-page. Is there any way to do such a thing?
If this isn't possible, then how do I get the javascript in the html-page to use the variable from the php-file? Is the best option really the href?
As requested some of the code:
php:
if (filter_input(INPUT_GET, "event", FILTER_SANITIZE_STRING))
{
include "../Html/EventPage.html"; //target page
$temp = filter_input(INPUT_GET, "event", FILTER_SANITIZE_STRING); //get the variable
include "../PHP/Connector.php"; //include the connection page
$value = callFunction(Connect(), $temp); //call connection page and return value
echo "<div class = 'cant select div in html'>" . $value . "</div>"; //echo value in location
}
html:
<div id = "dynamic">
this should have the php
</div>
I assume you use some code like this to display the page:
include('page.html');
A safer way would be to prevent evaluation in page.html:
echo file_get_contents('page.html', true);
You can then do any string modification you want for the content:
$replacement = 'MY REPLACEMENT';
echo str_replace('<div id="my-div">', '<div id="my-div">' . $replacement, file_get_contents('page.html', true));

PHP Simple HTML DOM Parser not displaying alt tag as text

<?
include('simple_html_dom.php');
// Create DOM from URL or file
$html = file_get_html('http://www.saie.bolognafiere.it/it/partner/media-partner');
// Find all images
foreach($html->getAttribute('ul#slider356 alt') as $element)
echo $element->plaintext . '<br>';
?>
Actually I would like that all the alt tag content contained in the " ul id="slider356 " are displayed as text but my code is not working can someone help me ?
Thank you
To complete Sean's comment, there is two ways to get the attribute's value:
$node->attribute;
$node->getAttribute('attribute');
So, in your case, try this:
$html = file_get_html('http://www.saie.bolognafiere.it/it/partner/media-partner');
// Find all images inside ul#slider356
foreach($html->find('ul#slider356 img') as $element)
// Print alt value
echo $element->alt . "<br/>";
// $element->getAttribute('alt') should work as well
Working code
Please check the Manual for more info...

how to scroll an array of text?

I am currently working on a web site for a boy scout troop and I have a database of young men that have earned there eagle scout. I was wanting that data to scroll across a text widget in WordPress. I currently have the plugins to make it work I'm just struggling with the code side of it. I have some php code to send the database to my array. My current issue is getting a dynamic array from my php to my js function and then making it scroll from right to left. what would be the best way to go about this?
.php
$Eagle = array();
while($row = mysqli_fetch_array($result))
{
$Eagle[] = "#".$row[numb]." ".$row['FName']." ".$row['LName']." ".$row['Date'];
}
Concatenate your $Eagle array to a string using implode() (php docs) and put it into an <marquee> element
echo '<marquee behaviour="scroll">' . implode(' | ', $Eagle) . '</marquee>';
for more options, have a look here or here
EDIT
an alternative way of doing what you're trying to accomplisch:
echo '<marquee behaviour="scroll">';
while ($row = mysqli_fetch_array($result))
echo "#{$row[$numb]} {$row['FName']} {$row['LName']} {$row['Date']}";
echo '</marquee>';

How would I create a news system?

I have a database where I can create a news article through PHPMyAdmin with the fields: "Name of the article", "Content", "Writer".
I have already made a "latest news" system, which looks like this:
$result = mysql_query("SELECT * FROM news LIMIT 5");
if(mysql_num_rows($result) > 0)
{
while($row = mysql_fetch_array($result))
{
echo $row['Namn'];
echo " av ";
echo $row['Skribent'];
echo "<br />";
}
}
mysql_close($con);
?>
And that is the latest news system (skribent means = writer), (namn means = name (of the article), (av means = by),.
So, now I have two problems. Once these news are displayed, I want them to be clickable and once you click it, you get to the original page where the article is located, how do I do it?
And, how do I do so that when I insert a query it will automatically create a nice page for every article, like most famous sites have. When they create a news it comes like http://google.com/news/how-to-be-a-dork/
Firstly to link articles to a different page you could use the element's onclick method, and use javascript to redirect the user, or you could add <a> tag around the article element.
Secondly, if you would like pretty urls you will have to read up on url re-writing, I unfortunately don't know much about it.

Categories

Resources