Reputation: 45
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
function:
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);
alert(selected.value);
week = selected.value;
id = weekkeuze;
samen = "id="+id+"&week="+week+"&projectnaam="+projecten;
var test = document.getElementById(id)
test = test+1;
submit_javascript("GET","capaciteitberekening.php?"+samen+"&dag="+0,id+"ma","true");
submit_javascript("GET","capaciteitberekening.php?"+samen+"&dag="+1,id+"di","true");
submit_javascript("GET","capaciteitberekening.php?"+samen+"&dag="+2,id+"wo","true");
submit_javascript("GET","capaciteitberekening.php?"+samen+"&dag="+3,id+"do","true");
submit_javascript("GET","capaciteitberekening.php?"+samen+"&dag="+4,id+"vr","true");
}
How is this happening?
Upvotes: 0
Views: 82
Reputation: 1074038
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])) ?>)"
Upvotes: 1
Reputation: 38345
You're using the PHP tags to dynamically define part of the HTML on the server, but when it reaches the browser it's going to be plain old regular JavaScript inside an element's onchange
attribute. So your code
onchange="weekchange(this, <?php echo $i ?>,<?php echo $teller ?>,<?php echo $project[$i] ?>)"
might result in a bit of HTML that looks like this:
onchange="weekchange(this, 1,some text,project 1234)"
That's not valid JavaScript, because project 1234
doesn't mean anything in JavaScript. Even without the spaces in there, project1234
, would mean that it needs to pass the value of a JavaScript variable called project1234
to the function, rather than passing the literal value project1234
.
You'll need to wrap any arguments you're generating from PHP in quotes, either single or double, like so:
onchange="weekchange(this, '<?php echo $i ?>','<?php echo $teller ?>','<?php echo $project[$i] ?>')"
Upvotes: 2