JQUERY rounding number when posting to PHP? - php

I'm trying to pass $id which for exemple is 12345678909728062 (When I echo it I get this value in index.php), but when I pass it to another php-file with JQUERY it rounds it to 12345678909728060. I would like to understand why this happens? I have tested with other numbers in the id aswell with same result of rounding the last 2 digits.
<?php
include 'php/header.php';
?>
<script>
$(document).ready(function() {
$("button").click(function() {
$(".r_div").load("php/items/purchase.php", {
user_id: <?php echo $id; ?>,
fired_button: $(this).val()
});
});
});
</script>
<div class="r_container">
<div class="r_div">
<?php
include 'php/items/store_load.php';
?>
</div>
</div>
<?php
include 'php/footer.php';
?>

I found the problem being the way numbers work in programming, so passing it as a string solved the problem.
user_id: "<?php echo $id; ?>",

Related

Jquery reload div content on select change using php ajax

OK here is my code
portfolio.php
<select name="portfolio" id="portfolio_dropdown" class="service-dropdown">
<?php foreach($years as $year){ ?>
<option value="<?php echo $year['year']; ?>"><?php echo $year['year']; ?></option>
<?php } ?>
</select>
<div class="loading"></div>
<div id="portfolio">
<div id="port-cont">
<?php foreach($portfolios as $portfolio){ ?>
<div class="video">
<div class="play">
Play
</div>
<iframe src="http://www.youtube.com/v/<?php echo $portfolio['url']; ?>"frameborder="0" allowfullscreen></iframe>
<h3><?php echo $portfolio['title']; ?></h3>
<p><?php echo $portfolio['text']; ?></p>
</div>
<?php } ?>
</div>
</div>
my js code
$("body").on('change','#portfolio_dropdown',function(){
var year = $(this).val();
$.ajax({
type: "POST",
url: "catalog/controller/portfolio.php",
data: "year="+year,
beforeSend: function(){
$(".loading").show();
$("#portfolio").empty();
},
success: function(portfolio_data){
$(".loading").hide();
$("#portfolio").html(portfolio_data);
}
});
});
my portfolio controller file
if(isset($_POST['year'])){
include_once "../../system/validation.php";
include_once "../model/DataBase.php";
include_once "../model/Display.php";
$year = integer($_POST['year']);
$get_portfolio_data = new Display("portfolio");
$portfolios = $get_portfolio_data->getDataByColumnName("year",$year);
include_once "../view/themes/default/template/portfolio_data.php";
exit();
}
My portfolio file "the first one " i get my data with another function that i get my last year(2014) portfolio data
so when i change the year i call my controller file to get the data related to that year
my question here is how to return $portfolios that i got from my controller file so i don't have to change my template file code or include another file as i did
i mean i need to reload my portfolio.php template content
i tried something like this
$("body").on('change','#portfolio_dropdown',function(){
var year = $(this).val();
$.ajax({
type: "POST",
url: "catalog/controller/portfolio.php",
data: "year="+year,
beforeSend: function(){
$(".loading").show();
$("#portfolio").hide();
},
success: function(portfolio_data){
$(".loading").hide();
$("#portfolio").show().load("portfolio" + " #port-cont");
}
});
});
but i don't know how to send my new $portfolios data to the same div without including another template file
Actually i'm little confused how to do this properly
So i hope you got what i want to do :)
You can create a separate portfolio-div.php file that only contains the code you want to replace with, and call that from the $.ajax call.
Or you can add a $_GET parameter like ?skiptemplate=1 to portfolio.php so that when you call the file with that parameter, the surrounding template markup is skipped when the file is called from an ajax call.
If you do not have the opportunity to change the server-side code, you can load the whole template file again, but extract only the #port-cont part to replace the existing content. Don't use load() (which itself will call an URL), use html() instead:
$("#portfolio").show().html(portfolio_data);
(though this will insert all HTML, not only the #port-cont part)
$("#portfolio").html(portfolio_data)
Just pass the parameter into html

Updating $_SESSION variables using Ajax

On my main page I assign four different $_SESSION variables as follows:-
<p class="p-wht space left">Working: <? echo $_SESSION['Working'] ?></p>
<p class="p-wht green space left">Clear: <? echo $_SESSION['Clear'] ?></p>
<p class="p-wht red space left">Busy: <? echo $_SESSION['Busy'] ?></p>
<p class="p-wht cyan space left">STC: <? echo $_SESSION['STC'] ?></p>
These are set when the page loads:-
include('status.controller.php');
and they are set as:
$_SESSION['Working'] = $status['Working'];
$_SESSION['Clear'] = $status['Clear'];
$_SESSION['Busy'] = $status['Busy'];
$_SESSION['STC'] = $status['STC'];
They update when the page is refreshed or first loaded, but when I am trying to update them using AJAX the values don't seem to be changing.
I have tried:-
$(function(){
setInterval(function(){
$.ajax({
url:'scripts/php/status.controller.php',
success:function(data){
if(data == "scripts/php/status.controller.php"){
}else{
alert(data);
//location.reload();
}
}
});
},4000);
});
but this doesn't seem to update the values. It only updates the values when the page is completely reloaded, including location.reload(); which I don't want to use.
Any ideas?
ADDED; contents of status.controller.php file; here is the contents of this file; nathandasilva.co.uk/status.controller.txt
Can you tell me, what is the output of scripts/php/status.controller.php page. If the page will send html as output, then you should just replace the content, example:
<div id="content">
<p class="p-wht space left">Working: <? echo $_SESSION['Working'] ?></p>
</div>
<script>
$(function() {
setInterval(function(){
$.ajax({
url:'scripts/php/status.controller.php',
success:function(data) {
$("#content").html(data);
}
});
},4000);
});
</script>

jquery click function with different database id

So I have a..
.php
<?php
while($row = $results->fetch())
{
?>
<span class="rss-badge" id="<?php echo $row['id_ilmoitus'] ?>">Enlarge</span>
<br />
<?php
}
?>
.jquery
$("<?php echo $row['id_ilmoitus] ?>").click(function(){
alert("pressed", "<?php echo $row['id_ilmoitus] ?>");
});
My plan is to make that specific span to do something. So that is why I want every span have different Ids and jquery to use. There are numerous id's in database.
Try something like this :
$('.rss-badge').on('click',function(){
alert($(this).id);
//--- LET'S DO IT
});
First of all your syntax is wrong you are missing # before that.
Secondly,Instead of adding multiple event on every id, add one on class selector and get id of the item clicked:
$('span.rss-badge').click(function(){
alert("pressed"+ this.id);
});
Basically you are using wrong selector at first, in your html rendering an id cannot start with numbers it should be an alphabetic character.
<?php
while($row = $results->fetch())
{
?>
<span class="rss-badge" id="data<?php echo $row['id_ilmoitus'] ?>">Enlarge</span>
<br />
<?php
}
?>
$('#id') is the correct selector if you want to bind click event
var id = "#data<?php echo $row['id_ilmoitus]; ?>";
$(id).click(function(){
alert("pressed", "<?php echo $row['id_ilmoitus] ?>");
});

Slider bar dynamic on javascript php and html

I have this code for create dynamic number of sliders with dynamic number of answers per slider and I can't understand two things of my code:
Why only show one slider? (The first id slider number on my sql table)
Why in the amount box only appears correct the ini value? When you move the slider appears A r r a y (one character each time that you move the slider) ?
<script>
var arrayslidersvalueanswers="<?= $tableslidersanswervalues ?>";
<?php foreach ($tableslidersqid as $qid) { ?>
<?php foreach ($qid as $index) { ?>
<?php echo $index;?>
<?php foreach ($tableslidersanswersid as $qanswerid) { ?>
<?php foreach ($qanswerid as $indexanswer) { ?>
var answers="<?= $qanswerid ?>";
var qid= "<?= $index ?>";
jQuery(document).ready(function(){
/////////////////////SLIDER !!!!!!!!!!!!!!!!!!!!!!!!!!!
var valor="<?= $tableslidersanswervalues[1][0] ?>"; // Ini slider
$(document.getElementById("amount"+qid+"[]")).val(valor);
$(document.getElementById("slider"+qid+"[]")).slider({
min: 1,
max:arrayslidersvalueanswers.length,
slide: function( event, ui) {
var ans=answers[0][1] ;
$(document.getElementById("amount"+qid+"[]")).val(answers[ui.value-1][0]);
} //end slider ini method builder
});//END OF FUNCTION
});
});// end JavaScript document function
<?php } ?>
<?php } ?>
<?php } ?>
<?php } ?>
</script>
<html>
<p>
<label for="amount< ?= $row_Answer['QuestionIDFK'];?>">< ?php echo $row_questionset['QuestionValue']; ?>< /label>
<input type="text" id="amount< ?= $row_Answer['QuestionIDFK'];?>[]" name="amount< ?= $row_Answer['QuestionIDFK'];?>[]" />
</p>
<div id="slider< ?= $row_Answer['QuestionIDFK'];?>[]" name="slider< ?php echo $row_Answer['AnswerValue']; ?>[]" >< /div>
</html>
You only have one slider in your HTML.
I think your foreach loops should go over your HTML to generate more markup and sliders. Then you can use a general selector in jquery to get all the sliders to work.
$("div[id^='slider']").slider(...
will select all divs with an id starting by "slider".
The way your code should be structured should be:
<script>
$(("div[id^='slider']").slider(//slider configuration here);
</script>
<html>
<?php foreach($allTableValues as $table): ?>
<label>...
<input>....
<slider id="slider_<?php echo $table->id ?>">...
<?php endforeach; ?>
</html>

Can't use PHP value from jQuery.load() content

I'm working on a project that uses jQuery modals that dynamically load their content on a button click.
I am having an issue using this loaded content like I would normally.
Here is an example of my problem
<script type="text/javascript>
click function{
load modal{
open: $('#modalID').load('phpfile.php?id=<?php echo $id ?>');
}
</script>
That all works fine, but when trying to use jQuery within the "phpfile.php" is where the problem lies
<?php
$id = $_GET['id'];
$db = USE ID TO GET DATABASE INFO; //works fine here
?>
//ECHO SOME HTML HERE
<script type="text/javascript">
$('#buttonID').on('click', function(){
alert('test <?php echo $id ?>');
});
</script>
When I click the button, I get the alert but it just says test and I don't get the ID like I should.
Thanks in advance for your help and suggestions on this one!
You have pasted pseudo code so it is difficult to say what is wrong in your code.
The following should work, give it a try:
File 1.
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
<?
$id = "10";
?>
<script>
$(document).ready(
function(){
$('#modalID').load('phpfile.php?id=<?php echo $id ?>');
});
</script>
<div id = 'modalID'></div>
And phpfile.php:
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
<?php
$id = $_GET['id'];
?>
<script>
$('#buttonID').on('click', function(){
alert('test <?php echo $id ?>');
});
</script>
<input type="button" id = "buttonID" value="click me">
Please check on the do you have any errors in your browser's console. The above code should work. The possibilities are like if you don't get your $_GET['id'] or any script errors will prevent execution of your code.
I have created a sample php file which is working fine.
<?php
$id = rand();
?>
<button id="buttonID"> Click </button>
//ECHO SOME HTML HERE
<script type="text/javascript">
$('#buttonID').on('click', function(){
alert('test <?php echo $id ?>');
});
</script>

Categories