Two identical .load() don't work together - javascript

I have these two scripts.
First:
$(document).ready(function(){
refreshStatus();
});
function refreshStatus(){
setTimeout(function(){
$('#div_top_info').load('load_status.php');
}, 2000);
}
Second:
var skill = <?php echo json_encode($skill_, JSON_UNESCAPED_UNICODE); ?>;
var skill_final = <?php echo json_encode($skill_final, JSON_UNESCAPED_UNICODE); ?>;
var camp_final = <?php echo json_encode($camp_final, JSON_UNESCAPED_UNICODE); ?>;
var jmeno = <?php echo json_encode($jmeno_, JSON_UNESCAPED_UNICODE); ?>;
var pozice = <?php echo json_encode($pozice_, JSON_UNESCAPED_UNICODE); ?>;
var delka = <?php echo json_encode($delka_, JSON_UNESCAPED_UNICODE); ?>;
var opravneni = <?php echo json_encode($opravneni, JSON_UNESCAPED_UNICODE); ?>;
$(document).ready(function(){
refreshPrehled();
});
function refreshPrehled(){
$('#checkboxes').load('load_prehled.php', {
skill: skill,
skill_final: skill_final,
camp_final: camp_final,
jmeno: jmeno,
pozice: pozice,
delka: delka,
opravneni: opravneni
}, function(){
setTimeout(refreshPrehled, 12000);
});;
}
Second is below first on my page.
Does this mean only one setTimeout can run on one page?
And if thats the case, how do I put these two together into one script when I wanna load two different .php into two different html objects?
Thanks for help!
EDIT
Also html objects:
<div class="div_top_info" id="div_top_info"></div>
<form action="index.html" method="POST" id="checkboxes"></form>
I have tried different variations of timeouts, setInterval, different positioning, delay one of the timeouts. Really don't know. I fight this second day now.
EDIT2
Important info:
Sorry bad info.
They load, but into each php (load_status and load_prehled) I put this code:
$z_cas_editace = date('Y-m-d H:i', filemtime('datazelva_UL.txt'));
And second one updates the time and first one doesn't.
The first function also works fine when I put it in place of second.

Just my head spinning...
var skill = <?php echo json_encode($skill_, JSON_UNESCAPED_UNICODE); ?>;
var skill_final = <?php echo json_encode($skill_final, JSON_UNESCAPED_UNICODE); ?>;
var camp_final = <?php echo json_encode($camp_final, JSON_UNESCAPED_UNICODE); ?>;
var jmeno = <?php echo json_encode($jmeno_, JSON_UNESCAPED_UNICODE); ?>;
var pozice = <?php echo json_encode($pozice_, JSON_UNESCAPED_UNICODE); ?>;
var delka = <?php echo json_encode($delka_, JSON_UNESCAPED_UNICODE); ?>;
var opravneni = <?php echo json_encode($opravneni, JSON_UNESCAPED_UNICODE); ?>;
$(document).ready(function(){
refreshStatus();
refreshPrehled();
});
function refreshStatus(){
setTimeout(function(){
$('#div_top_info').load('load_status.php');
}, 2000);
}
function refreshPrehled(){
$('#checkboxes').load('load_prehled.php', {
skill: skill,
skill_final: skill_final,
camp_final: camp_final,
jmeno: jmeno,
pozice: pozice,
delka: delka,
opravneni: opravneni
}, function(){
setTimeout(refreshPrehled, 12000);
});;
}

Related

check php session by JavaScript ,if it does not exist should not cause any error

