I already checked all others answers from that questions, but no one seems to work.
He's my problem, I try to reload a div where I got a php variable.
I got MVC system, so i can't call other files when i want.
My controller doesn't receive arguments from ajax call ($_POST and $_FILES are not isset).
When I try to do other stuff after ajax call, it works (example: unlink a file)
<?php
$test = 'lksd';
...
if( some conditions )
{
$test= 'piof';
unlink(../images/myImage.jpg);
}
?>
...
<div id="test">
<?php
echo $test;
?>
</div>
And my ajax call is in a jquery plugin (http://www.dropzonejs.com/)
I found where i can do my stuff, and then i call this function:
$('#test').load(window.location + "#test");
My div doesn't refresh but my file is deleted, if you have any idea of what can i do to solve this problem...
Thanks in advance
Create the PHP detail you want to stand alone. Call it something like getDetail.php:
<?php
$test = 'lksd';
...
if( some conditions ) {
$test= 'piof';
}
echo $test;
?>
In your page, you can now load that content at any time:
<html>
<body>
<div id="test">
</div>
Reload
<script>
$(document).ready(function(){
$("#test").load("getDetal.php");
$("#reloadDetail").click(function(){
$("#test").load("getDetail.php");
});
});
</script>
</body>
</html>
Related
I was asked a question by a friend and I honestly for the life of my could not come up with a sure answer. So I pose it to you instead;
If I have 2 documents (page.html and form.php), and I have included form.php in my page.html as so:
<html>
<head>
<title>Home</title>
<?php include 'form.php'; ?>
</head>
<body>
<form>
<FORM CONTENT HERE>
</form>
<div class="results">
<p><?php echo $result_variable ?></p>
</div>
</body>
<script>
!-- Ajax function here, POSTs to form.php
</script>
</html>
Which has an AJAX form, POSTing to form.php, and a field for results, that prints the PHP variable $result_variable.
Now if My PHP page takes the posted data, after the initial document load / include has been completed - then changes the value of $result_variable as a result - will the value of $result_variable be updated within the page.html without the page reloading?
For example - if form.php looked something like this;
<?php
$result_variable = 1;
if (!empty($_POST)){
$result_variable = 2;
}
?>
After the initial pageload (without form submission), $result_variable should yeild 1. Though after the ajax form submission has been completed, the $result_variable has been changed to 2. Will this reflect without reloading page.html?
Thanks.
Nope. since $result_variable is not part of the current page that includes form.php.
It will only work if you populate it using javascript.
Let's say:
ajax targets form.php, so this is what form.php should look like:
<?php
$result_variable = 1;
if(!empty($_POST)){
$result_variable = 2;
}
// print the result as an ajax response (the format here is not JSON)
echo $result_variable;
// if the response format is JSON
echo json_encode(array("result_key_name" => $result_variable));
?>
Here's an example using jQuery $.post http://api.jquery.com/jquery.post/
// on a normal php/html page
$.post("form.php", form_data, function(data){
// data is the result echoed using json_encode() from form.php
alert(data.result_key_name); // as per the server-side example above
}, "json");
Hello can anyone please help me in fixing this code?
1st code
<script type="text/javascript">
if (document.getElementById("tester") != undefined)
{
document.write('**2nd code should be here**');
}
else
{
document.write('<img scr="./img.png" />');
}
</script>
2nd code
<?php while (have_posts()) : the_post(); get_template_part('item-video');
endwhile; ?>
What I want to happen is to insert the 2nd code into the first code. But when I try to insert it, it gives me a blank page.
the biggest thing I've heard is that PHP is server side, while Javascript is client side. Therefore, changing the html page using document.write() isn't going to do anything unless you reload the page. I think you can circumvent this problem tho, using Ajax and the load() function.
Situation:
I'm building a Wordpress Site. Menu becomes 'fixed' to the top of the page when scrolling down the page (http://deerfielddesigns.com.mlseo.net/). When i am logged into wordpress, the dashboard admin bar cover the menu. I wanted to create a different class for the menu when I am logged in as opposed to when I am logged out.
Code:
if ( direction === 'down' ) {
$('#main-header').addClass( 'et-fixed-header' );
} else {
$('#main-header').removeClass( 'et-fixed-header' );
}
I wanted to insert an "if ( is_user_logged_in() )" statement in there to change the class output but I don't really understand too much about javascript and if it plays nice with php. Does anyone have any insight as to what i need to do to make this work? Thanks all!
if this is not an AJAX driven login system, simply open your header.php file and perform the following
$the_class = is_user_logged_in() ? 'logged-in-class' : 'logged-out-class';
Then find your main-header and do what's required.
<header id="main-header" class="<?php echo $the_class;">
If it is an ajax driven login system, you'll need to understand how to work with wordpress' add_action, but that is an answer for an entirely different question.
Make an ajax request to the required php function whenever required. You may use ajax features of various JS libraries like jQuery, YUI etc. to do so.
PHP is a server side language (which quite likely is generating Javascript).
Javascript is in the browser.
The short answer is "You can't easily call PHP from JS".
The simple way to identify from you javascript if a user is logged in is to populate a javascript variable with their status. In your theme's header.php, add:
<script type="text/javascript">
var is_logged_in = <?php echo json_encode(is_user_logged_in()) ?>;
</script>
Then, in your javascript elsewhere, you can use:
if (is_logged_in) {
//....
}
If you write inline javascript in your php files you can do this:
<?php // some php code ?>
<script type="text/javascript">
<?php if ( is_user_logged_in() ) { ?>
// Place some javascript here
<?php } else { ?>
// Place some javascript here
<?php ?>
</script>
<?php // some php code ?>
I'm using a jquery alert library named Apprise2, it works fine when im using it for alerts in my form validation function. all i want is to show alert on page load, i call it at the end of the page, but it dont work, this is what i use at the end of page :
<?php if ($_SESSION['lasterror'] == '1') {?>
<script>
Apprise('error-occured');
</script>
<?php } ?>
when im using "alert" instead of "apprise" it works and shows the alert, i linked to the library bellow and i hope you can help me with this problem. thanks and sorry for my english.
You can try to do this in doc ready handler:
<?php if ($_SESSION['lasterror'] == '1') {?>
<script>
$(function(){
Apprise('error-occured');
});
</script>
<?php } ?>
Point 2: this might be the issue:
You are testing a string which might be a number here:
<?php if ($_SESSION['lasterror'] == '1') {?>
//-----------------------------------^-------here
I am trying to make an on-line user content which refresh it self and calls every time php function.
I used
<div class="onlineFriends">
<?php
$members = find_online_friends($_SESSION['id']);
foreach($members as $member):?>
<div class="user" href=<?php echo ($member['f_id']); ?> rel="popup_name" >
<img src=<?php
$avatars = find_avatar($member['f_id']);
foreach($avatars as $avatar)
echo ($avatar['src']) ?>
/>
</div>
<?php endforeach; ?>
</div>
<script>
$(function(){
var refreshId = setInterval(function() {
$('.onlineFriends ').load("# .onlineFriends ").fadeOut("slow", function () {
$(this).fadeIn("slow");
});
}, 50000);
});
</script>
This works good. But .load() function I think loads first all entire page and then calls .onlineFriends. I can see it with firebug on console. it returns all page source code as GET answer. My question is it will make slow down ? Because I will use this method 5 times for other div contents and each time each functions will load full page.
Also I tried to create separate .php file but I have in php code some dependences and I cant run this function in another file.
Any request to your page will result to it's full code. You can specify in php to send back only wanted part of page when specific GET or POST data is present and use $.get() or $.post() to get them.
Because I will use this method 5 times
On the same page? Then it's better to replace them from the one of $.get() and $.post() data during callback function.
Create other page friends.php
and in page index use cod java/ajax and use jquery-min
<script src="js/jquery-1.10.1.min.js"></script>
$(document).ready(function() {
$("#center").load("friends.php?var=<?php echo $member['f_id']; ?>");
var refreshId = setInterval(function() {
$("#center").load('friends.php?var=<?php echo $member['f_id']; ?>');}, 9000);
$.ajaxSetup({ cache: false });
your div html use
<div class="center"></div>
your friends.php use
<?php include"your bd here" $jab = $_GET['var'];
'select bd'
echo $resultyourneed['f_id'] ?>