user2479441 user2479441 - 11 months ago 39
PHP Question

Javascript function only receive integers

I call for a java script function and send 4 parameters with it, it only works if there are only integers send for the 4th parameter.
Here is my


onchange="weekchange(this, <?php echo $i ?>,<?php echo $teller ?>,<?php echo $project[$i] ?>)"

When I call this function I send 4 parameters with it: this, $i, $teller and $project[$i].

When $project[$i] is only integers my java script function will accept it. This variable is the name of a project, this name can have numbers, letters and characters like !@#$%^& in it. When I have a project name that has a character that has a letter or characters like !@#$%^& in it, it doesn't do anything. Also when I only have integers and it starts with a 0 i get very weird results. 01234 ends up being 668?

My java script function:

function weekchange(selected, weekkeuze, aantalprojecten, projectnaam)
var samen;
var week;
var id;
var projecten = String(projectnaam);
week = selected.value;
id = weekkeuze;
samen = "id="+id+"&week="+week+"&projectnaam="+projecten;
var test = document.getElementById(id)
test = test+1;


How is this happening?


Strings in JavaScript must be in quotes; a handy way to output a string to the JavaScript layer is to use json_encode (which is happy to output JSON fragments, not just entire JSON documents). Quotes in attribute values (like onchange) should be marked up as HTML entities, so we can use htmlspecialchars to handle the output of json_encode. So:

onchange="weekchange(this, <?php echo $i ?>,<?php echo $teller ?>,<?php echo htmlspecialchars(json_encode($project[$i])) ?>)"