var secret_photo
var secret_name
if (!<?php echo isset($_SESSION['fb_item'])?'true':'false'; ?>) {
if($.cookie("domain[user]")){
secret_photo = 'noavatar.png';
secret_name = 'admin';
}
} else {
secret_photo = <?php echo json_encode($_SESSION['fb_item']['url']); ?>;
secret_name = <?php echo json_encode($_SESSION['fb_item']['name']); ?>;
}
I need to set php session to html using JavaScript .There is 2 logins , one is the facebook and the other is my own login system, facebook uses session and my own login sets cookie.
When I login by facebook , things are good , but if I login using my own login system , that echo check is going to cause problem , so how am I going to fix it ? Check if the session is available or not even if it doesn't exist it wont cause any problem.
You are probably encounter an error at the bottom of your code. You can't access $_SESSION['fb_item'] if it doesn't exist, it either shows a Notice or generate wrong js code secret_photo = ;. Just move condition to php side:
var secret_photo
var secret_name
<?php if(!isset($_SESSION['fb_item'])):?>
if($.cookie("domain[user]")){
secret_photo = 'noavatar.png';
secret_name = 'admin';
}
<?php else:?>
secret_photo = <?php echo json_encode($_SESSION['fb_item']['url']); ?>;
secret_name = <?php echo json_encode($_SESSION['fb_item']['name']); ?>;
<?php endif;?>
<script type="text/javascript">
var secret_photo
var secret_name
<?php if(!isset($_SESSION['fb_item'])){?>
if($.cookie('user')){
secret_photo = 'noavatar.png';
secret_name = 'admin';
}
<?php }else{?>
secret_photo = <?php echo json_encode($_SESSION['fb_item']['url']); ?>;
secret_name = <?php echo json_encode($_SESSION['fb_item']['name']); ?>;
<?php }?>
</script>

Ajax function on button press

I am trying to get information to post via PHP on button click, everything in my code looks right, and when I view the page source everything is filled in the way it should be. When I pull up the debugger when the button is clicked it does not make any calls at all, here is my button
<button type="button">Continue</button>
here is my ajax call
$(document).ready(function() {
$("button").click(function(){
var Id = <?php echo $give ?>;
var Ids = <?php echo $rec ?>;
var ex = <?php echo $exchange ?>;
var ret = <?php echo $tost ?>;
var email = <?php echo $email ?>;
var name = <?php echo $name ?>;
var pe = <?php echo $pe ?>;
var re = <?php echo $re ?>;
$.post("postts.php", {
Id: Id,
Ids: Ids,
ex: ex,
ret: ret,
email: email,
name: name,
pe: pe,
re: re
}, function(data){
alert(data);
$("p").text(data);
}, 'json' );
});
});
all of my vars will populate with the correct information, I just don't know why when I click the button it does nothing. I am not sure if I have made a typo, or if my syntax is wrong, this is only the second time I have done something like this.
You have error in writing variable values from PHP to JavaScript, you must wrap PHP output by quotation marks, so JavaScipt will "see" string ;)
instead of:
var email = <?php echo $email ?>;
write:
var mail = '<?php echo $email ?>';

Assigning PHP Variables To Javascript Variables Not Working

