Hide/Show HTML content using PHP - php

I am very new to PHP and I am trying to get a piece of PHP code to run inside of my HTML file.
I have a drop down menu. If users select one item, it should display additional fields. So, I want them to only display if they select that item from the drop down menu. I am trying to select it based on the value for that drop down item. I have not declared any PHP values in a PHP script. This is all in HTML.
I know that with jquery you have to pull in the jquery library before running the script. Do I need to do this with PHP also?
Here is the code that I am trying to run:
Thank you in advance!
html file
<?php
if ($dropmenuValue == specificDropDownOption) {
?>
<div>
-conditional content-
</div>
<?php
}
?>

One of the main things about PHP and other server-side languages is that once they render the page, they shut down, and that's it. There's nothing they can do after.
You need to resort to a client-side here, probably the simplest way being adding the values of PHP variables to the appropriate JavaScript variables and then taking it from there. You would also need to render all possible contents and only show what you need.
So, in your case, you could make a CSS class to denote hidden content and then use JavaScript to hide/show different parts of the markup. Please note that all the hidden code (rendered by PHP, but hidden by CSS) is still visible in the page source, so if you have anything sensitive in there you should definitely do it by either making AJAX calls to load partial content, or regular page redirection/navigation.
EDIT
Here is a super-simple example I made for you, to see how you can show/hide content:
HTML (parts can be rendered by PHP, of course)
<div id="content1" class="content">Hey</div>
<div id="content2" class="content hidden">There</div>
<div id="content3" class="content hidden">World</div>
<hr />
<button onclick="show(1)">Show 1</button>
<button onclick="show(2)">Show 2</button>
<button onclick="show(3)">Show 3</button>
JS
function show(id) {
// select all the content divs
var allDivs = document.getElementsByClassName('content');
// iterate over them and add a hidden class to each
for (var i = 0; i < allDivs.length; i++){
allDivs[i].classList.add('hidden');
}
// finally, remove the hidden class from the one we referenced
document.getElementById('content' + id).classList.remove('hidden');
}
See it in action here: http://jsfiddle.net/f0onk7bk/

You can try something out with this... While using HTML inside of the PHP
#item { display: block;}
#itme.active { display:none }

Something like this would work when the page was loaded/reloaded...
<?php
if(isset($_POST['dropdown_name_here']) && $_POST['dropdown_name_here'] == "specificParameter")
{
echo("<div id='condition_one_div'> ... </div>");
}
else
{
echo("<div id='condition_two_div'> ... </div>");
}
?>
... but JavaScript is what you would want to use for dynamic content I would think.
var div = document.getElementById('div_name_here');
var dropdown = document.getElementById('dropdown_name_here');
if(dropdown.value == "specificParameter")
{
//add/show content for div here
}
else
{
//hide content for div here
}
is this what you are looking for?
<select id='dropdown_name' onchange='if(this.value == "parameter"){ document.getElementById("div_name").style.display = "block"; }else{ document.getElementById("div_name").style.display = "none"; }' >
</select>

Related

how do i include #include('layout.example') in Jquery code?

