Calling JavaScript function from <a href> [duplicate] - javascript

This question already has answers here:
Call php function from JavaScript
(5 answers)
Closed 8 years ago.
I am trying to call a JavaScript function with parameter as php script using the following code
submit
any suggestions will be appreciated

Use something like this
submit
OR
var job ='';
job = <?php echo $job; ?>;
submit

If $job is anything but a number, your code will fail.
To drop a PHP variable into a JavaScript context, you must use json_encode. Since you're in an HTML attribute, you must also use htmlspecialchars.
So:
<a href="javascript:submitform(<?php echo htmlspecialchars(json_encode($job));?>);">

You can write in php directly
<?php
echo 'submit';
?>

You can also use any of this two
submit
submit

Related

Error Passing a variable from php to a javascript function [duplicate]

This question already has answers here:
Pass a PHP string to a JavaScript variable (and escape newlines) [duplicate]
(14 answers)
Closed 2 years ago.
I am trying to call a javascript function from php. The function takes one argument.
This is the call:
<?php
function welcomeBackMessage($user, $password){
echo "<h1>BACK: " . $user . "pwd " . $password . "</h1>";
echo "<script> welcomeBack({$user})</script>";
}
?>
This is the JS function; declared before the php functions calls it:
<script>
//Called form php to welcome back a guest
function welcomeBack(user){
print("JS WELCOME BACK CALLED");
var login = document.getElementById(loginbutton);
login.parentNode.removeChild(login);
}
The JS function never executes, and debuting the console I get the following:
<h1>BACK: guest2pwd 1234</h1><script> welcomeBack(guest2)</script>
Can't find variable: guest1.
The idea is to pass the variable to a JS function so I get update a DOM element with variable.
Any input appreciated.
You need quotes around the string.
echo "<script> welcomeBack('{$user}')</script>";

Calling javascript function with php variable as parameter from php code block [duplicate]

This question already has answers here:
Calling Javascript function in PHP while passing PHP variables into the function
(2 answers)
Closed 4 years ago.
I have tried all the post of stack overflow and from google but none of those seem to work for me.
This is my php code:
echo $firstPar;
echo $secondPar;
$tab_str.="<td width=\"20%\">‹";
This is the Javascript code:
function functionCall(firstPar, secondPar) {
alert(firstPar);
alert(secondPar);
}
If I am passing string values directly on the php call rather than passing the variables, I am getting the values inside the function. But the variable values are coming as null. Whereas the echo displays the variable values.
There should be quotes around parameters. Try using this:
$tab_str .= "<td width=\"20%\">‹";
Try like this
echo $firstPar;
echo $secondPar;
$tab_str .= '<td width="20%">‹';

How can one call a PHP function from JavaScript? [duplicate]

This question already has answers here:
What is the difference between client-side and server-side programming?
(3 answers)
Closed 3 years ago.
I have a piece of code, and I need to call a PHP code snippet when this function is called. Basically, it would be like this:
function jsFunc(){
//do PHP stuff
}
I don't know if this is a trivial thing, and I'm just not getting it, but I would appreciate any help.
If you want to do that in a .js file you can organize with Ajax calls
Here great example https://www.w3schools.com/js/js_ajax_php.asp
or if you use jQuery library
https://www.w3schools.com/jquery/jquery_ajax_load.asp
https://www.w3schools.com/jquery/jquery_ajax_get_post.asp
Do the following:
function jsFunc(){
var php_code = <?php
// your php code
?>;
}
EDIT: To call an object within the php code, use
echo $php_object
as suggested by noobcode.
Run it like:-
function jsFunc(){
//do PHP stuff
$pp = <?php echo "hello"; ?>;
console.log($pp);
}
Now you can easily use your function where i have used echo.

PHP - Javascript integration? [duplicate]

This question already has answers here:
What is the difference between client-side and server-side programming?
(3 answers)
Closed 7 years ago.
Suppose you are in a php file and you want to run a javascript function that has both javascript and php inside of it. How would you go about doing this?
Ex:
<?php
<script>
function myFunction(){
// php code
document.write("Hello World!");
}
</script>
?>
I know I can easily make a php function for this particular code, but just for future reference, I would like to know how to do this.
I apologize if this has been answered before but it seemed difficult to word in google search.
The PHP code has to output text that is valid inside the javascript. Something like this.
<?php
<script>
function myFunction(){
// php code
var message = "<?php echo "My alert message"; ?>";
window.alert(message);
}
</script>
?>

Pass a variable from a javascript function to another php file [duplicate]

This question already has answers here:
What is the difference between client-side and server-side programming?
(3 answers)
Closed 8 years ago.
i am new to javascript and php and i need your help. I have a javascript function inside a php file. In this javascript function i have a variable that i want to pass it to another php file. Can anyone help me how to do this.
Thanks
There are several ways you can do this. Probably the simplest way is:
var myData = 'whatever';
window.location.href = 'otherscript.php?myData=' + myData;
Then your script 'otherscript.php' can access that variable with $_GET['myData'].
This method requires a page refresh. You can do it without refreshing the page by using ajax, but that is a little bit trickier.
Something like that should work:
<script type="text/javascript">
function yourFunction(yourParameter) {
<?php $abc = "<script>document.write(yourParameter)</script>"?>
}
</script>
<?php
echo $abc;
?>

Categories

Resources