Execute PHP script inside JavaScript
Thanks guys, used AJAX and solved the problem.
function connect(num) {
var x = document.getElementById('myBtn');
$.ajax({
type: "POST",
url: "request-connect.php?id="+num
})
modal.style.display = "block";
}
How to include in .php a .html with links to a .js that has php codes
In your PHP file, create a global variable containing your JSON in a tag:
<script>var myNum = <?php echo json_encode($value); ?>;</script>
and then reference that variable in your script file with myNum
.
How can I call PHP functions by JavaScript?
Yes, you can do ajax request to server with your data in request parameters, like this (very simple):
Note that the following code uses jQuery
jQuery.ajax({
type: "POST",
url: 'your_functions_address.php',
dataType: 'json',
data: {functionname: 'add', arguments: [1, 2]},
success: function (obj, textstatus) {
if( !('error' in obj) ) {
yourVariable = obj.result;
}
else {
console.log(obj.error);
}
}
});
and your_functions_address.php like this:
<?php
header('Content-Type: application/json');
$aResult = array();
if( !isset($_POST['functionname']) ) { $aResult['error'] = 'No function name!'; }
if( !isset($_POST['arguments']) ) { $aResult['error'] = 'No function arguments!'; }
if( !isset($aResult['error']) ) {
switch($_POST['functionname']) {
case 'add':
if( !is_array($_POST['arguments']) || (count($_POST['arguments']) < 2) ) {
$aResult['error'] = 'Error in arguments!';
}
else {
$aResult['result'] = add(floatval($_POST['arguments'][0]), floatval($_POST['arguments'][1]));
}
break;
default:
$aResult['error'] = 'Not found function '.$_POST['functionname'].'!';
break;
}
}
echo json_encode($aResult);
?>
Related Topics
Swift Sync Data of Core Data an MySQL Database
Php: Split String into Array, Like Explode with No Delimiter
Wampserver PHPmyadmin Maximum Execution Time of 360 Seconds Exceeded
How to Add Square Braces Around Subarray Data Inside of a JSON Encoded String
A Recursive Remove Directory Function for PHP
Create Image from Url Any File Type
How to Change Variables in the .Env File Dynamically in Laravel
How to Alias the Name of a Column in Eloquent
How to Execute PHP Code Within JavaScript
Php/Symfony/Doctrine Memory Leak
PHP Simple Foreach Loop with HTML
Increase Days to PHP Current Date()
Send Xml Data to Webservice Using PHP Curl
Setting a PHP Script as a Windows Service