Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
I just messed up with is a problem from the last couple of hours and still didn't get the solution i m new in php.what I m trying to do is display and hide the dive according to php condition.
here is the scenario:
note: by default DIV(display:none)
if(condition is satisfied ) //condition in PHP only
//call the javascript function which displays the div
else
//call the javascript function which hides the div
if you have the solution with example please must share it's a humble request
thank you
You want to mix Javascript, HTML and CSS as little as possible. It might look easier to mix it right now, but that will change as you get more experience.
There are multiple ways, but one I prefer is running PHP and passing the variables to a JS variable and let JS decide:
const showSpecificElement = <?php echo funtionWhichDoesSomething(); ?>;
if( showSpecificElement ){
// Javascript magic here
}
// Or, alternatively:
if(<?php echo funtionWhichDoesSomething(); ?>){
// Javascript magic here
}
// Or, slightly better:
$classWhichHidesThisDivOrNot = rand(1,2)==1 ? 'hideMe' : 'showMe';
<div class="<?=$classWhichHidesThisDivOrNot?>"> ... </div>
Related
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 years ago.
Improve this question
how can I specifically convert this php below code to html? I want to do this as I want to be able to run this code on my website.
From reading other forum posts I understand about people saying them link the PHP file externally using something like this php. However, from my understanding my PHP code is slightly different in terms of the output and I'm just not sure.
Below is my php code I want to run externally on my site from the html file.
<?php
echo $_SERVER['HTTP_USER_AGENT'];
$browser = get_browser();
print_r($browser);
?>
Can you use Ajax and show the result in HTML
<script>
$.ajax({
urL:"./tomy.php",
type:"POST",
data:{getuserAgent:1},
success:function(data){
$("#mydiv").html(data);
}
});
</script>
tomy.php
<?php
if(isset($_POST['getuserAgent']){
echo $_SERVER['HTTP_USER_AGENT'];
$browser = get_browser();
print_r($browser);
}
?>
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
I have a table with add, edit, delete options. Now I want to hide divs with buttons for not logging users.
Or maybe I should make a deactivated buttons for not loggin users?
when user login then you have session like
$_SESSION['email']=$email;
then used this code
<?php
if(isset($_SESSION['email']))
{
echo "<div>any text</div>";
}
?>
do some effort to write the code
Depending on how your site works there are two possible solutions that I can think of.
USE PHP
If the user is logged in then use:
<?php
if ($logged_in) {
?>
<div>Your text here</div>
<?php
}
?>
NOTE: You do not need to use "echo" or "print" if you close and open your PHP tags between the "if" as shown below. This can make the HTML part of the much easier to read and write.
USE JAVASCRIPT
If the page does not reload when the user logs in then you will need to use Javascript to hide and show the DIV using something like:
var x = document.getElementById("myDIV");
if (x.style.display === "none") {
x.style.display = "block";
} else {
x.style.display = "none";
}
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
I want to switch a task of jQuery to PHP.
Earlier it was like :
myString += "Hello There ! ";
$("#latestRoutes").prepend(myString).slideDown(2000);
But now As I am switching to PHP, How can I do the $.prepend function in PHP to append a variable to a particular string ?
On page load if the variable isset then show it as a first child of your div like this:
<div>
<?php if(isset($mystring)) echo '<p class="test">'.$mystring.'</p>'; ?>
<p>...</p>
<p>...</p>
</div>
but since this is php is served only on page refresh and of course you cannot have fancy js animations at once, some extra work needs to be done. If you want this to communicate with js add a class there and with js check its existence and then start its animations. This can be done like:
if($('.test').length > 0) {
alert("This message is brought by php, great!!!");
$('.test').on('click', function(){
$(this).hide();
});
}
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
I am trying to pass JS variable to php echo statement.
here is my code
<?php print 'test'; ?>
how can I do that
I just test this and it works but how can I write the value within the href
alert(myvalue); ";
This is impossible from a temporal point of view.
When the PHP code is run, there is no JavaScript. The page is not yet loaded into the browser. There is no JavaScript engine being run. There are no JavaScript variables. By the time the browser renders the page, the server code has already done its job.
With that said, you can render HTML which refers to a JavaScript function. For example:
<?php print 'test'; ?>
Then, implement DoNav accordingly:
function DoNav(url)
{
location.href = url + '?id=' + my_JS_value; // Navigate to the new URL with the JavaScript variable
}
JavaScript and PHP cannot directly communicate as JavaScript is client-side and PHP is server-side, i.e. PHP is executed before the JavaScript is sent to the browser.
The only way to achieve what you want is to sent a request from JavaScript that calls a PHP script (e.g. AJAX) and passes the variable via GET (like your example) or POST.
Javascript code is executed on the client side, so you do not have access to it.
You can just create the link using Javascript, like this:
var my_JS_value = 0;
document.write('test');
You can insert the element with php and set the href attribute later with JS. Anyway there's a ton of other ways to achieve the same.
<?php
print '<a id="mylinkelement" >test</a>
<script>
var mylinkelement=getElementById("mylinkelement");
mylinkelement.href="thisTest.html?id="+my_JS_value;
</script>';
?>
You don't even need php for that :D
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I want to display about 20 li on a page with a link at the bottom that says something like 'display more'. This link will then clear the first 20 and display the next 20. How should i go about doing this. (I am creating a gallery)
I was thinking about using PHP and MySQL. However for what I am doing I do not really need to store it in a database so is there an easier way of doing it only using html, php or javascript?
thanks
Try this
$list_images=glob('imagesPath'.'*.{jpg|jpeg|png|gif}', GLOB_BRACE);
if(isset($_REQUEST['page_id']))
{
$pageNo=$_REQUEST['page_id'];
}
if($pageNo =='')
$offset=0;
else
$offset=$pageNo;
for($i=$offset; $i<($offset+20); $i++)
{
echo"<a href=''><img src='$list_images[$i]' /> </a>";
}
// Store the page no in a hidden variable
<input type='submit' value='More' onclick ='pageSet()' />
//Javascript
function pageSet()
{
var id=document.getElementById('page_id').value;
if(id =='')
{
document.getElementById('page_id').value =1;
}
else
{
document.getElementById('page_id').value =parseInt(id)+1;
}
}
As some people said in their comments the subject is too general. But here is the general answer.
You can do this with javascript. You can create a javascript array that contains all the records that you need to display in your list. If the 'display more' button is clicked you can trigger a javascript function that clears the page and writes another set of items from your list. You just have to maintain the pointers to beginning and end of the current list.
Here is the plugin that you can use.
http://www.geckonewmedia.com/blog/2009/8/20/simplepager---jquery-paging-plugin--updated
But let me know if you need any custom code
Here is a simple solution, doing what you describe, without any javascript and DB, just store your files to disk:
<?php
$array = file('file.txt');
$from = isset($_GET['from'])?$_GET['from']:0;
echo '<pre>';
for($i=($from+0);$i<($from+20) && $i<count($array);$i++) {
echo '#'.$i.' '.$array[$i];
}
echo '</pre>MORE<br />';