So if i have a small layout stored in example.blade.php, How do i use it in jquery ?
I want to include certain elements such as textboxes when a particular radio button is checked otherwise not.
Example:
$("document").ready(function(){
if ($("#role").prop( "checked")) {
$("#content").html(#include('layouts.nav'));
}
else
{
$("#content").html('');
}
});
The above code does not work so please provide some solution.
You can not do that. #include() is Blade and will be interpreted by PHP while JS runs in the browser. The Blade directives are already interpreted by PHP when the code hits the browser
You can create a hidden div in your view and then use jquery to get it's html content, here is an example:
<div class="layouts-nav" style="display:none;">
#include('layouts.nav')
</div>
<script>
$("document").ready(function(){
if ($("#role").prop( "checked")) {
var layoutNav = $('#layouts-nav').html();
$("#content").html(layoutNav);
}
else
{
$("#content").html('');
}
});
<script>

Get current HTML element in PHP

I may have misunderstood the purpose of PHP here. But I want to do the following:
I have a PHP function, and I call it from HTML, e.g.
<BODY>
<DIV id='A'>
<?php emit("hello1"); ?>
</DIV>
<DIV id='B'>
<?php emit("hello2"); ?>
</DIV>
</BODY>
And I want to know, within the function, which DIV it was called from.
e.g.
<?php function emit($txt){
echo "$txt";
echo "from DIV id $DIVID"
}
?>
And I want it, obviously, to print
hello1 from DIV id A
hello2 from DIV id B
Is there any way of finding the "current" DIV's ID?
Yes, you have misunderstood the purpose of PHP.
PHP is a server side programming language, it does not run on the HTML page, but before the HTML gets loaded on to the browser.
The task that you are trying to do can be done from JavaScript if interested. I will give an example of jQuery:
var emit = function(el, txt) {
var id = el.attr('id');
el.html(txt+" from DIV id "+id);
}
Now call using
emit($("#a"), "hello1");
Same can be done from JS in the following way
var emit = function(el, txt) {
el = document.getElementById("el");
id = el.getAttribute('id');
el.innerHTML(txt+" from DIV id "+id);
};
Use like:
emit("a", "hello1");

Show Hide Div Dependent on Get ['status']

Hopefully someone will know how to do this...
I would like the show/hide to work dependent on the the image/icon clicked.
For example if Request Icon is clicked, the colorbox popup will be displaying the Request and Offered divs.
Whereas if the Accepted Icon is clicked, the colorbox popup will display Accepted and Offered Divs but hides the Request div.
Here's a few in the PHP:
<div <?php if($_GET['status']=="Requested"){ echo "class=iconActive";}else {echo "class=iconInactive";}?>>
<a href="/index.php/agent-appointment-request-list/?status=Requested">
<img src="components/com_agent/images/requestedIcon_s1.png" id="requestedIcon" alt="" /></a>
<div class="iconTitle">Requested</div>
</div>
Here's the jquery for show/hide that I have:
$(".slidingDivReq").show();
$("#show_hideReq").show();
$('#show_hideReq').click(function(){
$(".slidingDivReq").slideToggle();
});
Update: Here's the section for the content that'll be show/hidden. :
<div class="AppointmentTitleFirst">
<img src="components/com_agent/images/req_appt_title_s1.png" class="ApptIcon"></img>
<h1>Requested Appointment # <?php echo $this->appointment->id;?> Status:<?php echo $this->appointment->status_name;?></h1>
<img src="components/com_agent/images/dropdown_arrow_s1.png" class="ApptDropDownArrow1" id="show_hideReq"></img>
</div>
<div class="clearFloat"></div>
<div id="requestedAppointmentContent" class="slidingDivReq">
Content here....
</div>
Pretty much, I'm not sure how to use the GET['status'] and use it with the show/hide. So if the GET['status'] == Requested, then the show_hideReq will be .show(), but my other divs in the colorbox will use .hide(); (unless it's a page that needs a few other divs to .show())
Hopefully someone will know.
I'm not sure how to use the GET['status'] and use it with the
show/hide
You can use a hidden element as a variable, sothat you can test your GET['status'] in jquery. Add this element in your php page:
<input type="hidden" id="StatusId" value="<?php echo GET['status']; ?>"/>
Then you can do this:
$('#div').click(function(){
var statusId = $("#StatusId").val();
If( statusId === 0){
//Do something
}else{
// Do something elese
}
});
You should send the status value in the url that redirect to this php page, for example from js file it would look like:
var url = "http://localhost/yourcurrentphpfile.php?Status=" + somevalue;
window.location.href = url;
or the clicked <a> tag in your php page that redirect to this page should has the href property looks like http://localhost/yourcurrentphpfile.php?Status=somevalue.
yourcurrentphpfile.php is the php page that containst the hidden input.
You'd better use $(element).css("dissplay","none"); to hide an element and $(element).css("dissplay","inherit"); to show it.
Also in your PHP you better use if (isset($_GET['status']) && !empty($_GET['status'])) to ensure you have a value. Also, you need to edit your ECHO instructions to:
echo 'class = "iconActive"';
and
echo 'class = "iconInactive"';
Following your code's logic,
$(function(){ //on page load
$("div.classActive").load(function(){
//Do whatever you want when the div is active such as show/hide elements
})
$("div.classInactive").load(function(){
//Do whatever you want when the div is inactive such as show/hide elements
})
})
Only of the 2 will be executed since when a page renders only one of them will exist.

