0

I've had a look around and can't seem to find a working solution, so here's the requirements.

I'm building a system that takes data from a master page and loads it into a modal style window for quick data processing. I have a javascript function passing 4 status parameters, and whilst 3 of them will always be integers, one of them can be integer or string.

The function works if all 4 parameters are integer, but fails when a string is passed.

function passJob(jobid,equipid,status,location) {

var a = document.getElementById('jobnumber');
var b = document.getElementById('equipid');
var c = document.getElementById('status');
var d = document.getElementById('location');
  a.value = jobid;
  b.value = equipid;  
  c.value = status;  
  d.value = location;  
}

PHP

<a href='#' onclick='passJob($sr,$eid,$ss,$sl);'>Modify Job</a>

$sr, $ss and $sl will always be numeric, $eid will either be integer, or a string starting with M and then having a number after it.

I've tried adding quotes to the variables, around the variables, inside the function etc and no luck :(

0

4 Answers 4

1

You need to pass as string if you do not know what they are - also make sure you do not nest the same type of quote:

onclick='passJob($sr,"$eid",$ss,$sl);'
Sign up to request clarification or add additional context in comments.

1 Comment

I've used this solution. Initially I was echoing the values into the function via php, whilst enclosed the ' character was coming through as an unexpected token. I've changed the programming style of that section of the page to allow echoing inside quote marks !
0

Just wrap it in quotes. This treats it like a string at all times to avoid any potential JavaScript parsing errors.

<a href="#" onclick="passJob($sr, '$eid', $ss, $sl);">Modify Job</a>

Comments

0

That is because you do not properly encode the variables in a Javascript notation. Try:

echo "<a href='#' onclick='passJob(".json_encode($sr).",".json_encode($eid).",".json_encode($ss).",".json_encode($sl).");'>Modify Job</a>";

1 Comment

No idea why this got down-voted. JSON encoding the variable is the most reliable way to pass it to JavaScript without needing to know what the variable actually is.
0

Do like below

<a href="#" onclick="passJob($sr, '$eid', $ss, $sl);">Modify Job</a>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.