I am currently having trouble with this. I would like to make one of my variables in Javascript have a PHP value. Here is what I mean:
<script>
JSvariable = <?php echo $PHPvariable; ?>;
</script>
For some reason that is not working. Here is my full (snippet) of code:
<script>
currentreplyid = <?php echo $allpostcomments[$key]['replyid']; ?>;
$('#parentcommentholder').val(currentreplyid);
</script>
I am sure it is some stupid mistake, but I can not seem to find it! What is the problem? Thank you!
PS #parentcommentholder is an input field, and it just had the value 0 after the field is supposed to of been changed.
Here is some source:
<?php
$postcommentsquery = "SELECT * FROM comments WHERE parent = :parent AND postid = :postid ORDER BY datecreated DESC";
$postcommentsparams = array(':parent' => $allreplies[$key]["postid"],
':postid' => $postid);
try{
$postcommentsstmt = $connection->prepare($postcommentsquery);
$postcommentsresult = $postcommentsstmt->execute($postcommentsparams);
}
catch(PDOException $ex){
echo ("Failed to run query: " . $ex->getMessage());
}
$allpostcomments = $postcommentsstmt->fetchAll();
foreach ($allpostcomments as $key => $value) {
?>
<script>
var currentreplyid = <?php echo $allpostcomments[$key]['replyid']; ?>;
$('#parentcommentholder').val(currentreplyid);
</script>
<input id="parentcommentholder"></div>
Don't forgot for give quotes ' or ". Use following:
<script>
var JSvariable = '<?php echo $PHPvariable; ?>';
//or
var JSvariable = "<?php echo $PHPvariable; ?>";
</script>
Reason: If php variable contains string and if while assigning it to javascript variable we shall not give quote like:
<?php $PHPvariable = 'String';?>
var JSvariable = <?php echo $PHPvariable; ?>;
Will transform into :
var JSvariable = String;//which will give error in javascript
But this will work fine if PHP variable contains a numeric value like:
<?php $PHPvariable = 2;?>
var JSvariable = <?php echo $PHPvariable; ?>;
Will transform into :
var JSvariable = 2;//which will work perfect
Complete code should be:
<script>
var currentreplyid = "<?php echo $allpostcomments[$key]['replyid']; ?>";
//or if you are sure your variable contains int value
var currentreplyid = parseInt("<?php echo $allpostcomments[$key]['replyid']; ?>");
$('#parentcommentholder').val(currentreplyid);
</script>
Try the below instead of using javascript (as I don't think you need it):
<?php
$postcommentsquery = "SELECT * FROM comments WHERE parent = :parent AND postid = :postid ORDER BY datecreated DESC";
$postcommentsparams = array(':parent' => $allreplies[$key]["postid"],
':postid' => $postid);
try{
$postcommentsstmt = $connection->prepare($postcommentsquery);
$postcommentsresult = $postcommentsstmt->execute($postcommentsparams);
}
catch(PDOException $ex){
echo ("Failed to run query: " . $ex->getMessage());
}
$allpostcomments = $postcommentsstmt->fetchAll();
foreach ($allpostcomments as $key => $value) {
?>
<input id="parentcommentholder" value="<?php echo ((int)$allpostcomments[$key]['replyid']>0) ? $allpostcomments[$key]['replyid'] : 0; ?>" />
<?php
}
?>
If your defiantly sure $allpostcomments[$key]['replyid'] is bringing back a value, this should work without any issues.

array from php to javascript (need fresh decision)

I have found these two ways:
1st way (width incorrect)
<?php
$arr = new Array (...);
?>
<input id="arr" value="<?php echo json_encode($arr); ?>" hidden>
<script>
$(function (){
var arr = $("#arr").val(); // array with trash
});
</script>
2nd (we need control sequence)
<?php
$arrJS = json_encode($arr);
?>
<script>
var arrJS = <?php echo $arrJS; ?>; // good array
</script>
Question: Is there a better attempt?
Have you considered using AJAX? Here's an example:
Javascript:
$.ajax( "example.php" )
.done(function(json) {
var jsObject = json;
})
and in your 'example.php' file:
<?php
$arr = new Array ('element1'=>'value1', 'element2'=>'value2');
header("Content-type: text/json");
echo json_encode($arr);
exit;
?>

SyntaxError: missing ; before statement with AJAX

I came across this error while doing an ajax request and after looking over if I simply can't find the problem. Here is the code below.
$('#post_submit').click(function() {
var poster_id = <?php echo $session_user_id; ?>;
//firebug syntax error shows the line below is the problem
var profile_user_id: <?php echo $user_id; ?>;
var post = $('#post').val();
$.post('ajax_submit_post.php',
{
profile_user_id: profile_user_id,
poster_id: poster_id,
comment_type : comment_type,
post: post
},
function () {
$('#status').append('<li class="hide">Posted</li>').hide(1000);
});
});
I think you meant to use the equals sign?
var profile_user_id = <?php echo $user_id; ?>;
instead of
var profile_user_id: <?php echo $user_id; ?>;

Categories

Resources