Javascript passing through formatted int as string to function

Liquicidize

So in PHP I have this variable

$studID

which can have formatted int values as "00-00000".

I want to pass this through to a Javascript function so I did this:

<?php ...
onclick="MyFunction('.$studID.')";
... ?>

This is the Javascript file:

function MyFunction(id1){
  alert("You have entered: " + id1);
}

My problem is that it returns an integer value of the first half minus the second half instead of the ID itself!

Example:

Passed through: 15-00788

Result from function: -773 (instead of 15-00788)

Any help would be appreciated!

murrayju

Hard to tell you exactly without the full context of the code, but the problem is that you need to surround the data with quotes to pass it into the JavaScript function as a string. Something like:

<?php ...
onclick="MyFunction(\''.$studID.'\')";
... ?>

Should probably work. (Have to be sure to properly escape the quote characters)

Also, you should consider using double quotes in php, which supports inline variable expansion (instead of doing concatenation). See the docs

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Empty string after passing through the function

From Dev

Passing string variable through function in python 3.7.6

From Dev

Passing encoded string to Javascript function

From Dev

Passing string parameter in JavaScript function

From Dev

Passing HTML string to JavaScript function

From Dev

Trouble passing arguments through an initialiser function in javascript

From Dev

Passing an argument to a Javascript function through PHP

From Dev

Passing a variable to a function through a button (HTML/Javascript)

From Dev

javascript array to formatted string

From Dev

Passing a string to Function.prototype.apply() JavaScript

From Dev

Passing string to a Javascript function does not work

From Dev

Passing a JavaScript string to a Rust function compiled to WebAssembly

From Dev

Passing a string variable into a function parameter in Javascript

From Dev

Passing sql string variable into javascript function

From Dev

Passing a PHP string and an integer as arguments to a JavaScript function

From Dev

Passing through $this into function

From Dev

Passing function through useContext

From Dev

Passing parameter through function

From Dev

Passing string with a space between words through various function layers

From Dev

R Passing a string vector through tz in POSIXct function

From Dev

Passing String through Segue

From Dev

String not passing through segue?

From Dev

save formatted int to firestore for Javascript

From Dev

Passing two variables in route through javascript function in laravel

From Dev

Passing HTML through JavaScript

From Dev

Array Passing through javascript

From Dev

JQuery Javascript function to return highchart graph data source in JSON and formatted correctly through Selenium and C#

From Dev

Read formatted text from form through javascript

From Dev

Invoking a p:remoteCommand via a JavaScript function passing a message local to that function to another function through the "oncomplete" handler

Related Related

  1. 1

    Empty string after passing through the function

  2. 2

    Passing string variable through function in python 3.7.6

  3. 3

    Passing encoded string to Javascript function

  4. 4

    Passing string parameter in JavaScript function

  5. 5

    Passing HTML string to JavaScript function

  6. 6

    Trouble passing arguments through an initialiser function in javascript

  7. 7

    Passing an argument to a Javascript function through PHP

  8. 8

    Passing a variable to a function through a button (HTML/Javascript)

  9. 9

    javascript array to formatted string

  10. 10

    Passing a string to Function.prototype.apply() JavaScript

  11. 11

    Passing string to a Javascript function does not work

  12. 12

    Passing a JavaScript string to a Rust function compiled to WebAssembly

  13. 13

    Passing a string variable into a function parameter in Javascript

  14. 14

    Passing sql string variable into javascript function

  15. 15

    Passing a PHP string and an integer as arguments to a JavaScript function

  16. 16

    Passing through $this into function

  17. 17

    Passing function through useContext

  18. 18

    Passing parameter through function

  19. 19

    Passing string with a space between words through various function layers

  20. 20

    R Passing a string vector through tz in POSIXct function

  21. 21

    Passing String through Segue

  22. 22

    String not passing through segue?

  23. 23

    save formatted int to firestore for Javascript

  24. 24

    Passing two variables in route through javascript function in laravel

  25. 25

    Passing HTML through JavaScript

  26. 26

    Array Passing through javascript

  27. 27

    JQuery Javascript function to return highchart graph data source in JSON and formatted correctly through Selenium and C#

  28. 28

    Read formatted text from form through javascript

  29. 29

    Invoking a p:remoteCommand via a JavaScript function passing a message local to that function to another function through the "oncomplete" handler

HotTag

Archive