Using jQuery with php with IF logic

I have some php conditional logic...basically I want to use jQuery to show or hide certain div's when I submit a form:
<?php
if (isset($_POST['submit']) {
/*
If the user submits the form and
use jQuery to hide an existing div tag
<script type="text/javascript">
$('#mydiv').hide();
</script>
*/
}
?>
Is there a sort-of non-messy way to go about mixing php and jQuery like this? I just want my jQuery to be in one block and my php to be in a separate block (i.e. not interspersed)
You can catch the "onClick" event with ajax and then do what you want with your div like :
in your HTML page :
<button type="button" onclick= "yourFunction()">Click Me!</button
and then in a separate javascript file:
yourfunction() {
$("div").hide();
}
EDIT : You don't need PHP if you only want to hide a div when the form is submitted
Well if you are simply trying to hide something after the form has been processed successfully then putting your JavaScript code inside that if block is as good place as any other. You would just need to take care that the DOM tree is actually ready before you try to hide an element.
if (isset($_POST['submit']) {
echo <<<JS
<script type="text/javascript">
$(document).ready(function(){
$('#mydiv').hide();
});
</script>
JS;
}
However if that DIV you are trying to hide contains important information which isn't supposed to be seen in case form processing fails you should "hide" your container on the server side.
$status = false; // Status of the form processing
if (isset($_POST['submit']) {
// If processing succeeds you set $status to true
}
<?php if($status){ ?>
<div id="myDiv">
// Your super secret ingredient formula!
</div>
<?php } ?>

Need help with controlling a page dynamically

I'm looking to make a page wherein a user will make a selection for eg: "How old is your computer?" - "One year", "Two years" etc etc and the page will remove and add 'options' (which at the moment only need to be informative sections of text)
Is there any way to do something like that?
The technologies I'm using are PHP and of course, HTML and CSS to style the pages.
Thanks in advance!
PHP cannot detect if changes has been made to the page, you will have to use Javascript for that.
If you are using a HTML select element and you want to create the content dynamically it could look something like this:
HTML
<select id="computerAge">
<option>2 years</option>
<option>3 years</option>
</select>
<div id="dynamicContent">
</div>
Javascript
var computerAge = document.getElementById('computerAge');
var dynamicContent = document.getElementById('dynamicContent');
computerAge.onchange = function()
{
// Get a list of the current dynamic content
var contents = document.getElementById('content').document.getElementsByTagname('p');
// Remove current dynamic content
for(i = 0; i < contents.length; i++)
{
dynamicContent.removeChild(contents[i]);
}
// Create new dynamic content
var dynamicP = document.createElement('p');
if(computerAge.value == '2 years')
{
dynamicP.innerHTML = 'Content for 2 year old computer';
dynamicContent.appendChild(dynamicP);
}
elseif(computerAge.value == '3 years')
{
dynamicP.innerHTML = 'Content for 3 year old computer';
dyanmicContent.appendChild(dynamicP);
}
}
Or if you're not creating elements dynamically:
HTML
<select id="computerAge">
<option>2_years</option>
<option>3_years</option>
</select>
<div id="content">
<div id="2_years" style="display: none">
<p>Content for 2 year old computer</p>
</div>
<div id="3_years" style="display: none">
<p>Content for 3 year old computer</p>
</div>
</div>
Javascript
function init()
{
// Get the select element
var select = document.getElementById('computerAge');
select.onchange = function()
{
// Get the elements which could be the dynamic content
var content = document.getElementById('content')
var contents = content.getElementsByTagName('div');
// Iterate over contents; make selected visible and hide others
for(i = 0; i < contents.length; i++)
{
if(contents[i].id == select.value)
{
contents[i].style.display = 'block';
}
else
{
contents[i].style.display = 'none';
}
}
}
}
window.onload = init;
I think you're mixing some stuff together. From what I understand is you want to show or hide portions of the page depending on what the user selected in a dropdown.
PHP makes "dynamic webpages" that's true. But what's ment by this is that the page can serve different content on each request. But once the content is served it's rendered on the client side and not in PHP hands anymore.
If you want to change the content without reloading the whole page you should use javascript.
You could also server new content using ajax, but I think you just want to put some div's display propertie to none or something like that.

Categories